Home > Back-end >  How to ignore field/column only when return response from spring boot
How to ignore field/column only when return response from spring boot

Time:12-22

I need to ignore the field when return the response from spring boot. Pls find below info,

I have one pojo called Student as below

Student {
id,
name,
lastName
}

i am getting a body for as PostRequest as below

{
id:"1",
name:"Test",
lname:"Test"
}

i want get all the data from frontEnd (id,name,Lname) But i just want to return the same pojo class without id as below,

{
name:"Test",
lName:"Test"
}

I have tried @JsonIgnore for column id, But it makes the id column as null(id=null -it is coming like this even when i send data to id field from postman) when i get the data from frontEnd.

I would like to use only one pojo to get the data with proper data(withoud getting id as Null), and need to send back the data by ignoring the id column.

Is there any way to achieve it instead of using another pojo?

CodePudding user response:

You just need to use @JsonInclude(JsonInclude.Include.NON_NULL) at class level and it will be helpful for ignore all your null fields.

For example :

@JsonInclude(JsonInclude.Include.NON_NULL)
public class Test {
    // Fields
    // Constructors
    // Getters - setters
}

As of now you are using only one POJO it's not good practice because it's your main entity into your project, so good practice is always make DTO for the same.

CodePudding user response:

This is possible via the @JsonView annotation that is part of Jackson. Spring can leverage it to define the views used on the controller.

You'd define your DTO class like this:

class User {
    User(String internalId, String externalId, String name) {
        this.internalId = internalId;
        this.externalId = externalId;
        this.name = name;
    }
    @JsonView(User.Views.Internal.class)
    String internalId;

    @JsonView(User.Views.Public.class)
    String externalId;

    @JsonView(User.Views.Public.class)
    String name;

    static class Views {
        static class Public {

        }

        static class Internal extends Public {

        }
    }
}

The Views internal class acts as a marker to jackson, in order to tell it which fields to include in which configuration. It does not need to be an inner class, but that makes for a shorter code snippet to paste here. Since Internal extends Public, all fields marked with Public are also included when the Internal view is selected.

You can then define a controller like this:

@RestController
class UserController {

    @GetMapping("/user/internal")
    @JsonView(User.Views.Internal.class)
    User getPublicUser() {
        return new User("internal", "external", "john");
    }

    @GetMapping("/user/public")
    @JsonView(User.Views.Public.class)
    User getPrivateUser() {
        return new User("internal", "external", "john");
    }
}

Since Spring is aware of the JsonView annotations, the JSON returned by the /public endpoint will contain only externalId and name, and the /internal endpoint will additionally include the internalId field.

Note that fields with no annotation will not be included if you enable any view. This behaviour can be controlled by MapperFeature.DEFAULT_VIEW_INCLUSION, which was false in the default Spring ObjectMapper when I used this for the last time.

  • Related