users@jersey.java.net

Re: [Jersey] Cannot more _at_provider classes

From: Paul Sandoz <Paul.Sandoz_at_Sun.COM>
Date: Mon, 22 Jun 2009 16:44:00 +0200

Hi,

The problem is the isWriteable methods of your providers. Even though
you declare the type as a generic parameter of the class you also need
to check it in the isWriteable method (see end of email).

JAX-RS specification does not state that the generic type should be
taken into account when selecting an appropriate writer (an
implementation could do so to optimize look up).

What is happening is that one provider is getting selected for both
the Car and Trunk types and there will be a class cast exception if a
Truck instance is passed to the CarWriter or vice versa.

Hope this helps,
Paul.

@Provider
@Produces(MediaType.TEXT_PLAIN)
public class CarWriter implements MessageBodyWriter<Car> {

        @Override
        public long getSize(Car arg0, Class<?> arg1, Type arg2, Annotation[]
arg3,
                        MediaType arg4) {
                // TODO Auto-generated method stub
                return -1;
        }

        @Override
        public boolean isWriteable(Class<?> arg0, Type arg1, Annotation[] arg2,
                        MediaType arg3) {
                // TODO Auto-generated method stub
                return arg0 == Car.class;
        }
         ...

@Provider
@Produces(MediaType.TEXT_PLAIN)
public class TruckWriter implements MessageBodyWriter<Truck> {

        @Override
        public long getSize(Truck arg0, Class<?> arg1, Type arg2,
                        Annotation[] arg3, MediaType arg4) {
                // TODO Auto-generated method stub
                return -1;
        }

