Micke
Micke

Reputation: 11

Angular, Spring boot REST getting JSON parse error: null spring boot

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!

Upvotes: 1

Views: 1315

Answers (1)

NiVeR
NiVeR

Reputation: 9806

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;
    ...
}

Upvotes: 1

Related Questions