How to handle/create new content-type/MediaType in JAX-RS? - rest

I am researching on Jersey and RESTEasy. Media-type negotiation for XML and JSON works fine, and I am able to consume and produce both of them. However, I am being asked to produce and consume a response for a new content-type. For instance, BSON, or a self customized content-type. I googled online but could not find much information in it. Is there anyway, I could still use the #Produces and #Consumes annotation in JAX-RS for the new content-type?
Thanks in advance.

Yes, you can use #Produces and #Consumes with custom media types. In order to use the custom media type when marshalling and unmarshalling content you need to create MessageBodyWriter and MessageBodyReader implementations to handle the media type.
Here is how to implement a custom media type:
Annotate your resource methods with #Consumes({"application/mycustomtype}) and #Produces({"application/mycustomtype}) as required.
Implement custom MessageBodyReader and MessageBodyWriter implementations to support your custom media type.
Annotate your MessageBodyReader with #Provider and #Consumes({"application/mycustomtype})
Annotate your MessageBodyWriter with #Provider and
#Produces({"application/mycustomtype})

Related

How to create sub resource in restlet

How to create a sub resource in restlet
1- I want to have two #Get annotated method in same resource
2- I want to assign each method a URI
In fact, this can only be done using the JAX-RS support of Restlet. For the native REST support (classes that extend ServerResource) they can be attached to a single path. I mean every annotated methods in the server resource will be called on the same path.
That said you can define several #Get methods in a same server resource but the routing will be done based on media types or query parameters.
Hope it answers your question.
Thierry

gwt serialize and de-serialize object from and to a string (client side only)

I have java objects used in GWT RPC calls. On the GWT client I need to store these objects in a web sql database. I need to be able to convert these objects to a string and then also de-serialize them from the string. I have seen many aproaches JSON, AutoBean, various other libraries but none seem to be able to handle circular references which I do have in my objects.
Anyone know of a GWT library that can handle my requirements?
GWT RPC itself transmit responses to the client using JSON and handles circular references okay (at least I think so) - So I cannot see why this mechanism cannot be re-used. Any ideas?
If the Web SQL service conforms to JAX-RS or Jackson JSON, use RestyGWT.
There is no conversion necessary on your part. RestyGWT encodes your POJOs to Jackson-compliant JSON.
The flip-side is you would have to read up on JAX-RS, JAXB, Jackson annotations. Which is not a flip-side to me but an exciting opportunity to learn a widely used web service technology.
GWT RPC itself transmit responses to the client using JSON
that is an inaccurate perception.
GWT RPC object encoding is deliberately obfuscated and difficult to decipher and format stability is not guaranteed from version to version.
there is no json in the RPC data interchange.
If your statement were true, then you would not have needed a servlet that extends GWT RemoteServiceServlet to service a RPC request.
BTW, what web SQL service is that? Proprietary in-house?

How can I access SecurityContext programmatically but not through annotation in CXF JAX-RS?

I'm trying to access the javax.ws.rs.core.SecurityContext in my class programmatically and not using #Context annotations, is a way to do it?
e.g. Spring provide implementation like SecurityContextHolder.getContext() that get the object from the ThreadLocal; is there similar implementation available in CXF JAX-RS?

Use pojo as gwt RequestFactory proxy instead of interface

Is there any easy way to use a pojo as a request factory proxy and not an interface? The case is that I would like to reuse the actual value object as is without creating an interface describing it.
I do not that this can not be done out of the box. GWT fails to compile with an error regarding non getter/setter methods insite the "proxy" class.
This not possible, by design. See this previous StackOverflow answer.

GWT and Jetty - HTTP method GET is not supported by this URL

I'm starting a servlet extending google's RemoteServiceServlet in Jetty. It did not work with this error:
HTTP method GET is not supported by this URL
I searched this error and found that RemoteServiceServlet does not implement doGet and doPost.
I tried this link http://docs.codehaus.org/display/JETTY/GWT. It didn't work either.
Any ideas how I can get it to work?
Should I send back the html file in the doGet?What should I do in the doPost?
Thank you
RemoteServiceServlet is designed to be used with GWTRPC. This means, that you don't implement doPost or doGet at all (doPost is even final in AbstractRemoteServiceServlet). You implement your own service methods instead, which you specify in your RemoteService interface.
If you want to implement doPost/doGet yourself, instead of using GWTRPC (see "Making HTTP Requests" in the GWT documentation), then extend the usual HttpServlet instead of RemoteServiceServlet. If you need both, I would suggest to create two separate servlets.
Removing super.doGet(req, resp) in my doGet method helped me. Anyway there is not much usefull operations in it.