I’m trying to create and run simple example of JAX-RS using @Produces, @Consumes annotation and JAXB.
@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:
<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?
Both sides (client and server) contain the HotelRESTInfo class:
@XmlRootElement
public class HotelRESTInfo {
...
}
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.
The thing is that the
@GET read ()methodconsumes the request body, and produces the response body.So you can have :
Obviously, you would like that your client consumes webservices, so
@Consumedefinitively makes sense in the Client side.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 reusing annotations on the client.
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 !