1

Im using Spring Boot 2.1.2.RELEASE together with Angular 7 and MongoDB.

Im writing a service where i want to create a new DB entry but I get an error message from Spring Boot saying JSON parse error: null spring boot.

The problem is that when Angular is creating the JSON which is sent in the request it includes all values that are null as { id: null, ... } I have tried to find a solution to my issue, but the answers i found isn't that satisfying for me.

  1. Build the JSON string manually in Angular and exclude all parameters that are null. (for complex object where serveral values can be null this is not a nice solution

  2. Add the following config to get jackson to understand that it should interpritade this as a null value:

spring.jackson.default-property-inclusion=NON_NULL

or

spring.jackson.default-property-inclusion=non_null

This does not work for me. Still the same problem.

Does anyone have any suggestion of how to solve this issue?

Thanks!

Micke
  • 11
  • 4

1 Answers1

1

You can use @JsonInclude annotation like this:

@JsonInclude(Include.NON_NULL)
public class MyViewModel { ... }

If you want you can also apply on property granularity:

public class MyViewModel {

    @JsonInclude(Include.NON_NULL)
    private String value;
    ...
}
NiVeR
  • 9,644
  • 4
  • 30
  • 35
  • I have tried this aswell but with no success, still get the same error. Looking at this answer they have suggested this approach when spring boot is rendering the JSON should the same annotation have affect on parsing the JSON ?https://stackoverflow.com/questions/12707165/spring-rest-service-how-to-configure-to-remove-null-objects-in-json-response – Micke Mar 05 '19 at 14:13