users@jersey.java.net

Re: [Jersey] Response inconsistency in framework.......

From: Paul Sandoz <Paul.Sandoz_at_Sun.COM>
Date: Mon, 18 May 2009 18:07:25 +0200

Hi Jalpesh,

This is a problem of type erasure and limitations of the Java
languages. When you create an instance of something like
ArrayList<String> the generic type information is lost.

You need to wrap your implementation around an extended instance of
GenericEntity:

   https://jsr311.dev.java.net/nonav/javadoc/javax/ws/rs/core/GenericEntity.html

An alternative is to utilized arrays e.g. OrderData[]. Array type
information is preserved by instances so for such cases you do not
require the use of GenericEntity.

Hope this helps,
Paul.

BTW we tried really hard to make Response generic but failed as we
could not make it work correctly with the concept of building responses.

On May 18, 2009, at 5:53 PM, Jalpesh Patadia wrote:

> Hello everyone,
>
> Thanks for everybody’s [especially Paul’s] help in helping me
> resolve some of the issues I’ve had in the past. I’m having one more
> J issue that I’d need help for, and searching the web didn’t give
> me the right answer, so I figured I’d ask here.
>
> Basically I have this code (which works fine) :
>
> public List<OrderData> getOrders(@Context HttpServletRequest
> request, @QueryParam("startDate") String stDate) throwsParseException
> {
> List<OrderData> returnList = // geth the order details…
> return returnList;
> }
>
> OrderData is defined as :
>
> @XmlRootElement
> @XmlAccessorType(XmlAccessType.FIELD)
> public class OrderData
> {
> @XmlElement(nillable = true) private Date date;
> ….
> {
>
> This code behaves correctly and when I request with an Accept header
> value of application/xml , or application/json – it returns the
> right output.
>
> Now what I need to do is instead of returning a status code of 200
> [OK], I want it to return 206 [Partial Return]. So I modified my
> code to be this :
>
> public Response getOrders(@Context HttpServletRequest request,
> @QueryParam("startDate") String stDate) throws ParseException
> {
> List<OrderData> returnList = // geth the order details…
>
> // create the response out of the order.
> Response rs = Response.status(206).entity(returnList).build();
> return rs;
> }
>
>
> I would expect that this should return the same data, but with the
> status code 206 instead of 200. However I am getting a response code
> of 500 [Internal Server Error] with a message body :
>
> “A message body writer for Java type, class java.util.ArrayList, and
> MIME media type, application/xml, was not found”
>
> Would any of you guys tell me what I’m missing from my configuration
> that’s causing this issue.
>
>
> Thanks,
>
> Jalpesh
>
>
> PS: Here’s the stack trace thrown in the server logs with this
> message. I’m on version 1.0.3 of jersey.
>
>
> May 18, 2009 9:48:32 AM
> com.sun.jersey.spi.container.ContainerResponse write
> SEVERE: A message body writer for Java type, class
> java.util.ArrayList, and MIME media type, application/xml, was not
> found
> May 18, 2009 9:48:32 AM
> com.sun.jersey.server.impl.application.WebApplicationImpl onException
> SEVERE: Internal server error
> javax.ws.rs.WebApplicationException
> at
> com
> .sun
> .jersey.spi.container.ContainerResponse.write(ContainerResponse.java:
> 241)
> at
> com
> .sun
> .jersey
> .server
> .impl
> .application
> .WebApplicationImpl._handleRequest(WebApplicationImpl.java:689)
> at
> com
> .sun
> .jersey
> .server
> .impl
> .application
> .WebApplicationImpl.handleRequest(WebApplicationImpl.java:612)
> at
> com
> .sun
> .jersey
> .server
> .impl
> .application
> .WebApplicationImpl.handleRequest(WebApplicationImpl.java:603)
> at
> com
> .sun
> .jersey.spi.container.servlet.WebComponent.service(WebComponent.java:
> 309)
> at
> com
> .sun
> .jersey
> .spi
> .container.servlet.ServletContainer.service(ServletContainer.java:425)
> at
> com
> .sun
> .jersey
> .spi
> .container.servlet.ServletContainer.service(ServletContainer.java:590)
> at javax.servlet.http.HttpServlet.service(HttpServlet.java:
> 717)
> at
> org
> .apache
> .catalina
> .core
> .ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:
> 290)
> at
> org
> .apache
> .catalina
> .core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206)
> at
> com.clickbank.api.LoggingFilter.doFilter(LoggingFilter.java:39)
> at
> org
> .apache
> .catalina
> .core
> .ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:
> 235)
> at
> org
> .apache
> .catalina
> .core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206)
> at
> com.clickbank.api.APISecurityFilter.doFilter(APISecurityFilter.java:
> 91)
> at org.springframework.web.filter.DelegatingFilterProxy.invokeDelegate
> (DelegatingFilterProxy.java:236)
> at org.springframework.web.filter.DelegatingFilterProxy.doFilter
> (DelegatingFilterProxy.java:167)
> at
> org
> .apache
> .catalina
> .core
> .ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:
> 235)
> at
> org
> .apache
> .catalina
> .core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206)
> at org.springframework.security.util.FilterChainProxy
> $VirtualFilterChain.doFilter(FilterChainProxy.java:378)
> at org.springframework.security.intercept.web.FilterSecurityInterceptor.invoke
> (FilterSecurityInterceptor.java:109)
> at org.springframework.security.intercept.web.FilterSecurityInterceptor.doFilter
> (FilterSecurityInterceptor.java:83)
> at org.springframework.security.util.FilterChainProxy
> $VirtualFilterChain.doFilter(FilterChainProxy.java:390)
> at
> org
> .springframework
> .security
> .ui
> .SessionFixationProtectionFilter
> .doFilterHttp(SessionFixationProtectionFilter.java:52)
> at
> org
> .springframework
> .security.ui.SpringSecurityFilter.doFilter(SpringSecurityFilter.java:
> 53)
> at org.springframework.security.util.FilterChainProxy
> $VirtualFilterChain.doFilter(FilterChainProxy.java:390)
> at
> org
> .springframework
> .security
> .ui
> .ExceptionTranslationFilter
> .doFilterHttp(ExceptionTranslationFilter.java:101)
> at
> org
> .springframework
> .security.ui.SpringSecurityFilter.doFilter(SpringSecurityFilter.java:
> 53)
> at org.springframework.security.util.FilterChainProxy
> $VirtualFilterChain.doFilter(FilterChainProxy.java:390)
> at
> org
> .springframework
> .security
> .providers
> .anonymous
> .AnonymousProcessingFilter
> .doFilterHttp(AnonymousProcessingFilter.java:105)
> at
> org
> .springframework
> .security.ui.SpringSecurityFilter.doFilter(SpringSecurityFilter.java:
> 53)
> at org.springframework.security.util.FilterChainProxy
> $VirtualFilterChain.doFilter(FilterChainProxy.java:390)
> at
> org
> .springframework
> .security
> .ui
> .rememberme
> .RememberMeProcessingFilter
> .doFilterHttp(RememberMeProcessingFilter.java:109)
> at
> org
> .springframework
> .security.ui.SpringSecurityFilter.doFilter(SpringSecurityFilter.java:
> 53)
> at org.springframework.security.util.FilterChainProxy
> $VirtualFilterChain.doFilter(FilterChainProxy.java:390)
> at
> org
> .springframework
> .security
> .wrapper
> .SecurityContextHolderAwareRequestFilter
> .doFilterHttp(SecurityContextHolderAwareRequestFilter.java:91)
> at
> org
> .springframework
> .security.ui.SpringSecurityFilter.doFilter(SpringSecurityFilter.java:
> 53)
> at org.springframework.security.util.FilterChainProxy
> $VirtualFilterChain.doFilter(FilterChainProxy.java:390)
> at
> org
> .springframework
> .security
> .ui
> .basicauth
> .BasicProcessingFilter.doFilterHttp(BasicProcessingFilter.java:174)
> at
> org
> .springframework
> .security.ui.SpringSecurityFilter.doFilter(SpringSecurityFilter.java:
> 53)
> at org.springframework.security.util.FilterChainProxy
> $VirtualFilterChain.doFilter(FilterChainProxy.java:390)
> at
> org
> .springframework
> .security
> .ui
> .AbstractProcessingFilter.doFilterHttp(AbstractProcessingFilter.java:
> 277)
> at
> org
> .springframework
> .security.ui.SpringSecurityFilter.doFilter(SpringSecurityFilter.java:
> 53)
> at org.springframework.security.util.FilterChainProxy
> $VirtualFilterChain.doFilter(FilterChainProxy.java:390)
> at
> org
> .springframework
> .security.ui.logout.LogoutFilter.doFilterHttp(LogoutFilter.java:89)
> at
> org
> .springframework
> .security.ui.SpringSecurityFilter.doFilter(SpringSecurityFilter.java:
> 53)
> at org.springframework.security.util.FilterChainProxy
> $VirtualFilterChain.doFilter(FilterChainProxy.java:390)
> at
> org
> .springframework
> .security
> .context
> .HttpSessionContextIntegrationFilter
> .doFilterHttp(HttpSessionContextIntegrationFilter.java:235)
> at
> org
> .springframework
> .security.ui.SpringSecurityFilter.doFilter(SpringSecurityFilter.java:
> 53)
> at org.springframework.security.util.FilterChainProxy
> $VirtualFilterChain.doFilter(FilterChainProxy.java:390)
> at
> org
> .springframework
> .security.util.FilterChainProxy.doFilter(FilterChainProxy.java:175)
> at
> org
> .springframework
> .security.util.FilterToBeanProxy.doFilter(FilterToBeanProxy.java:99)
> 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:
> 233)
> at
> org
> .apache
> .catalina.core.StandardContextValve.invoke(StandardContextValve.java:
> 191)
> at
> org
> .apache
> .catalina.core.StandardHostValve.invoke(StandardHostValve.java:128)
> at
> org
> .apache
> .catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:102)
> at
> org
> .apache
> .catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:
> 109)
> at
> org
> .apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:
> 286)
> at
> org
> .apache.coyote.http11.Http11Processor.process(Http11Processor.java:
> 845)
> at org.apache.coyote.http11.Http11Protocol
> $Http11ConnectionHandler.process(Http11Protocol.java:583)
> at org.apache.tomcat.util.net.JIoEndpoint
> $Worker.run(JIoEndpoint.java:447)
> at java.lang.Thread.run(Thread.java:619)
>
> PRIVILEGED AND CONFIDENTIAL
> This transmission may contain privileged, proprietary or
> confidential information. If you are not the intended recipient, you
> are instructed not to review this transmission. If you are not the
> intended recipient, please notify the sender that you received this
> message and delete this transmission from your system.