Return some object in my Json

后端 未结 1 1399
梦毁少年i
梦毁少年i 2021-01-27 23:23

My userController.java:

@RestController
public class UserController {

    private static final Logger LOGGER = LoggerFactory.getLogger         


        
1条回答
  •  陌清茗
    陌清茗 (楼主)
    2021-01-28 00:17

    You could use @JsonView to control what is rendered. You can find more details in this Spring blog post https://spring.io/blog/2014/12/02/latest-jackson-integration-improvements-in-spring

    Assuming you have this View class with interface Summary

    public class View {
        interface Summary {}
    }
    

    You could then annotate your properties like so:

    @JsonView(View.Summary.class)
    private String name;
    
    @JsonView(View.Summary.class)
    private String firstname;
    

    And then your request mapping:

    @JsonView(View.Summary.class)
    @RequestMapping(value = "/user", method = RequestMethod.GET)
    

    This would only return name and firstname in the resulting JSON.

    0 讨论(0)
提交回复
热议问题