Jersey REST API的多种资源 [英] Jersey REST api multiple resources
问题描述
请参见下面的代码.我可以访问 http://localhost:8080/messengerdemo/messages 并与所有API交互,但是每次都可以我访问 http://localhost:8080/messengerdemo/profiles 我遇到了404 not found错误.我做错什么了?我是一个初学者,正在尝试学习jersey和REST API.
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>
消息资源
@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();
}
}
个人资料资源.
@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();
}
}
推荐答案
在删除所有相关类并重新创建它们之后.一切正常.
After deleting all the related class and recreated them. It all worked.
我想这是因为我没有检查我的REST客户端工具中Content-Type
是否设置为application/json
.
I guess it is because I didn't check if the Content-Type
is set to be application/json
within my REST client tool.
这篇关于Jersey REST API的多种资源的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!