users@jersey.java.net

[Jersey] Re: jersey 2.0 jaxrs RI - return json string on exception

From: rajeev jha <rjha94_at_gmail.com>
Date: Wed, 24 Jul 2013 17:07:44 +0530

Hi Marek

Attaching

# source files
# screenshot showing all the included jars in eclipse (jersey 2.0 lib + api
+ ext and jersey-media jars with jackson) Not sure if this is specific to
JDKHttp container.

Please let me know if you need more information.

/rajeev

The stack trace is
==============x============

Jul 24, 2013 5:01:49 PM org.glassfish.jersey.server.ApplicationHandler
initialize
INFO: Initiating Jersey application, version Jersey: 2.0 2013-05-14
20:07:34...
console v2.0 : Press Enter to stop the server.
Jul 24, 2013 5:02:08 PM org.glassfish.jersey.server.ServerRuntime$Responder
process
SEVERE: Error occurred when processing a response created from an already
mapped exception.
Jul 24, 2013 5:02:08 PM org.glassfish.jersey.server.ServerRuntime$Responder
release
WARNING: Attempt to release single request processing resources has failed.
org.glassfish.jersey.server.ContainerException: Error during writing out
the response headers.
    at
org.glassfish.jersey.jdkhttp.JdkHttpHandlerContainer$ResponseWriter.writeResponseStatusAndHeaders(JdkHttpHandlerContainer.java:268)
    at
org.glassfish.jersey.server.ServerRuntime$Responder$1.getOutputStream(ServerRuntime.java:461)
    at
org.glassfish.jersey.message.internal.CommittingOutputStream.commitStream(CommittingOutputStream.java:198)
    at
org.glassfish.jersey.message.internal.CommittingOutputStream.flushBuffer(CommittingOutputStream.java:303)
    at
org.glassfish.jersey.message.internal.CommittingOutputStream.commit(CommittingOutputStream.java:259)
    at
org.glassfish.jersey.message.internal.CommittingOutputStream.close(CommittingOutputStream.java:274)
    at
org.glassfish.jersey.message.internal.OutboundMessageContext.close(OutboundMessageContext.java:845)
    at
org.glassfish.jersey.server.ContainerResponse.close(ContainerResponse.java:411)
    at
org.glassfish.jersey.server.ServerRuntime$Responder.release(ServerRuntime.java:545)
    at
org.glassfish.jersey.server.ServerRuntime$Responder.process(ServerRuntime.java:365)
    at
org.glassfish.jersey.server.ServerRuntime$1.run(ServerRuntime.java:230)
    at org.glassfish.jersey.internal.Errors$1.call(Errors.java:271)
    at org.glassfish.jersey.internal.Errors$1.call(Errors.java:267)
    at org.glassfish.jersey.internal.Errors.process(Errors.java:315)
    at org.glassfish.jersey.internal.Errors.process(Errors.java:297)
    at org.glassfish.jersey.internal.Errors.process(Errors.java:267)
    at
org.glassfish.jersey.process.internal.RequestScope.runInScope(RequestScope.java:317)
    at
org.glassfish.jersey.server.ServerRuntime.process(ServerRuntime.java:198)
    at
org.glassfish.jersey.server.ApplicationHandler.handle(ApplicationHandler.java:946)
    at
org.glassfish.jersey.jdkhttp.JdkHttpHandlerContainer.handle(JdkHttpHandlerContainer.java:164)
    at com.sun.net.httpserver.Filter$Chain.doFilter(Filter.java:77)
    at sun.net.httpserver.AuthFilter.doFilter(AuthFilter.java:83)
    at com.sun.net.httpserver.Filter$Chain.doFilter(Filter.java:80)
    at
sun.net.httpserver.ServerImpl$Exchange$LinkHandler.handle(ServerImpl.java:668)
    at com.sun.net.httpserver.Filter$Chain.doFilter(Filter.java:77)
    at sun.net.httpserver.ServerImpl$Exchange.run(ServerImpl.java:640)
    at
java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1110)
    at
java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:603)
    at java.lang.Thread.run(Thread.java:722)
Caused by: java.io.IOException: headers already sent
    at
sun.net.httpserver.ExchangeImpl.sendResponseHeaders(ExchangeImpl.java:204)
    at
sun.net.httpserver.HttpExchangeImpl.sendResponseHeaders(HttpExchangeImpl.java:86)
    at
org.glassfish.jersey.jdkhttp.JdkHttpHandlerContainer$ResponseWriter.writeResponseStatusAndHeaders(JdkHttpHandlerContainer.java:264)
    ... 28 more

===========x================================










On Wed, Jul 24, 2013 at 4:38 PM, rajeev jha <rjha94_at_gmail.com> wrote:

> Hi Marek
> let me create a small project and zip it across. Only issue is that I am
> not using maven so you have to include the jars separately.
>
> thanks
>
> /rajeev
>
>
> On Tue, Jul 23, 2013 at 5:37 PM, Marek Potociar <marek.potociar_at_oracle.com
> > wrote:
>
>> Hi,
>> What is the stack trace you see? Do you have a working reproducible test
>> by a chance? I am not able to locate any occurrence of "Headers have
>> already been sent" message in Jersey 2.0 code.
>>
>> Marek
>>
>> On Jul 20, 2013, at 9:40 AM, rajeev jha <rjha94_at_gmail.com> wrote:
>>
>> Hi
>>
>> I have posted this on SO as well (
>> http://stackoverflow.com/questions/17747681/jersey-2-0-jaxrs-ri-return-json-string-on-exception
>> )
>>
>>
>> I am creating a REST service using jersey 2.0. I am extending
>> WebApplicationException
>>
>> 1)
>> **Method raising a particular exception**
>>
>> if(json.equals("") || json.equals(" ")) {
>> throw new ArgumentException("bad post data");
>> }
>>
>> 2)
>> public class ArgumentException extends RestException {
>> .....
>>
>> public ArgumentException(String message) {
>> super(Status.BAD_REQUEST,message);
>> }
>> }
>>
>> 3)
>> public class RestException extends WebApplicationException {
>> ...........
>> public RestException(Status status, String message) {
>>
>> super(Response.status(status)
>> .entity(message)
>> .type("text/plain")
>> .build());
>> /*
>> super(Response.status(status)
>> .entity(new
>> ErrorBean(status.getStatusCode(),message))
>> .type(MediaType.APPLICATION_JSON)
>> .build()); */
>>
>> }
>>
>> 4)
>>
>> ErrorBean is a POJO
>>
>>
>>
>> The method that returns error as plain string inside RestException works
>> (right http code 400 and message). However when I try to pass the ErrorBean
>> POJO and use MediaType.APPLICATION_JSON in response I get an error saying
>> "Headers have already been sent" with http error code 500 (so some internal
>> problem with plumbing) and empty response.
>>
>>
>> I have also looked at this question
>> http://stackoverflow.com/questions/2871935/returning-json-or-xml-for-exceptions-in-jersey
>>
>> How can I return the exception with code and message as a JSON like
>>
>> {"code" : 400, "message" : .... }
>>
>>
>> Thanks
>>
>> /rajeev
>>
>>
>>
>>
>




jersey_jars.png
(image/png attachment: jersey_jars.png)