Serve image/file in json - java

assume that I have class like this
public class Entity {
private String name;
private File file;
// Getters & setters
}
is it possible to return object of this class as json? I'm using jax-rs

Yes, you can.
If you're using JAX-RS, then I expect you to know the basic of creating a REST-ful Web Services ya.
You can use #Produces("application/json") annotation in one of your endpoint. Use that endpoint to return an Entity, and it will be automatically converted as JSON.

Related

How to use jackson mixin on Jersey REST services method parameters

I had Jackson deserializing problem of one of my java class which is come from third party API that doesn't have default constructors.To overcome that issue i used JacksonMixIn and it worked fine.But the problem was that i have a REST endpoint implemented on Jersey API which is accepted one of above mentioned classes as a method parameter from client side to server side.So when deserializing it throws me following error.
No suitable constructor found for type [simple type, class net.rcarz.jiraclient.Priority]: can not instantiate from JSON object (need to add/enable type information?)
at [Source: org.glassfish.jersey.message.internal.EntityInputStream#558e8ae; line: 1, column: 454]...
Affected classes
public class TestCaseVO{
private Priority priority;
private User reporter;
}
public class Priority {
protected Priority(RestClient restclient, JSONObject json) {
super(restclient);
if (json != null)
deserialise(json);
}
}
This is the object used to communicate client to server
public class myDataObject{
private String userName;
private List<TestCaseVO> testCases;
//Getter and setters
}
Jersey Endpoint
#POST
#Path("/bug")
#Consumes(MediaType.APPLICATION_JSON)
#Produces(MediaType.APPLICATION_JSON)
public TestCaseVO attachBugForTestCase(myDataObject myDataObject){
// when deserializing to MyDataObject it thorows above error
//Handle logic
}
Client side code snippet
var myDataObject= {
"testCases": [$scope.bug.TestCaseVO],
"userName":userName}
angularJsMyService.Bug.attachBug({},myDataObject)
.$promise.then(function(data){
...
}
My question is that how can i use Jackson mixin on REST methods parameters prior to Jackson deserializing them.Appreciate any help.
I guess you probably didn’t integrate Jackson with Jersey in the right way. Check out Jersey’s doc on Jackson. In your project, there should be a class implementing ContextResolver<ObjectMapper>. The ObjectMapper instance returned by getContext(Class<?> type) in this class is used by Jersey’s REST endpoints. You may to configure that ObjectMapper with your Mix-in.

How to JSON parse immutable object in Java + Jersey

So I am just trying out Jersey for REST services and it seems to we working out fine. I only expose get services and all of the object types that I expose with these services have an immutable object representation in Java. By default Jersey seems to use a parser (JAXB?), requiring a #XmlRootElement annotation for the class that should be parsed, zero-arg constructor and setters.
I have been using Gson with no zero-arg constructor, no setters and final on all fields with no problems at all. Is there any way to accomplish this with Jersey(i.e. the paser it is using)? I have seen solutions with adapter classes that map data from a immutable object to a mutable representation, but this seems like a lot of boilerplate(new classes, more annotations, etc.) if it can be achieved with Gson without anything added.
Note: 1) I have heard people promote using zero-arg constructor and claim that Gson should not work without it. This is not what I am interested in. 2) I really have tried googling this but my keywords might be off. In other words, humiliate me in moderation.
EDIT 1:
My webservice works if I do like this:
#XmlRootElement
public class Code{
private String code; //Silly object just used for example.
public Code(){}
//(G || S)etters
}
With this class exposing the object:
#GET
#Produces(MediaType.APPLICATION_JSON)
public Set<Code> get(#QueryParam("name") String name) { // Here I want to use a class of my own instead of String name, haven't figured out how yet.
return this.codeService.get(name);
}
If I replace the Code with the following, the webservice stops working:
public class Code{
private final String code;
#JsonCreator
public Code(#JsonProperty("code") String code) {
this.code = code;
}
//Getters omitted
}
What I want is to be able to 1) have immutable objects that can be parsed to/from json and 2) Be able to define something like #RequestBody in Spring MVC for my incoming objects.
Actually this could be pretty easy with Genson. You just need the jar and then configure the Genson feature to use constructors with arguments (if you don't want to put annotations on it).
Genson genson = new GensonBuilder().useConstructorWithArguments(true).create();
// and then register it with jersey
new ResourceConfig().register(new GensonJaxRSFeature().use(genson));
Or you can use JsonProperty on the arguments. See the User Guide for more details.

Accepting XML on server side using RESTEasy

I want to create a REFTful web service that accepts XML. I can see a lot of examples where server is sending response as an XML. But I want to process request as an XML.
Anyone has idea on how to do that?
You can use JAXB to map XML to Java classes.
#PUT
#Accepts("application/xml")
public Response putThing(Thing theThing) {
// save theThing
URI theThingUri = new URI(...);
return Response.created(theThingUri).build();
}
#XmlRootElement
public class Thing {
// members, constructors, getter, setter
}
See the RESTEasy documentation.

How to retrieve the values from the service which is returning List<Object> using jax-ws

I have a service which returns the List, this object varies depends on different scenario.
Can somebody suggest me does jax-ws support this behaviour or do we have any alternative option.
Since that JAX-WS use JAXB for serializate the objects, JAXB need to know the name of the type for marshall or unmarshall. In a standalone environment can deal with this kind of things. However, when dealing with a list of objects, this becomes more complicated.
Moreover, each data type must be defined in the WSDL. The service client must be able to convert the response XML to the data type desired.
If you wish to return different lists of different type, the simplest is to use a wrapper for the response. e.g.
public class ResponseWrapper {
private List<Audio> audios;
private List<Video> videos;
// setters and getters
}
#WebService
public class MediaStore {
#Inject
AudioService audioService;
#Inject
VideoService videoService;
#WebMethod
public ResponseWrapper getCollections(String artistId) {
ResponseWrapper response = new ResponseWrapper();
response.setAudios(audioService.getAudios(artistId));
response.setAudios(videoService.getVideos(artistId));
return response;
}
}
Another way would be to work directly with SOAP messages, but you could avoid doing so.

Send java object to a rest WebService

I'm searching a nice way to send a java object to my rest web service.
It's possible or not ?
For sample I wan't to send an "User" Object to my rest :
public Class User{
private String name;
private String surname;
public getName(){
return name;
}
public setName(String name){
[...]
}
It's possible to generate AUTOMATICALY this kind of Rest ?
www.foo.com/createUser/name="foo"&surname="foo"
I would consider using a JSON representation for this kind of Java objects.
I prefer the Jersey implementation of JAX-RS and it has built-in support for JSON serialization over JAXB.
Hope this helps...
Have a look at Restlet. The tutorial shows you how to get started.
Restlet allows you to use a number of representation formats, including XML and JSON.
It's possible to generate AUTOMATICALY this kind of Rest ?
www.foo.com/createUser/name="foo"&surname="foo"
That's NOT REST. That's RPC.

Categories