1

My flow is given below.

enter image description here What is my issue:

Input json request has some fields set to "". I want the response json to have those fields set to "" as well. Instead, the fields are assigned values null or 0.

What I need: How do I set the fields in response to "" instead of null or 0?

Code:

JSON Request:

    {
    userID: "ABC",
    creationDate: "",
    noOfItems: "" 
}

Bean Definition:

class UserOrderDetails {
    private String userID;
    private LocalDate creationDate;
    private int noOfItems;

    public String getUserID() {
        return this.userID;
    }

    public void setUserID(String userID) {
        this.userID=userID;
    }
    public LocalDate getCreationDate() {
        return this.creationDate;
    }

    public void setCreationDate(LocalDate creationDate) {
        this.creationDate=userID;
    }   
    public int getNoOfItems() {
        return this.noOfItems;
    }

    public void setNoOfItems(int noOfItems) {
        this.noOfItems=noOfItems;
    }
}

Controller class:

class UserOrderCreationController {
@RequestMapping(value = "/createUserOrder", method = RequestMethod.POST)
    public ResponseEntity<?> createUserOrder(@RequestBody UserOrderDetails userOrderDetails , @RequestHeader HttpHeaders headers) throws Exception{
    //Some business logic to handle the user Order
    return new ResponseEntity<UserOrderDetails>(userOrderDetails, HttpStatus.CREATED);
    }
}

Response:

{
    userID: "ABC",
    creationDate: null,
    noOfItems: 0 
}

NOTE: I cannot change LocalDate, int - data types of CreationDate and noOfItems to String since it is used by many other classes.

Deepboy
  • 523
  • 3
  • 16
  • 28
  • LocalDate is not native to JSON. Date time is always in String. More details in here: https://stackoverflow.com/questions/10286204/the-right-json-date-format – Pete T Jun 07 '18 at 16:07

1 Answers1

1

For the String fields,whatever the value you send that will be saved and it will be given back,For other data types such as DATE,Integer,while converting JSON value to JAVA if the value is Balnk("")/null,the object-mapper convert it to null and save to DB,the same will be given back in response. If you want null as "" in response you should write customSerializer which will convert null to "" in response. You need to annotate those fields with
@JsonSerialize(using = ToStringSerializer.class)

ToStringSerializer.class we need to write.

Workaround: I have got this workaround ,Can you try it and see.

public class AssignNullSerializer2 extends JsonSerializer {

public void serialize(Object value, JsonGenerator jgen, SerializerProvider provider)
        throws IOException, JsonProcessingException
{
    // any JSON value you want...
    if(value==null) {
    jgen.writeString("");
    }
}

} In ApplicationConfiguration add the below snippet.

    ObjectMapper mapper = new ObjectMapper();
DefaultSerializerProvider sp=new DefaultSerializerProvider.Impl();
sp.setNullValueSerializer(new AssignNullSerializer2());
mapper.setSerializerProvider(sp);

It will convert all the null values in DTO'S/Response Objects to ""/Empty_String I did not find control to apply it on specific field.

  • I tried using a custom serializer and @JsonSerialize(using= CustomDateSerializer.class). As per https://stackoverflow.com/questions/35342323/jsonserialize-not-working, it does not work for null values. Do you know anything else that might work? – Deepboy Jun 07 '18 at 19:25
  • I tried @JsonSerialize(nullsUsing = CustomDateSerializer.class) as well, but the serializer class is not called. – Deepboy Jun 07 '18 at 19:49
  • You are correct.For null values by default CustomSerializer will not work! – Naveen Kumar Katragadda Jun 08 '18 at 10:12
  • Can you try the workaround i have mentioned in the edited comment above. – Naveen Kumar Katragadda Jun 08 '18 at 10:24
  • thankyou. I will try the workaround and let you know how it goes. – Deepboy Jun 08 '18 at 12:40
  • If possible, can you pls take a look at another question of mine? - https://stackoverflow.com/questions/50764815/map-one-value-to-multiple-columns-in-jpa – Deepboy Jun 08 '18 at 16:26