        @Override
        public boolean isWriteable(Class<?> arg0, Type arg1, Annotation[] arg2,
                        MediaType arg3) {
                // TODO Auto-generated method stub
                return arg0 == Truck.class
        }
         ...

Paul.

On Jun 22, 2009, at 4:29 PM, Schwascho wrote:

>
> Hi,
> I'm just new on JAX-RS and I did a little example on Jersey. I built
> two
> provider classes that should print out the information of the
> classes called
> "Car" and "Truck". If i do a GET request on the first ressource
> resource it
> works. On the the second request i get the follwing message:
>
> javax.servlet.ServletException: non-HTTP request or response
> at
> com
> .sun
> .jersey
> .spi
> .container.servlet.ServletContainer.service(ServletContainer.java:241)
> at
> org
> .apache
> .catalina
> .core
> .ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:
> 290)
> at
> org
> .apache
> .catalina
> .core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206)
> at
> org
> .jboss
> .web
> .tomcat.filters.ReplyHeaderFilter.doFilter(ReplyHeaderFilter.java:96)
> at
> org
> .apache
> .catalina
> .core
> .ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:
> 235)
> at
> org
> .apache
> .catalina
> .core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206)
> at
> org
> .apache
> .catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:
> 235)
> at
> org
> .apache
> .catalina.core.StandardContextValve.invoke(StandardContextValve.java:
> 191)
> at
> org
> .jboss
> .web
> .tomcat
> .security
> .SecurityAssociationValve.invoke(SecurityAssociationValve.java:190)
> at
> org
> .jboss
> .web.tomcat.security.JaccContextValve.invoke(JaccContextValve.java:92)
> at
> org.jboss.web.tomcat.security.SecurityContextEstablishmentValve.process
> (SecurityContextEstablishmentValve.java:126)
> at
> org
> .jboss
> .web
> .tomcat
> .security
> .SecurityContextEstablishmentValve
> .invoke(SecurityContextEstablishmentValve.java:70)
> at
> org
> .apache
> .catalina.core.StandardHostValve.invoke(StandardHostValve.java:127)
> at
> org
> .apache
> .catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:102)
> at
> org
> .jboss
> .web
> .tomcat
> .service.jca.CachedConnectionValve.invoke(CachedConnectionValve.java:
> 158)
> at
> org
> .apache
> .catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:
> 109)
> at
> org
> .apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:
> 330)
> at
> org
> .apache.coyote.http11.Http11Processor.process(Http11Processor.java:
> 828)
> at
> org.apache.coyote.http11.Http11Protocol
> $Http11ConnectionHandler.process(Http11Protocol.java:601)
> at
> org.apache.tomcat.util.net.JIoEndpoint$Worker.run(JIoEndpoint.java:
> 447)
> at java.lang.Thread.run(Thread.java:619)
>
> Here my web.xml
>
> <?xml version="1.0" encoding="UTF-8"?>
> <web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
> xmlns="http://java.sun.com/xml/ns/javaee"
> xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
> xsi:schemaLocation="http://java.sun.com/xml/ns/javaee
> http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" id="WebApp_ID"
> version="2.5">
> <display-name>JerseyTest</display-name>
> <servlet>
> <display-name>JAX_RS REST Servlet</display-name>
> <servlet-name>REST-Servlet</servlet-name>
>
> <servlet-
> class>com.sun.jersey.spi.container.servlet.ServletContainer</servlet-
> class>
> </servlet>
> <servlet-mapping>
> <servlet-name>REST-Servlet</servlet-name>
> <url-pattern>/services/*</url-pattern>
> </servlet-mapping>
> </web-app>
>
> The Car class and the Truck class have the follwing format:
>
> public class Car {
> private String marke;
> private String modell;
>
> public Car() {
> }
>
> public Car(String marke, String modell) {
> this.marke = marke;
> this.modell = modell;
> }
>
> //Getters and Setters
> }
>
> The first Ressource:
>
> @Path("test2")
> public class CarService {
>
> @GET
> @Path("car")
> @Produces(MediaType.TEXT_PLAIN)
> public Car getCar(@Context HttpHeaders headers) {
> MediaType type = headers.getMediaType();
> Car c1 = new Car("VW", "Passat");
> return c1;
> }
> }
>
> The other one:
>
> @Path("test1")
> public class TruckService {
>
> @GET
> @Path("truck")
> @Produces(MediaType.TEXT_PLAIN)
> public Truck getTruck(@Context HttpHeaders headers) {
> MediaType type = headers.getMediaType();
> Truck t1 = new Truck("Scania", "L2");
> return t1;
> }
> }
>
> The first provider:
>
> @Provider
> @Produces(MediaType.TEXT_PLAIN)
> public class TruckWriter implements MessageBodyWriter<Truck> {
>
> @Override
> public long getSize(Truck arg0, Class<?> arg1, Type arg2,
> Annotation[] arg3, MediaType arg4) {
> // TODO Auto-generated method stub
> return -1;
> }
>
> @Override
> public boolean isWriteable(Class<?> arg0, Type arg1, Annotation[]
> arg2,
> MediaType arg3) {
> // TODO Auto-generated method stub
> return true;
> }
>
> @Override
> public void writeTo(Truck t, Class<?> arg1, Type arg2,
> Annotation[] arg3, MediaType arg4,
> MultivaluedMap<String, Object> arg5, OutputStream outputStream)
> throws IOException, WebApplicationException {
> PrintWriter out = new PrintWriter(outputStream);
> out.write("Marke :" + t.getMarke());
> }
>
> }
>
>
> And the second provider:
>
> @Provider
> @Produces(MediaType.TEXT_PLAIN)
> public class CarWriter implements MessageBodyWriter<Car> {
>
> @Override
> public long getSize(Car arg0, Class<?> arg1, Type arg2,
> Annotation[] arg3,
> MediaType arg4) {
> // TODO Auto-generated method stub
> return -1;
> }
>
> @Override
> public boolean isWriteable(Class<?> arg0, Type arg1, Annotation[]
> arg2,
> MediaType arg3) {
> // TODO Auto-generated method stub
> return true;
> }
>
> @Override
> public void writeTo(Car car, Class<?> arg1, Type arg2, Annotation[]
> arg3,
> MediaType arg4, MultivaluedMap<String, Object> arg5,
> OutputStream outputStream) throws IOException,
> WebApplicationException {
> PrintWriter out = new PrintWriter(outputStream);
> out.write(car.getMarke());
>
> }
>
> }
>
> Has anybody an idea? Thanks
>
>
>
>
>
>
>
> --
> View this message in context: http://n2.nabble.com/Cannot-more-%40provider-classes-tp3136533p3136533.html
> Sent from the Jersey mailing list archive at Nabble.com.
>
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: users-unsubscribe_at_jersey.dev.java.net
> For additional commands, e-mail: users-help_at_jersey.dev.java.net
>