I appeal to the controller, which must return a list of objects. Objects are converted to a JSON string. When converting objects to a JSON string, all object getters are called. As a result, all object data is returned. How to prevent the return of some fields of the object, maybe there is a special annotation?

@Controller @MessageMapping("/v1.0") public class MyController { @MessageMapping("/getList") @SendTo("/topic/List") public Collection<MyObj> getList() throws Exception { return this.MyObjService.getList(); } } 

Actually a class of future objects:

 public class MyObj { private String name; private String login; public String getName() { return name; } public String getLogin() { return login; } } 

I would like to prohibit the return of the login field.

  • 2
    If I guessed it, adding annotations to the @JsonIgnore field should help - Artem Konovalov
  • Thank! helped - Alex

1 answer 1

As noted by @ArtemKonovalov, by default, Spring uses the Jackson library as its JSON Mapper. She suggests using the @JsonIgnore annotation.


But

Sooner or later, you will want to get two different JSON with different fields from the same object. Therefore, in a good way, you need to make separate DTOs without logic, which you will fully serialize into JSON.


Ps. In addition, I recommend to make it a rule to always explicitly hang the @JsonProperty annotation on the fields and explicitly indicate the field name in it. Otherwise, random refactoring with field renaming will break your API.

  • Thank! I will try to implement your advice - Alexey