Hello. There was a situation not quite clear to me: there is an essence

@Entity @Table(name = "trCompany") @JsonInclude(JsonInclude.Include.NON_NULL) public class TransportCompany implements Serializable, Storable { private static final long serialVersionUID = 1000000005645644L; @JsonProperty("id") @JsonView(Views.User.class) @Id @GeneratedValue(generator = "increment") @GenericGenerator(name = "increment", strategy = "increment") @Column(name = "id", nullable = false) private Long id; @JsonProperty("name") @JsonView(Views.User.class) @Column(name = "name", nullable = true, length = 150) private String name; @JsonProperty("phone") @JsonView(Views.User.class) @Column(name = "phone", nullable = true) private String phone; ... @Transient @JsonView(Views.User.class) private Collection<Orders> orderCollection; @Override public String toString() { return String.format("%d, %s, %s, %s;", id, name, phone, orderCollection); } ... } 

There are dao and services, and a view controller

 @RestController public class TransportCompanyController { ..... @JsonView(Views.User.class) @PreAuthorize(value = "hasAnyRole('ROLE_ADMIN', 'ROLE_USER')") @RequestMapping(value = "/t/all", method = RequestMethod.GET, produces = MediaType.APPLICATION_JSON_VALUE) public ResponseEntity getAll(){ List<TransportCompany> transportCompanyList = transportCompanyService.getAll(); System.out.println("TR-COMP: " + transportCompanyList.get(0).toString()); return ResponseEntity.status(HttpStatus.OK).body(transportCompanyList); } ... } 

And this is all wrapped up in spring-boot. In general, the problem is in the received JSON response, it looks like this:

 [{"name":"Новая почта","phone":"+380671111111"},{...},{...}] 

and I, well, very, very much, need the answer to look like this, with ID :

  [{"id":"1","name":"Новая почта","phone":"+380671111111"},{...},{...}] 

In general, something with the JSON representation is not that, although working with similarly described entities for other objects, but similar, objects and their controllers, respectively, work normally, i.e. the answer comes with the ID - as it should be ... Thanks in advance for the answer! PS / Just in case. Inside the controller there is System.out, everything is fine there ID - is present.

    1 answer 1

    Do this and your id will be displayed in json:

     @Entity @Table(name = "trCompany") @JsonInclude(JsonInclude.Include.NON_NULL) public class TransportCompany implements Serializable, Storable { @JsonView(Views.User.class) @GeneratedValue(generator = "increment") @GenericGenerator(name = "increment", strategy = "increment") @Column(name = "id", nullable = false) @id Long id; @JsonProperty("id") public Long getId() { return id; } public void setId(Long id){ this.id = id; } } 
    • To be honest, it did not help. But thanks a lot , thanks to you, I found my retarding mistake . It turned out that my getter was with NULL)). and System.out worked tk there toString (). In general, it worked because I never used a getter anywhere else ... PS / I need to be more careful, damn it ... Thank you. From here you can learn the lesson that even after the auto-generation of setters and getters, they are worth = CHECK, damn ... - Alex
    • Use Lombok and don't bother with getters - Borisov Max