Hi
first, apologies. I should have read through all the available
documentation. Now, I have read the docs and gone through the example on
github (
https://github.com/jersey/jersey/tree/2.1/examples/json-jackson).
However, the problem remains. Details are
#1 I am trying to use a non JAXB bean so I am not putting any annotations
in the POJO. it is a plain simple POJO with getters and setters.
#2 The service is annotated like
------------------------------------
@Path("/apiv1")
public class Service extends Application {
@GET
@Path("/test")
@Consumes(MediaType.APPLICATION_JSON)
@JSONP
@Produces({"application/javascript", MediaType.APPLICATION_JSON})
public String testService() {
throw new RestException("json error in service") ;
}
}
----------------------------
#3. After looking at the provided example, I thought just registering
JacksonFetaure should be enough. (I am not doing any custom mapping)
so I tried A) with JDKHttpServer
=============================
ResourceConfig rc = new ResourceConfig()
.packages("test")
.register(test.ErrorBean.class)
.register(JacksonFeature.class);
URI endpoint = new URI(BASE_URI);
server = JdkHttpServerFactory.createHttpServer(endpoint,rc);
==============================
B) when (A) did not work, I tried the grizzly sample (ditto the github
sample)
=================================
public class MyApplication extends ResourceConfig{
public MyApplication() {
super(
ErrorBean.class,
JacksonFeature.class
);
}
}
==============================
#4) I have looked at the dependencies to see if I am missing any jars etc.
Right now I include
jersey-media-json-jackson
4 jackson jars
#5 The stack trace I get is following
========x==========================
JSON with Jackson Jersey Example App
Jul 26, 2013 12:55:21 PM org.glassfish.jersey.server.ApplicationHandler
initialize
INFO: Initiating Jersey application, version Jersey: 2.0 2013-05-14
20:07:34...
Exception in thread "main" java.lang.AbstractMethodError:
org.glassfish.jersey.jackson.JacksonFeature.configure(Ljavax/ws/rs/core/FeatureContext;)Z
at
org.glassfish.jersey.model.internal.CommonConfig.configureFeatures(CommonConfig.java:617)
at
org.glassfish.jersey.model.internal.CommonConfig.configureMetaProviders(CommonConfig.java:558)
at
org.glassfish.jersey.server.ResourceConfig.configureMetaProviders(ResourceConfig.java:768)
at
org.glassfish.jersey.server.ApplicationHandler.initialize(ApplicationHandler.java:341)
at
org.glassfish.jersey.server.ApplicationHandler.access$500(ApplicationHandler.java:157)
at
org.glassfish.jersey.server.ApplicationHandler$3.run(ApplicationHandler.java:280)
at org.glassfish.jersey.internal.Errors$2.call(Errors.java:289)
at org.glassfish.jersey.internal.Errors$2.call(Errors.java:286)
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.processWithException(Errors.java:286)
at
org.glassfish.jersey.server.ApplicationHandler.<init>(ApplicationHandler.java:277)
at
org.glassfish.jersey.server.ContainerFactory.createContainer(ContainerFactory.java:79)
at
org.glassfish.jersey.grizzly2.httpserver.GrizzlyHttpServerFactory.createHttpServer(GrizzlyHttpServerFactory.java:110)
at test.GrizzlyServer.main(GrizzlyServer.java:18)
=============x========================
Help appreciated
Thanks
/rajeev
On Thu, Jul 25, 2013 at 5:29 PM, Michal Gajdos <michal.gajdos_at_oracle.com>wrote:
> Hi Rajeev,
>
> thanks for the example. In order to use Jackson in your app you need to
> register JacksonFeature in your app, see [1] and [2]. After this step your
> app should work.
>
> [1] https://jersey.java.net/documentation/latest/user-guide.html#d0e5281
> [2]
> https://jersey.java.net/documentation/latest/user-guide.html#json.jackson
>
> Michal
>
>
> On 24.07.2013 13:37 , rajeev jha wrote:
>
> 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
>>>
>>>
>>>
>>>
>>
>
>