Jersey+jackson - how to serialize generic list - json

I have a problem with converting my lists to JSON. I'd like to use generic lists with templating.
This doesn't work for me. My POJO:
class Event {
private Long id;
public void setId(Long id) {
this.id = id;
}
public Long getId() {
return id;
}
}
Generic list result:
public class ListResponse<T> {
private List<T> result = new ArrayList<T>();
public List<T> getResult() {
return result;
}
public void setResult(List<T> result) {
this.result = result;
}
}
Concrete list result:
public class EventListResponse extends ListResponse<Event> {}
In my resource class I'm using this:
#PermitAll
#Path("/list")
#POST
#Consumes(MediaType.APPLICATION_JSON)
#Produces(MediaType.APPLICATION_JSON)
public EventListResponse getEvents() {
//...
EventListResponse response = new EventListResponse();
List<Event> events = eventService.getList();
response.setResult(events);
return response;
}
The serialize doesnt work in this case, I end up with Strings insead of "Event" JSON objects.
I get results like:
array(3) { ["type"]=> string(17) "eventListResponse" ["result"]=> array(1) { [0]=> string(38) "com.mypackage.model.Event#3a622482" } }
Whats the problem here?
Thank you

Related

#RestController custom ResponseEntity - Jackson

What i'm trying to achieve here is to get a custom response from the RequestMapping, below is the structure of the json which I'm trying to get in case of an array of objects:
{
"error": false,
"message": "the message",
"data": [{},{},...]
}
and the below in case of object
{
"error": false,
"message": "the message",
"data": {}
}
The code is working fine but the problem is "data" will not always has an array, it may store an object, so what I tried is to create a custom POJO class which contains my custom response and when I want to annotate two attributes with same name i'm getting the below error
Could not find acceptable representation
And what if I create another class which will contain the same attributes but with an JsonObject not with array, is there any better way to achieve this ?
Below are my classes :
#JsonInclude(Include.NON_NULL)
public class JsonResponseObject<T> implements java.io.Serializable {
private static final long serialVersionUID = 1L;
private boolean error;
private String message ;
#JsonProperty(value="data")
private ArrayList<T> array;
#JsonProperty(value="data")
private Object object ;
public JsonResponseObject() {
}
public boolean isError() {
return error;
}
public void setError(boolean error) {
this.error = error;
}
public String getMessage() {
return message;
}
public void setMessage(String message) {
this.message = message;
}
public ArrayList<T> getArray() {
return array;
}
public void setArray(ArrayList<T> array) {
this.array = array;
}
public Object getObject() {
return object;
}
public void setObject(Object object) {
this.object = object;
}
}
UserJsonController.java :
#RestController()
#RequestMapping(value = "/json")
public class UserJsonController {
#Autowired
private UserRepository userDAO;
#RequestMapping(value = "/users", method = RequestMethod.GET, produces = MediaType.APPLICATION_JSON_VALUE)
public ResponseEntity<Object> getUsers() {
ArrayList<Users> entityList = (ArrayList<Users>) userDAO.findAll();
JsonResponseObject<Users> jsonResponse = new JsonResponseObject<Users>();
jsonResponse.setError(false);
jsonResponse.setMessage("test");
jsonResponse.setArray(entityList);
return new ResponseEntity<>(jsonResponse, HttpStatus.OK);
}
#RequestMapping(value = "/users/{id}", method = RequestMethod.GET, produces = MediaType.APPLICATION_JSON_VALUE)
public ResponseEntity<Object> getUserByID(#PathVariable int id) {
JsonResponseObject<Users> jsonResponse = new JsonResponseObject<Users>();
jsonResponse.setError(false);
jsonResponse.setMessage("test");
jsonResponse.setObject(userDAO.findById(id).get());
return new ResponseEntity<>(jsonResponse, HttpStatus.OK);
}}

Add JSON message in RestCotroller

I want to add JSON message on REST controller class(methods). For example i have delete method look's like:
#DeleteMapping("/people/{id}")
public ResponseEntity<PersonDto> deletePerson(#PathVariable Long id) {
return personService
.deletePerson(id)
.map(ResponseEntity::ok)
.orElse(ResponseEntity.notFound().build());
}
I want to return message Person (maybe numer of id) deleted.
Should i use ExceptionHandler? Or Can i do this Using exceptionHnadler?
Define a ResponseBean POJO class as
public class ResponseBean {
public final Long id;
public final String msg;
public ResponseBean(Long id, String msg) {
this.id = id;
this.msg = msg;
}
public Long getId() {
return id;
}
public String getMsg() {
return msg;
}
}
Change your controller method as
#DeleteMapping("/people/{id}")
public ResponseEntity<ResponseBean> deletePerson(#PathVariable Long id) {
return personService
.deletePerson(id)
.map(dto -> new ResponseBean(dto.getId(), "Person Deleted Successfully"))
.map(ResponseEntity::ok)
.orElse(ResponseEntity.notFound().build());
}
Response
{
"id" : 2,
"msg" : "Person Deleted Successfully"
}
You can customize ResponseBean class as per your need.

Jackson in Spring: how to unmarshal a "generic" class w/o it thinking it's a LinkedHashMap?

