2010/1/20 Paul Sandoz <Paul.Sandoz_at_sun.com>:
> Hi Adam,
>
> The problem is that Jersey selects the most acceptable media type from the
> @Produces given what the client sends in the Accept header.
>
> In this case i think you want to override what the client accepts and do
> something like the following:
>
> @GET
> @Path("{rrn: [0-9]{4}-[0-9]{4}-[0-9]{4}-[0-9]{4}-[0-9]{4}}/pdfs")
> @Produces("multipart/mixed")
> public MultiPart get() {
> if (/not error/) {
> return ...
> } else {
> Bean b = ...
> throw new WebApplicationException(Response.status(400).entity(b,
> "text/xml").build());
> }
> }
>
> I used a 400 to indicate client error but i am not sure what you mean by
> failure as it could equally be 500 server error.
>
> Note that you can also do:
>
> @GET
> @Path("{rrn: [0-9]{4}-[0-9]{4}-[0-9]{4}-[0-9]{4}-[0-9]{4}}/pdfs")
> @Produces("multipart/mixed")
> public Object get() {
> if (/not error/) {
> return ...
> } else {
> return Response.status(400).entity(b, "text/xml").build();
> }
> }
I was using the following code snippets -
@GET
@Path("{rrn: [0-9]{4}-[0-9]{4}-[0-9]{4}-[0-9]{4}-[0-9]{4}}/pdfs")
@Produces({"multipart/mixed", "text/xml"})
public Response getLodgementPdfsByRRN(
@PathParam("rrn") String rrn,
@DefaultValue("1") @QueryParam("languageCode") int languageCode
)
return Response.status(Response.Status.FORBIDDEN).entity(b).build();
I cant seem to find the two argument version of entity() that you
mention, do I actually need to use type() as well, like this -
return Response.status(400).entity(b).type("text/xml").build();
> Exceptions are useful because you can modify to use your own exception and
> an ExceptionMapper.
>
> Paul.
>
> On Jan 20, 2010, at 4:26 PM, Adam Retter wrote:
>
>> I have a GET Method using Jersey 1.1.4.1 which can return a mime
>> multipart response when successful, this all works fine :-)
>> But, I now want to extend it so that if there is a failure, it can
>> return an XML document detailing the problem.
>>
>> Previously I was using the following on my method -
>>
>> @GET
>> @Path("{rrn: [0-9]{4}-[0-9]{4}-[0-9]{4}-[0-9]{4}-[0-9]{4}}/pdfs")
>> @Produces("multipart/mixed")
>>
>> So, I changed that to -
>>
>> @GET
>> @Path("{rrn: [0-9]{4}-[0-9]{4}-[0-9]{4}-[0-9]{4}-[0-9]{4}}/pdfs")
>> @Produces({"multipart/mixed", "text/xml"})
>>
>> It still works fine for returning a mime multipart response when the
>> request is successful, but when the request fails and I try and return
>> my XML Bean generated by JAXB -
>>
>> @XmlRootElement(name = "response")
>> public class Response {
>> ...
>> }
>>
>> Then I get the following stack trace, which seems to imply to me that
>> the "text/xml" argument to @Produces is being ignored? -
>>
>>
>> 20-Jan-2010 16:21:12 com.sun.jersey.spi.container.ContainerResponse write
>> SEVERE: A message body writer for Java type, class
>> uk.co.landmark.solutions.registers.rest.Response, and MIME media type,
>> multipart/mixed, was not found
>> 20-Jan-2010 16:21:12
>> 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:253)
>> at
>> com.sun.jersey.server.impl.application.WebApplicationImpl._handleRequest(WebApplicationImpl.java:814)
>> at
>> com.sun.jersey.server.impl.application.WebApplicationImpl.handleRequest(WebApplicationImpl.java:740)
>> at
>> com.sun.jersey.server.impl.application.WebApplicationImpl.handleRequest(WebApplicationImpl.java:731)
>> at
>> com.sun.jersey.spi.container.servlet.WebComponent.service(WebComponent.java:372)
>> at
>> com.sun.jersey.spi.container.servlet.ServletContainer.service(ServletContainer.java:452)
>> at
>> com.sun.jersey.spi.container.servlet.ServletContainer.service(ServletContainer.java:633)
>> at javax.servlet.http.HttpServlet.service(HttpServlet.java:803)
>> at
>> org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:290)
>> at
>> org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206)
>> at
>> uk.co.landmarkinfo.registers.common.filter.UrlSecurityFilter.handleTermsPage(UrlSecurityFilter.java:306)
>> at
>> uk.co.landmarkinfo.registers.common.filter.UrlSecurityFilter.doFilter(UrlSecurityFilter.java:194)
>> at
>> org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:235)
>> at
>> org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206)
>> at
>> uk.co.landmarkinfo.registers.common.filter.TextSizeFilter.doFilter(TextSizeFilter.java:73)
>> at
>> org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:235)
>> at
>> org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206)
>> at
>> org.springframework.web.filter.CharacterEncodingFilter.doFilterInternal(CharacterEncodingFilter.java:96)
>> at
>> org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:76)
>> 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:175)
>> 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:263)
>> at
>> org.apache.jk.server.JkCoyoteHandler.invoke(JkCoyoteHandler.java:190)
>> at
>> org.apache.jk.common.HandlerRequest.invoke(HandlerRequest.java:283)
>> at
>> org.apache.jk.common.ChannelSocket.invoke(ChannelSocket.java:767)
>> at
>> org.apache.jk.common.ChannelSocket.processConnection(ChannelSocket.java:697)
>> at
>> org.apache.jk.common.ChannelSocket$SocketConnection.runIt(ChannelSocket.java:889)
>> at
>> org.apache.tomcat.util.threads.ThreadPool$ControlRunnable.run(ThreadPool.java:690)
>> at java.lang.Thread.run(Thread.java:619)
>>
>>
>> --
>> Adam Retter
>>
>> skype :adam.retter
>> http://www.adamretter.org.uk
>>
>> ---------------------------------------------------------------------
>> To unsubscribe, e-mail: dev-unsubscribe_at_jersey.dev.java.net
>> For additional commands, e-mail: dev-help_at_jersey.dev.java.net
>>
>
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: dev-unsubscribe_at_jersey.dev.java.net
> For additional commands, e-mail: dev-help_at_jersey.dev.java.net
>
>
--
Adam Retter
skype :adam.retter
http://www.adamretter.org.uk