Spring Boot @Responsebody Doesn't Serialize Entity Id

Spring boot @ResponseBody doesn't serialize entity id

I recently had the same problem and it's because that's how spring-boot-starter-data-rest works by default. See my SO question -> While using Spring Data Rest after migrating an app to Spring Boot, I have observed that entity properties with @Id are no longer marshalled to JSON

To customize how it behaves, you can extend RepositoryRestConfigurerAdapter to expose IDs for specific classes.

import org.springframework.context.annotation.Configuration;
import org.springframework.data.rest.core.config.RepositoryRestConfiguration;
import org.springframework.data.rest.webmvc.config.RepositoryRestConfigurerAdapter;

@Configuration
public class RepositoryConfig extends RepositoryRestConfigurerAdapter {
@Override
public void configureRepositoryRestConfiguration(RepositoryRestConfiguration config) {
config.exposeIdsFor(Person.class);
}
}

@GetMapping doesn't serialize Ids

I did it! Because I forgot to put the getter/setter for the model. And there are more potentials here I wanna tell:

  • private int id should be changed to private Integer id as @Muhammad Vaqas told me
  • Try to see the solution from this question: Spring boot @ResponseBody doesn't serialize entity id

And there is the full form of the model:

package com.harrycoder.weebjournal.user;

import java.util.Date;

import javax.persistence.*;

import org.springframework.data.annotation.CreatedDate;

import com.fasterxml.jackson.annotation.*;

@Entity
@Table(name = "users")
@JsonIgnoreProperties(value = {"createdAt", "updatedAt"},
allowGetters = true)
public class User {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Integer id;

@Column(name = "username")
private String username;

@Temporal(TemporalType.TIMESTAMP)
@Column(name = "joined_date")
@CreatedDate
private Date joinedDate;

@Column(name = "password")
private String password;

@Column(name = "bio")
private String bio;

@Column(name = "email")
private String email;

public Integer getId() {
return id;
}

public String getUsername() {
return username;
}

public String getPassword() {
return password;
}

public String getBio() {
return bio;
}

public String getEmail() {
return email;
}

public Date getJoinedDate() {
return joinedDate;
}
}

spring boot expose id

This guy answered it like I wanted to, thanks for the help.

When using Spring Data REST it has something especially designed for this. There is the notion of Projections and Excerpts with it you can specify what and how you want to return it.

@Projection(name="personSummary", types={Person.class})
public interface PersonSummary {
String getEmail();
String getId();
String getName();
}

Spring boot JPA deserialization problem when retrieving ID from other entity

You’re trying to deserialize member from json, but jackson do not know how to fill in this member with just “member_id”, and the error is clearly say so “no constructer with String is found”. So, in order to get the member id, you need to add new class request dto, something like:

String name;
String footage;
Long owner_id;

Use your new class in your controller, now with owner_id, use repository to find it in your db, then set it to your store.

Spring Data REST hides technical entity fields (@Version, @Id) from JSON by default. How to return them as usual properties?

Showing the ID of the entity is configuring in the RepositoryRestConfigurerAdapter:

@Bean
public RepositoryRestConfigurerAdapter repositoryRestConfigurerAdapter() {
return new RepositoryRestConfigurerAdapter() {
/**
* Exposing ID for some entities
*/
@Override
public void configureRepositoryRestConfiguration(RepositoryRestConfiguration config) {
config.exposeIdsFor(MyEntity.class);
super.configureRepositoryRestConfiguration(config);
}

};
}

Return IDs in JSON response from Spring Data REST

Spring Data Rest hides the ID by default, in order to have it in the JSON you have to manually configure that for your entity. Depending on your spring version you can either provide your own configuration (old):

@Configuration
public class ExposeEntityIdRestConfiguration extends RepositoryRestMvcConfiguration {

@Override
protected void configureRepositoryRestConfiguration(RepositoryRestConfiguration config) {
config.exposeIdsFor(Book.class);
}
}

...or register a RepositoryRestConfigurer (current):

@Component
public class ExposeEntityIdRestMvcConfiguration extends RepositoryRestConfigurerAdapter {

@Override
public void configureRepositoryRestConfiguration(RepositoryRestConfiguration config) {
config.exposeIdsFor(Book.class);
}
}

See the Spring Data Rest documentation for more details.



Related Topics



Leave a reply



Submit