Json List to String List : Spring mvc Controller - json

I want to send this List example :
{"id":[1]}
To this controller :
public String addUsersToProject(#RequestBody List<String> usersIds, #PathVariable String projectTitle){..}
But I can't read the list :
Could not read document: Can not deserialize instance of java.util.ArrayList
Any suggestion will be appreciated, Thank you.

The easiest way is create wrapper class with id field as List (userIds).
Example:
class IdsWrapper {
private List<Integer> id;
}
And use it in controller:
public String addUsersToProject(#RequestBody IdsWrapper ids) {...}
Also you can check link Possible maping solutions

Related

Can we validate the presence of a request tag in a JSON payload irrespective of the value

I have to validate a JSON payload to check if all the request attributes mentioned on the Rest API Specification are passed. the value of some of these can be null. The use of JSON annotations like #NotNull, #NotEmpty is not suitable as they validate the value as well.
I have tried #Jsoncreator on a constructor. This solution works perfect for simple payloads but I am not clear on how to get this worked in case of complex payloads involving nested objects.
Another problem to highlight is that the same objects are used for both POST and PUT operation payloads. I need the validation for only PUT operations.
Can you please suggest on feasible solutions?
for a JSON request of 2 attributes - name, age -
valid payloads -
{"name" :"StackOverflow", "age" :99}
{"name" :"StackOverflow", "age" :null}
invalid payload -
{"name" :"StackOverflow"}
I think your problem can be solved used by SpringMVC.
nested bean validation
Now we have a Customer class which involves nested class Address.
If an instance of Customer is validated, the referenced Address object will be validated as well, as the address field is annotated with #Valid.
public class Customer {
#NotBlank
private String name;
#NotBlank
private String email;
//add a #Valid annotation on nested class
#Valid
private Address address;
//setter and getter
}
public class Address {
#NotBlank
private String country;
#NotBlank
private String city;
//getter and setter
}
validate only PUT
You can use #PostMapping,#GetMapping,#PutMapping,#DeleteMapping....mapping HTTP requests onto specific handler methods.
#PutMapping(value = "/customer")
public String editCustomerInfo(#Valid Customer customer){
System.out.println(customer.toString());
return "put succeed";
}
#PostMapping(value = "/customer")
public String addCustomerInfo(Customer customer){
System.out.println(customer.toString());
return "post succeed";
}

JSON to POJO conversion using Jackson & JsonPath

I have a data scenario, where I want to populate an attribute in my POJO class using the attribute of a nested object. The below is just an example, but I have many such nested variables.
For example my java class is as follow
public class Book{
private String title;
private String author;
private String isbn;
...
}
And my json response that I need to deserialize is as follow
{
"title":"the jungle book",
"author":"Rudyard Kipling",
"code":{
"isbn":"1616416920"
}
}
So the attribute isbn in my java class, needs to be populated using the nested field isbn inside the code object.
I tried to use the #JsonProperty as follow
#JsonProperty(value="code.isbn")
private String isbn;
But it still set the attribute isbn to null inside my java POJO class.
Can someone please suggest how can I do this.
Thanking you in advance.
you can write custom code to parse the json to create Book object having isbn value set by parsing nested object.
or
for the given json, you need to define an object say "Code"
public class Book {
#JsonProperty(value="title")
private String title;
#JsonProperty(value="author")
private String author;
#JsonProperty(value="code")
private Code code;
}
public class Code {
#JsonProperty(value="isbn")
public String isbn;
}

REST: how to serialize a java object to JSON in a "shallow" way?

Suppose I have the following JPA entities:
#Entity
public class Inner {
#Id private Long id;
private String name;
// getters/setters
}
#Entity
public class Outer {
#Id private Long id;
private String name;
#ManyToOne private Inner inner;
// getters/setters
}
Both Spring and java EE have REST implementations with default serializers which will marshall the entities to/from JSON without further coding. But when converting Outer to JSON, both Spring and EE nest a full copy of Inner within it:
// Outer
{
"id": "1234",
"name": "MyOuterName",
"inner": {
"id": "4321",
"name": "MyInnerName"
}
}
This is correct behavior but problematic for my web services, since the object graphs can get deep/complex and can contain circular references. Is there any way to configure the supplied marshaller to marshall the POJOs/entities in a "shallow" way instead without having to create a custom JSON serializer for each one? One custom serializer that works on all entities would be fine. I'd ideally like something like this:
// Outer
{
"id": "1234",
"name": "MyOuterName",
"innerId": "4321"
}
I'd also like it to "unmarshall" the JSON back into the equivalent java object. Bonus kudos if the solution works with both Spring and java EE. Thanks!
After many problems I give reason to Cássio Mazzochi Molin saying that "the use of entities persistence in your REST API can not be a good idea"
I would do that the business layer transform persistence entities to DTO.
You can do this very easily with libraries like mapstruct
If you still want to continue with this bad practice you can use jackson and customize your jackson mapper
To unscramble complex object graphs using jaxb #XmlID and #XmlIDREF is made for.
public class JSONTestCase {
#XmlRootElement
public static final class Entity {
private String id;
private String someInfo;
private DetailEntity detail;
#XmlIDREF
private DetailEntity detailAgain;
public Entity(String id, String someInfo, DetailEntity detail) {
this.id = id;
this.someInfo = someInfo;
this.detail = detail;
this.detailAgain = detail;
}
// default constructor, getters, setters
}
public static final class DetailEntity {
#XmlID
private String id;
private String someDetailInfo;
// constructors, getters, setters
}
#Test
public void testMarshalling() throws JAXBException {
Entity e = new Entity( "42", "info", new DetailEntity("47","detailInfo") );
JAXBContext context = org.eclipse.persistence.jaxb.JAXBContextFactory.createContext(new Class[]{Entity.class}, null);
Marshaller m = context.createMarshaller();
m.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true);
m.setProperty(MarshallerProperties.MEDIA_TYPE, "application/json");
m.setProperty(MarshallerProperties.JSON_INCLUDE_ROOT, false);
m.marshal(e, System.out);
}
}
This will result in the following json-fragment
{
"detailAgain" : "47",
"detail" : {
"id" : "47",
"someDetailInfo" : "detailInfo"
},
"id" : "42",
"someInfo" : "info"
}
Unmarshalling of this json will ensure that detail and detailAgain are the same instances.
The two annotations are part of jaxb, so it will work in Spring as well as in java EE. Marshalling to json is not part of the standard, so i use moxy in the example.
Update
Explicitly using moxy is not neccessary in a JAX-RS Resource. The following snipped perfectly runs on a java-EE-7 container (glassfish 4.1.1) and results in the above json-fragment:
#Stateless
#Path("/entities")
public class EntityResource {
#GET
#Produces(MediaType.APPLICATION_JSON)
public Entity getEntity() {
return new Entity( "42", "info", new DetailEntity("47","detailInfo") );
}
}
I had the same problem and ended up using jackson annotations on my Entities to control the serialization:
What you need is #JsonIdentityReference(alwaysAsId=true) to instruct the bean serializer that this reference should be only an ID. You can see an example on my repo:
https://github.com/sashokbg/company-rest-service/blob/master/src/main/java/bg/alexander/model/Order.java
#OneToMany(mappedBy="order", fetch=FetchType.EAGER)
#JsonIdentityReference(alwaysAsId=true) // otherwise first ref as POJO, others as id
private Set<OrderDetail> orderDetails;
If you want a full control of how your entities are represented as JSON, you can use JsonView to define which field is serialized related to your view.
#JsonView(Views.Public.class)
public int id;
#JsonView(Views.Public.class)
public String itemName;
#JsonView(Views.Internal.class)
public String ownerName;
http://www.baeldung.com/jackson-json-view-annotation
Cheers !
for this problem There are two solutions.
1-using jackson json view
2- Createing two mapping classe for innner entity. one of them includes custom fields and another one includes all fields ...
i think jackson json view is better solution ...
Go through the FLEXJSON library to smartly include/exclude nested class hierarchy while serializing Java objects.
Examples for flexjson.JSONSerializer presented here
You can detach the JPA entity before serialization, if you use lazyloading it's avoid to load sub objects.
Another way, but is depend of the JSON serializer API, you can use "transient" or specifics annotation.
Why does JPA have a #Transient annotation?
A bad way is to use tool like dozer to copy JPA object in another class with only the properties need for json (but it works... little overhead of memory, CPU and time...)
#Entity
public class Outer {
#Id private Long id;
private String name;
#ManyToOne private Inner inner;
//load manually inner.id
private final Long innerId;
// getters/setters
}

Jackson Serialize and Deserialize String property as JSON

I have a model that looks like this (Play 2.1.1 java ebean)
#Entity
public class Link extends Model {
#Id
public Long id;
#Lob
public String points;
}
where points is a raw json string that contains x, y coordinates in an array.
I don't want to have to deserialize it to an array of Points, because it's only going to be used for the UI. and thus, I would like to save it to a text field in the database
I want the property points to get serialized as a json array when sent over the wire to the frontend and I want the frontend to be able to send an json array and make it into a string again.
In the controller:
// Serialize
List<Link> links = Link.findAll();
return ok(Json.toJson(links));
// Deserialize
Link link = Json.fromJson(request().body().asJson(), Link.class);
How should I do this?
Custom serializer, deserializer?
#JsonRawValue?
Any other annotation?
The answer was a lot simpler than you would suspect.
#Lob
public String points;
public JsonNode getPoints() {
return Json.parse(StringUtils.isBlank(points) ? "[]" : points);
}
#JsonSetter
public void setPoints(JsonNode json) {
points = json.toString();
}
Though I'm not that fond of getter's and setter's it works.
If anyone has a better solution that is more generic, feel free to post another answer :)

Jackson deserialization when POJO does not match JSON structure

I have some json :
{
key: "CORE-19",
fields: { summary: "iblah" }
}
I want to pack it into a POJO that looks more like:
#JsonIgnoreProperties(ignoreUnknown=true)
public class JiraIssue
{
private String mKey;
private String mSummary;
public String getKey(){ return(mKey);}
public void setKey(String inKey){mKey = inKey;}
public String getSummary(){return(mSummary);}
public void setSummary(String summary){ mSummary = summary; }
}
So basically I don't want to create a 'Fields' object as it is a bit superfluous for my needs. However I really can't see any way in Jackson to tell it that the 'summary' property actually comes from the 'fields' property. Is this possible?
Serialization of this class is not a concern, it will only ever be used for Deserialization. I have no control over the JSON format as it is coming from an external source (and the above is just a snippet). Also I'm actually using Jackson with Jersey.
Thanks!
There is actually an open issue for this kind of structural change. There is no way as of now to do that easily with annotation only without modifying your class. What you could do instead is handle the "fields" property as a "false" property, by adding the following method:
public void setFields(Map<String, String> fields) {
setSummary(fields.get("summary"));
}
This way you "unwrap" the property yourself.
Try:
#JsonProperty("fields.summary")
private String mSummary;