Jersey REST api multiple resources

人盡茶涼 提交于 2019-12-12 17:27:09

问题


Please see codes below. I can visit http://localhost:8080/messengerdemo/messages and interact with all the APIs but every time I access http://localhost:8080/messengerdemo/profiles I got a 404 not found error. What did I do wrong? I am a beginner and trying to learn jersey and REST API.

web.xml

  <servlet>
    <servlet-name>Jersey Web Application</servlet-name>
    <servlet-class>org.glassfish.jersey.servlet.ServletContainer</servlet-class>
    <init-param>
        <param-name>jersey.config.server.provider.packages</param-name>
        <param-value>org.learn.rest.messengerdemo.resources</param-value>
    </init-param>
    <load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
    <servlet-name>Jersey Web Application</servlet-name>
    <url-pattern>/*</url-pattern>
</servlet-mapping>

Message resources

@Path("/messages")
@Consumes(MediaType.APPLICATION_JSON)
@Produces(MediaType.APPLICATION_JSON)
public class MessageResource {
   MessageService messageService = new MessageService();

   @GET
   public List<Message> getMessages() {
      return messageService.getAllMessages();
   }
}

Profile resources.

@Path("/profiles")
@Consumes(MediaType.APPLICATION_JSON)
@Produces(MediaType.APPLICATION_JSON)
public class ProfileResource {

   private ProfileService profileservice = new ProfileService();

   @GET
   public List<Profile> getAllProfiles() {
      return profileservice.getAllProfiles();
   }
}

回答1:


After deleting all the related class and recreated them. It all worked.

I guess it is because I didn't check if the Content-Type is set to be application/json within my REST client tool.



来源:https://stackoverflow.com/questions/33381087/jersey-rest-api-multiple-resources

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!