So my entities look like this:
public class HappyClass<T>
{
private String id;
prviate int ver;
private Object obj;
public String getId()
{
return this.id;
}
public void setId( String id )
{
this.id = id;
}
public int getVer()
{
return this.ver;
}
public void setVer( int ver )
{
this.ver = ver;
}
#JsonTypeInfo( use = Id.NONE )
public T getObj()
{
return obj;
}
public void setObj( T obj )
{
this.obj = obj;
}
}
public class HappyGeneric
{
private String someStuff();
public String getSomeStuff()
{
return this.someStuff();
}
public void setSomeStuff( String someStuff )
{
this.someStuff = someStuff;
}
}
If I instantiate a class like this:
HappyClass<HappyGeneric> hc = new HappyClass<HappyGeneric>();
If I send it to Spring in a #ResponseBody it returns this:
{
"id" : "iamsomeid",
"ver" : 123,
"obj" : {
"someStuff" : "iamsomestuff"
}
}
However, when Spring and/or Jackson attempts to unmarshal the same JSON, it figures out that the main class is a HappyClass, however, the getObj() it unmarshals to a LinkedHashMap and not a HappyGeneric no matter what I seem to annotate it with.
Anybody have any ideas how I can force Jackson to unmarshal that generic to the original class?
Thanks!
EDIT: I'm aware I can call mapper.convertValue( blah.getObj(), HappyGeneric.class ) and get the object out that way-- I was hoping to get Spring to figure it out automatically (through annotations, for example).

Jersey unmarshal JSON: Last element null does not work

I am using Jersey to parse the following JSON:
{"response":{"status":"OK","campaigns":[{"id":12345,"state":"active","code":null}]}}
But I get the following error message:
java.lang.IllegalArgumentException: No more parsing elements.
If I switch the position of the fields code and state so that the resulting JSON looks like
{"response":{"status":"OK","campaigns":[{"id":12345,"code":null,"state":"active"}]}}
everything works fine. Also if I change the code-field in the first JSON to a non-null value like "code":"test", Jersey can parse this without any problems. I tried other more complex examples always getting the above mentioned error message when leaving the last field of any element of an array null.
I think I am doing something wrong, because I could not find any others having the similar problem. I already tried to implement a CustomJAXBContextResolver using other JSON notations like natural but nothing worked for me.
Any ideas?
Here are my binding classes:
#XmlRootElement
public class LoadEntityResponse {
public LoadEntityResponse() {
}
private Response response;
public Response getResponse() {
return response;
}
public void setResponse(Response response) {
this.response = response;
}
}
and
public class Response {
public Response() {
}
private String status;
private String error;
private String error_id;
private Campaign[] campaigns;
public String getStatus() {
return status;
}
public void setStatus(String status) {
this.status = status;
}
public String getError() {
return error;
}
public void setError(String error) {
this.error = error;
}
public String getError_id() {
return error_id;
}
public void setError_id(String error_id) {
this.error_id = error_id;
}
public Campaign[] getCampaigns() {
return campaigns;
}
public void setCampaigns(Campaign[] campaigns) {
this.campaigns = campaigns;
}
}
and finally
public class Campaign{
public Campaign() {
}
protected int id;
protected String code;
protected String state;
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getCode() {
return code;
}
public void setCode(String code) {
this.code = code;
}
public String getState() {
return state;
}
public void setState(String state) {
this.state = state;
}
}
Solved: Using JacksonJsonProvider now:
...
DefaultClientConfig config = new DefaultClientConfig();
config.getClasses().add(JacksonJsonProvider.class);
...
that´s all!
You can also use Jackson POJO support that comes with jersey-json but there is a need to do some configuration, see POJO support in Jersey User Guide.
Try using Genson http://code.google.com/p/genson/.
To enable it on client side use the following code:
ClientConfig config = new DefaultClientConfig();
config.getClasses().add(GensonJsonConverter.class);
cli = Client.create(config);
EDIT: on server side there is no configuration needed, when the jar is in your classpath json support is automatically enabled.

Remove type-information from JSON result

Currently I am trying to create a webservice which simply returns a list;
#Path("/random")
#Singleton
public class Random
{
#GET
#Path("/")
#Produces(MediaType.APPLICATION_JSON)
public MyResult<String> test()
{
MyResult<String> test = new MyResult<String>();
test.add("Awesome");
return test;
}
}
And my MyResult class looks like this:
#XmlRootElement
public class MyResult<T> implements Iterable<T>
{
private ArrayList<T> _items;
private int _total;
public MyResult()
{
_items = new ArrayList<T>();
}
public ArrayList<T> getItems()
{
return _items;
}
public void setItems(ArrayList<T> items)
{
_items = items;
}
public int getTotal()
{
return _total;
}
public void setTotal(int total)
{
_total = total;
}
public void add(T item)
{
getItems().add(item);
}
public Iterator<T> iterator()
{
return getItems().iterator();
}
}
Now I get the following result from the service:
{"items":[{"#type":"xs:string","$":"Awesome"}],"total":"0"}
But I don't want any of this information, I just require this:
{"items":["Awesome"],"total":"0"}
It seems to me this requires some configuration somewhere, who know how to get the required result?
Assuming you are using jackson, take a look at #JsonTypeInfo annotation. It is used for configuring details of if and how type information is used with JSON serialization and deserialization. The use and behaviour of it would depend on the version of jackson you are using.
To completely suppress type information, I had to use the following annotations:
#JsonTypeInfo(use=JsonTypeInfo.Id.NONE)
#JsonDeserialize(as=NoType.class)