我正在尝试使用@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中:
REST App
com.sun.jersey.spi.container.servlet.ServletContainer
1
Jersey Web Application
/rest/*
第二个应用程序是客户端.
现在我有以下客户端代码:
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);
但我不想使用jersey的Client,ClientResponse和WebResource.
我想用@Consumes做这个.
客户端appliaction web.xml是否应包含一些其他参数?
双方(客户端和服务器)包含HotelRESTInfo类:
@XmlRootElement
public class HotelRESTInfo {
…
}
发布者:全栈程序员-站长,转载请注明出处:https://javaforall.net/221816.html原文链接:https://javaforall.net
