We have a Resteasy webservice.
I use Jackson provider for JSON, both outgoing JSON in response and incoming JSON in request.
Is it possible to have a PostProcessInterceptor to be executed after JSON-Jackson serialization?
My PostProcessInterceptor has to change the JSON content for every outgoing response. But when the PostProcessInterceptor is executed if I print the entity response.getEntity().toString(); I see the toString method of the java.lang.Object, not the JSON String. That's because the Object has not yet been serialized by Jackson.
Is it possible to serialize with Resteasy/Jackson before running the PostProcessInterceptor?
I've also tried to use #Precedence annotation on my PostProcessInterceptor. But it doesn't work, even using "DECODER" precedence (which is the last one).
Any idea? Thanks in advance.
I would go for a CDI interceptor instead. You can get the intercepted method parameters from the InvocationContext and change them if necessary.
Related
Can someone please help clarify and show how to properly go about this issue?
What I understand so far is that the Spring framework uses Message Converters when a method is annotated with #ResponseBody to convert the return Java object to a format that can be accepted by the Client. If the clients' HTTP request Accept Header includes "application/json", it will use Jackson and the Jackson converter to convert the object and return it in a json format. Similarly if the Accept Header includes "application/xml", then the Message Converter will use Jaxb and the corresponding converter to convert the object to xml.
Now my issue is that I include both the Jackson and Jaxb libraries as specified in Spring documentation so that the corresponding converters can work. This should be enough for Spring to employ #ResponseBody as its supposed to. However, when I send an HTTP Request with the Accept header "application/xml" I get a 406 status code and when I send one with "application/json" I receive a correct json response.
From my research online, I see that some people use the ContentNegotiation technique to work around this, but I would like to use the Message Converter for now. However, every technique to make the Message Converter technique to respond to json and xml resource requests involve formatting my POJO with JAXB annotation. Is this really necessary?
I guess what I am asking is how would one set up their project properly so that Spring can use the Message Converter technique to respond to json and xml requests? What libraries must be included? Does one need to add JAXB annotations or is there an automatic way for Spring to format an object into xml the way it does for json?
I thank you for your time and help with this, but so far I am really loving Spring's implementation of JAX-RS!
I am trying to do something like this
Response response = webclient.post(SomeReqString);
This response is from javax.ws.rs.Response from jersey 1.18
How should I get the List<customobject> from above response?
There is no readEntity method in jersey 1.x and also there is no generic type class in jersey 1.x.
Due to some restriction, I cannot use webresource and Clientresponse.
The only useful method I found is response.getEntity() (it does not accept any parameter)
How should I use this to get the List?
Generally, response.readEntity(String.class) is a safe way to get the response-body as a string. You can then use another library to deserialize this string back into a list, depending on how it's formatted.
I am creating a Web Service that will handle incoming request properly (I knew, it is a description) I found some sample code which present SOAP WS and looks like:
#SoapAction("some fine url")
#ResponsePayload
public CertResponse getCert(#RequestPayload Cert param)
{...}
It takes Cert object from request and manages it properly. I want to do it in the REST approach and I changed this class to look like:
#RequestMapping(value="getCert", method = RequestMethod.POST)
#ResponseBody
public CertResponse getCert(#RequestBody Cert param)
{...}
But I have no idea how or even if it is possible to write a client that can send object through HTTP Post.
Can anyone give me a hint how can i send object Cert? Or if i have to stay with #SoapAction what should I do to make it work? I guess removing #Controller is not enough.
In SOAP approach, there is a well defined way to convert each class object to SOAP formatted XML. Thus, there is no effort.
If you will use RESTful approach, you have to describe how your Cert or CertResponse objects will be written to/read from the response/request.
Basically you have three options:
Use JSON or XML or plain String. Convert your Web service descriptor so that the request and response are one of those (JSON, XML, String). Then your getCert method should convert the request to Cert object, prepare your response as CertResponse object and convert it to an appropriate response type.
Leave your getCert method as is. But you need to specify "how Cert objects are read from request" and "how CertResponse objects are written to response" You need to define classes that extend interfaces MessageBodyReader and MessageBodyWriter. In these classes a similar conversion logic should be implemented that converts your objects to/from JSON or XML or String.
In any of these two options you need to implement two things : A method that converts JSON/XML/String to Cert, a method that converts CertResponse object to JSON/XML/String.
You can do a text based implementation, which parses/constructs the request/response by text processing and uses String class only. Or you can use some library such as JSON library with JSONObject class, or Java SAX or DOM based XML libraries that come with the Java bundle.
Check out for application frameworks such as Spring. They might provide ways to automatically convert your objects to JSON or XML, reducing the programming effort necessary.
Spring allows you to pass JSON objects from the client as a request parameters, it will convert them into your objects automatically. This discussion have some examples.
SOAP services are not really compatible with REST semantics. And it's not clear what SOAP framework do you use. Typically most SOAP frameworks offer you one way or another to generate a SOAP client code for you WSDL. You can check cxf.apache.org.
For REST services use something like Jersey or Spring MVC
I need to write a Json client in Android for Zenfolio API. I decided to use Spring ResTemplate with MappingHttpJacksonConverter. When i do POST with "exchange" method i recieve json response with one element named "#type" that causes deserializatoon exception. Is there an annotation that tells deserializer to omit that tag? How to turn on annotation for json deserializer?
Try #JsonIgnoreProperties(ignoreUnknown=true) on your mapping classes if you want to ignore all elements that you are not interested in.
Try using #JsonIgnore ("#type"), Jackson annotations are enabled by default.
See http://wiki.fasterxml.com/JacksonAnnotations for more info.
I have written a custom interceptor that does some parameter validation. I want to be able to return an error code and serialize a JAXB-annotated class as the response body.
If I throw a WebApplicationException, it doesn't have any special processing done to serialize the Response object inside (which makes sense; I assume that is done by another interceptor).
How should I go about stopping the interceptor chain but still have JAXB serialize the response entity?
Well, at least in the CXF JAX-RS interceptor flow, if you set:
message.getExchange().put(Response.class, response);
...then the actual service does not get invoked, while the other phases do get invoked. Haven't dug in to the CXF code to see where that logic kicks in.
So I built a response like this:
Response response = Response
.status(Response.Status.FORBIDDEN)
.entity(new ErrorEntity("This is a JAXB object with an error string"))
.build();
I also have some custom authentication running in a CXF JAX-RS filter and I only want to check the parameters when the authentication is alright, so I set my parameter interceptor class to run during the PRE_INVOKE phase.