I'm building a POJO class to match some requests coming my way in a REST API implemented on SpringBoot. Some of the data has to be given to me, otherwise I am not willing to even serve the request. To make sure the client gives me at least what I absolutely need, I have used Lombok's #NonNull:
#Data
public class ProductRequestBody implements Serializable
{
private String name;
private String category;
private String description;
private String abbreviation;
private String labelColor;
private Double cost;
public ProductRequestBody()
{
}
#JsonCreator
public ProductRequestBody(#NonNull #JsonProperty("name") String name,
#NonNull #JsonProperty("category") String category,
#JsonProperty("description") String description,
#NonNull #JsonProperty("cost") Double cost)
{
this.name = name;
this.category = category;
this.description = description;
this.cost = cost;
}
}
(I completely understand that handling monetary quantities as Doubles is a no-no; this is just an example.
Processing this from my controller is as easy as a listener on the /products endpoint like so:
#PostMapping(value = "/products")
public Product postProduct(#RequestBody ProductRequestBody newProduct)
{
// ...
// Serve the request appropriately
// ...
}
Now, if I receive a POST request with a null field that has not been marked as #NonNull, like the following, I can serve it without issue:
{
"name": "Some Product Name",
"category": "Some Product Category",
"cost" : 10.0,
"description": null
}
My goal, however, is to be able to handle JSON requests that simply don't even have the fields they don't care about. That is, I want to be able to serve the following as well, and I currently can't:
{
"name": "Some Product Name",
"category": "Some Product Category",
"cost" : 10.0,
// No "description" field in this payload
}
How could I go about doing this? The less code, the better, as always.
If you use spring-boot and lombok you can simplify your class to be like this:
#Data
public class ProductRequestBody implements Serializable {
#NonNull
private String name;
#NonNull
private String category;
#NonNull
private Double cost;
private String description;
private String abbreviation;
private String labelColor;
}
it will return 400 if name, category or cost will be not provided and 200 otherwise.
Spring handles serializing and deserializing json without any issue.
You should let spring handle it.
You can try the following.
#Data
public class ProductRequestBody implements Serializable {
#NonNull
private String name;
#NonNull
private String category;
private String description;
private String abbreviation;
private String labelColor;
#NonNull
private Double cost;
}
If you really want to follow the pattern of creating a constructor, then you should create a constructor with only the #NonNull fields and create getter of others (lombok handles that for you). If you want to add #JsonProperty then you need to create separate getter.
#Data
public class ProductRequestBody implements Serializable
{
private String name;
private String category;
private String description;
private String abbreviation;
private String labelColor;
private Double cost;
public ProductRequestBody()
{
}
#JsonCreator
public ProductRequestBody(#NonNull #JsonProperty("name") String name,
#NonNull #JsonProperty("category") String category,
#NonNull #JsonProperty("cost") Double cost)
{
this.name = name;
this.category = category;
this.cost = cost;
}
#JsonProperty("description")
public String getDescription() {
return description;
}
}
Related
In my spring boot project, I noticed a strange Jackson behavior. I searched over internet, found out what to do, but haven't found out why.
UserDto:
#Setter
#Getter
#AllArgsConstructor
public class UserDto {
private String username;
private String email;
private String password;
private String name;
private String surname;
private UserStatus status;
private byte[] avatar;
private ZonedDateTime created_at;
}
Adding a new user works just fine.
TagDto:
#Setter
#Getter
#AllArgsConstructor
public class TagDto {
private String tag;
}
Trying to add a new tag ends with an error:
com.fasterxml.jackson.databind.exc.MismatchedInputException: Cannot construct instance of TagDto (although at least one Creator exists): cannot deserialize from Object value (no delegate- or property-based Creator)
The solution to the problem was to add zero-arg constructor to the TagDto class.
Why does Jackson require no-arg constructor for deserialization in TagDto, while working just fine with UserDto?
Used same method for adding both.
My Tag and User entities are both annotated with
#Entity
#Setter
#Getter
#NoArgsConstructor
and have all args constructors:
#Entity
#Setter
#Getter
#NoArgsConstructor
public class User extends AbstractModel {
private String username;
private String password;
private String email;
private String name;
private String surname;
private UserStatus status;
#Lob
private byte[] avatar;
#Setter(AccessLevel.NONE)
private ZonedDateTime created_at;
public User(final String username, final String password, final String email, final String name, final String surname) {
this.username = username;
this.password = password;
this.email = email;
this.name = name;
this.surname = surname;
this.created_at = ZonedDateTime.now();
}
}
#Entity
#Setter
#Getter
#NoArgsConstructor
#AllArgsConstructor
public class Tag extends AbstractModel {
private String tag;
}
#MappedSuperclass
#Getter
public abstract class AbstractModel {
#Id
#GeneratedValue(strategy = GenerationType.AUTO)
private Integer id;
}
Entity generation:
#PostMapping(path = "/add")
public ResponseEntity<String> add(#Valid #RequestBody final D dto) {
this.abstractModelService.add(dto);
return new ResponseEntity<>("Success", HttpStatus.CREATED);
}
public void add(final D dto) {
//CRUD repository save method
this.modelRepositoryInterface.save(this.getModelFromDto(dto));
}
#Override
protected Tag getModelFromDto(final TagDto tagDto) {
return new Tag(tagDto.getTag());
}
#Override
protected User getModelFromDto(final UserDto userDto) {
return new User(userDto.getUsername(), userDto.getPassword(), userDto.getEmail(), userDto.getName(), userDto.getSurname());
}
Error occurs when parsing JSON
{"tag":"example"}
sent via postman localhost:8081/tag/add, returns
{
"timestamp": "2020-09-26T18:50:39.974+00:00",
"status": 400,
"error": "Bad Request",
"message": "",
"path": "/tag/add"
}
I am using Lombok v1.18.12 and Spring boot 2.3.3.RELEASE with Jackson v2.11.2.
TL;DR: Solution is at the end.
Jackson supports multiple ways of creating POJOs. The following lists the most common ways, but it likely not a complete list:
Create instance using no-arg constructor, then call setter methods to assign property values.
public class Foo {
private int id;
public int getId() { return this.id; }
#JsonProperty
public void setId(int id) { this.id = id; }
}
Specifying #JsonProperty is optional, but can be used to fine-tune the mappings, together with annotations like #JsonIgnore, #JsonAnyGetter, ...
Create instance using constructor with arguments.
public class Foo {
private int id;
#JsonCreator
public Foo(#JsonProperty("id") int id) {
this.id = id;
}
public int getId() {
return this.id;
}
}
Specifying #JsonCreator for the constructor is optional, but I believe it is required if there is more than one constructor. Specifying #JsonProperty for the parameters is optional, but is required for naming the properties if the parameter names are not included in the class file (-parameters compiler option).
The parameters imply that the properties are required. Optional properties can be set using setter methods.
Create instance using factory method.
public class Foo {
private int id;
#JsonCreator
public static Foo create(#JsonProperty("id") int id) {
return new Foo(id);
}
private Foo(int id) {
this.id = id;
}
public int getId() {
return this.id;
}
}
Create instance from text value using String constructor.
public class Foo {
private int id;
#JsonCreator
public Foo(String str) {
this.id = Integer.parseInt(id);
}
public int getId() {
return this.id;
}
#JsonValue
public String asJsonValue() {
return Integer.toString(this.id);
}
}
This is useful when a the POJO has a simply text representation, e.g. a LocalDate is a POJO with 3 properties (year, month, dayOfMonth), but is generally best serialized as a single string (yyyy-MM-dd format). #JsonValue identifies the method to be used during serialization, and #JsonCreator identifies the constructor/factory-method to be used during deserialization.
Note: This can also be used for single-value construction using JSON values other than String, but that is very rare.
Ok, that was the background information. What is happening for the examples in the question, it that UserDto works because there is only one constructor (so #JsonCreator is not needed), and many arguments (so #JsonProperty is not needed).
However, for TagDto there is only a single-argument constructor without any annotations, so Jackson classifies that constructor as a type #4 (from my list above), not a type #2.
Which means that it is expecting the POJO to be a value-class, where the JSON for the enclosing object would be { ..., "tag": "value", ... }, not { ..., "tag": {"tag": "example"}, ... }.
To resolve the issue, you need to tell Jackson that the constructor is a property initializing constructor (#2), not a value-type constructor (#4), by specifying #JsonProperty on the constructor argument.
This means that you cannot have Lombok create the constructor for you:
#Setter
#Getter
public class TagDto {
private String tag;
public TagDto(#JsonProperty("tag") String tag) {
this.tag = tag;
}
}
We have an Enum defined with Jackson annotation, "#JsonFormat(shape = JsonFormat.Shape.OBJECT)"
e.g.
package com.test;
import com.fasterxml.jackson.annotation.JsonFormat;
#JsonFormat(shape = JsonFormat.Shape.OBJECT)
public enum SampleEnumName {
OPTION_1 ("NAME_1", "DESCRIPTION_1", "CATEGORY_1"),
OPTION_2 ("NAME_2", "DESCRIPTION_2", "CATEGORY_2"),
OPTION_3 ("NAME_3", "DESCRIPTION_3", "CATEGORY_3");
private String name;
private String description;
private String category;
private SampleEnumName(String name, String description, String category) {
this.name = name;
this.description = description;
this.category = category;
}
public String getName() {
return name;
}
public String getDescription() {
return description;
}
public String getCategory() {
return category;
}
}
When its schema is generated we see it as an object, e.g.
{"type":"object","id":"urn:jsonschema:com:test:SampleEnumName","properties":{"name":{"type":"string"},"description":{"type":"string"},"category":{"type":"string"}}}
But for the same, when we check the JSON generated by swagger, the following is shown in schema:
{"type":"string","enum":["OPTION_1","OPTION_2","OPTION_3"]}
Is there a way (some swagger annotation equivalent of Jackson annotation "#JsonFormat(shape = JsonFormat.Shape.OBJECT)"), which can show Object definition of Enum?
We are using swagger 1.5.7
i got the same problem, updated to Swagger 2.9.2 and solved.
In my Android project I have two types of response where both response are identical except two keys.
Response 1
{"fullName":"William Sherlock Scott Holmes","address":"221B Baker Street, London, England, UK","downloads":642,"rating":3,"repos":["https://link1","https://link2","https://link3"]}
Response 2
{"name":"Sherlock","city":"London","downloads":642,"rating":3,"repos":["https://link1","https://link2","https://link3"]}
If you see the responses only two key names are changing fullName/name and address/city
I don't want to create one more pojo for other response. My question is: is it possible to use only one Pojo to read both responses?
public class AccountInfo {
private String name;
private String city;
//other objects
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getCity() {
return city;
}
public void setCity(String city) {
this.city = city;
}
//other setters and getters
}
Any help will be appreciated...
You can annotate the members to accept values from two different json names using the #SerializedName annotation:
#SerializedName(value = "name", alternate = {"fullName"})
private String name;
#SerializedName(value = "city", alternate = {"address"})
private String city;
Either named element can then be placed into the members that are annotated like this.
UPDATED :
#SerializedName alternate names when deserializing is added in Version 2.4
Yes, you can totally use one POJO class for deserializing both responses. Your POJO class will contain keys from both responses.
public class Response {
private String name;
private String city;
private String fullName;
private String address;
private Integer downloads;
private Integer rating;
private List<String> repos ;
}
But when using the Response class, be careful that for first response, the name and city will be null, and for the second one, the address and fullname.
Yeah you can do that in a single POJO. Try this:
public class POJO {
#SerializedName("name")
public String name;
#SerializedName("city")
public String city;
#SerializedName("fullName")
public String fullName;
#SerializedName("address")
public String address;
#SerializedName("downloads")
public Integer downloads;
#SerializedName("rating")
public Integer rating;
#SerializedName("repos")
public List<String> repos = new ArrayList<String>();
}
While parsing you have to check values for null. For eg -
While Parsing Response 1: name and city variables will be null
While Parsing Response 2: fullname and address will be null
Note : Try checking values for null before using else you'll get nullpointerexception
Define all possible fields in your POJO Class like
public class AccountInfo {
private String name;
private String city;
private String fullname;
private String address;
}
While performing operation check for null in those feilds
I'm trying to learn about serialization and encountered the following problem:
I have an implementation of a customer that looks somewhat like this.
private static customerCount = 0;
private String customerID;
private String name;
private String street;
private String city;
private String postcode;
private String type;
I'm trying to serialize / deserialize an Arraylist
In the constructor, the ID will be created like this:
private Customer(...){
this.customerID = "ID" + customerCount;
customerCount++;
}
The serialization process works, however, all the IDs are set to ID0 when I deserialize.
Can anyone help resolve this problem?
Update: Alright, I just found out that static fields wont be serialized. How can I "model" the ID of a customer so I can serialize it? I need to have a unique value to create IDs for customers.
Here's a solution that combines the factory with the list that keeps track of customer count.
The customer class has a protected constructor, forcing you to build them through another means within the same package.
public class Customer implements Serializable {
private String customerID;
private String name;
private String street;
private String city;
private String postcode;
private String type;
protected Customer(String customerID,
String name,
String street,
String city,
String postcode,
String type) {
this.customerID = customerID;
this.name = name;
this.street = street;
this.city = city;
this.postcode = postcode;
this.type = type;
}
}
Now within the package, create a list wrapper like this:
public class CustomerList {
private int customerCount = 0;
private List<Customer> customers = new ArrayList<>();
public boolean addCustomer(String name,
String street,
String city,
String postcode,
String type) {
Customer customer = new Customer("ID" + customerCount++,
name,
street,
city,
postcode,
type);
return customers.add(customer);
}
}
This class then takes care of constructing the new customer, and provides a unique ID.
Edit: Just noticed that you now also have the upside of making the CustomerList class serializable as well. Then you can load it and still have an accurate customer count for adding additional uniquely ID-ed customers.
Usually, you would like to serialize only attributes and their values, not the logic from the class. Logic should happen before serialization or after deserialization.
Evening,
I retrieve JSON data from a server in this format:
Json-Book:
{
_id: String,
isbn: String,
owner: String username,
rentedTo: Array[String usernames]
}
Json-Crowd:
{
_id: String,
isbn: String,
owner: String username,
availableForRent: Integer,
rentedTo: Array[String usernames]
}
Json-User:
{
username: String,
books: Array[Book],
crowds: Array[Crowd]
}
Similarly, I have three classes:
public class Book{
private String _id;
private String isbn;
private User owner;
private ArrayList<User> rentedTo;
public Book(String _id, String isbn, User owner, ArrayList<User> rentedTo) {
this._id = _id;
this.isbn = isbn;
this.owner = owner;
this.rentedTo = rentedTo;
}
}
public class Crowd {
private String _id;
private String name;
private User owner;
private ArrayList<User> members;
public Crowd(String _id, String name, User owner, ArrayList<User> members) {
this._id = _id;
this.name = name;
this.owner = owner;
this.members = members;
}
}
public class User {
private String name;
private Shelf shelf;
private ArrayList<Book> books;
private ArrayList<Crowd> crowds;
public User(String name, ArrayList<Book> books, ArrayList<Crowd> crowds) {
this.name = name;
this.booksOwned = books;
this.crowds = crowds;
}
}
As you can see, all the field names match up, but not all of the types. Users in the keys owner and rentedTo in Json-book and Json-crowd have values of type String and ArrayList<String> respectively, where the strings are unique usernames. In the classes, these fields are of type User and ArrayList<User>. In and of itself, this is no problem, because I can get the User object from the string with this method:
public User getUser(String username) {
return users.get(username);
}
Now, I'm a bit confused as to what deserializers I need in order to get proper objects from the JSON data. How would the, uh, architecture of this look? Deserializers for each class Book, Crowd and User, each of which fetches one field at a time from the Json-data and calls the constructor?
I can't wrap my head around how this would work together. The deserializer for User would need to use/reference the deserializer for Crowd, but do I another deserializer since the objects are in array?
I assume I would need an opposite set of serializers in order to get the classes into Json-data of the correct format.