java 带有@Consumes、@Produces 和 JAXB 的简单 JAX-RS 示例
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/12987912/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Simple JAX-RS example with @Consumes, @Produces and JAXB
提问by LancerX
I'm trying to create and run simple example of JAX-RS using @Produces
, @Consumes
annotation and JAXB.
我正在尝试使用@Produces
、@Consumes
注释和 JAXB创建和运行 JAX-RS 的简单示例。
@Stateless
@LocalBean
@Path("/hotel")
public class RestMain {
@GET
@Produces(MediaType.APPLICATION_XML)
@Path("{hotelId}")
public HotelRESTInfo read(@PathParam("hotelId") long hotelId) {
HotelDataSourceFake hotelDataSourceFake = new HotelDataSourceFake();
HotelRESTInfo hotelInfo = hotelDataSourceFake.getFakePlaceById(hotelId);
return hotelInfo;
}
}
web.xml:
网页.xml:
<servlet>
<servlet-name>REST App</servlet-name>
<servlet-class>com.sun.jersey.spi.container.servlet.ServletContainer
</servlet-class>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>Jersey Web Application</servlet-name>
<url-pattern>/rest/*</url-pattern>
</servlet-mapping>
The second application which is the client. Now I have the following client code:
第二个应用程序是客户端。现在我有以下客户端代码:
import com.sun.jersey.api.client.Client;
import com.sun.jersey.api.client.ClientResponse;
import com.sun.jersey.api.client.WebResource;
...
Client client = Client.create();
String uri ="http://localhost:8080/RESTJEE/rest/hotel/" + hotelId;
WebResource resource = client.resource(uri);
ClientResponse response = resource.accept("application/xml").get(ClientResponse.class);
HotelRESTInfo hotelRestInfo = response.getEntity(HotelRESTInfo.class);
But I don't want to use jersey's Client, ClientResponse and WebResource.
I want to do this with @Consumes
.
Should client appliaction web.xml
contain some additional parameters?
但是我不想使用 jersey 的 Client、ClientResponse 和 WebResource。我想用@Consumes
. 客户端应用程序是否应该web.xml
包含一些附加参数?
Both sides (client and server) contain the HotelRESTInfo
class:
双方(客户端和服务器)都包含HotelRESTInfo
类:
@XmlRootElement
public class HotelRESTInfo {
...
}
回答by Nicolas Zozol
I think that you are mismatching something.
我认为你不匹配的东西。
You have on one side the HttpClient that make requests, and on a other computer the HttpServer that build responses. It's basic and I suppose you get it.
您在一侧有发出请求的 HttpClient,在另一台计算机上有构建响应的 HttpServer。这是基本的,我想你明白了。
The thing is that the @GET read ()method
consumesthe request body, and producesthe response body.
问题是@GET read ()method
消费请求体,并产生响应体。
So you can have :
所以你可以有:
@GET
@Consumes(MediaType.APPLICATION_XML) //client sends also xml
@Produces(MediaType.APPLICATION_XML)
@Path("{hotelId}")
public HotelRESTInfo read(@PathParam("hotelId") long hotelId) {
(...)
}
Obviously, you would like that your client consumes webservices, so @Consume
definitively makes sense in the Client side.
显然,您希望您的客户端使用 webservices,因此@Consume
在 Client 端绝对有意义。
Unfortunately, JaxRS was built on the server side in 2008 or so, without thinking of synergies with a Java client. And @Consumes is definitively a server annotation, and I haven't seen in the documentation anything about reusingannotations on the client.
不幸的是,JaxRS 是在 2008 年左右构建在服务器端的,没有考虑与 Java 客户端的协同作用。@Consumes 绝对是一个服务器注释,我在文档中没有看到任何关于在客户端上重用注释的内容。
The Jersey client is pretty recent, in an effort of the JaxRS 2 specifications. Your questions shows that these specs may be difficult to write !
Jersey 客户端是最近开发的,符合 JaxRS 2 规范。您的问题表明这些规范可能难以编写!