2

I've been exploring Spring Boot (1.2.4) to create a simple RESTful repository, using H2 & Jackson.

Like others I've noticed that, by default, @Id fields aren't in the returned Json (as described in this 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 ).

So I want to tweak the configuration to include them.

BUT I'm having trouble getting it all to work.

Basic entity class:

@Entity
public class Thing {

    @Id
    @GeneratedValue(strategy=GenerationType.AUTO)
    private long id; //I want this to appear in the RESTful JSON

    private String someInformation;

    protected Thing(){};

    public String getSomeInformation() { return someInformation; }

    public void setSomeInformation(String someInformation) { this.someInformation = someInformation; }

}

Basic repository interface:

public interface ThingRepository extends PagingAndSortingRepository<Thing, Long> {    
    List<Thing> findBySomeInformation(@Param("SomeInformation") String someInformation);
}

Simple starting application:

@ComponentScan
@EnableAutoConfiguration
public class Application {    
    public static void main(String[] args) throws Exception {
        SpringApplication.run(Application.class, args);
    }    
}

And my configuration class:

@Configuration
public class RepositoryRestMvcConfig extends SpringBootRepositoryRestMvcConfiguration {     
    @Override
    protected void configureRepositoryRestConfiguration(RepositoryRestConfiguration config) {
        config.exposeIdsFor(Thing.class);
    }        
}

I can see from running Spring Boot with --debug (as described at http://docs.spring.io/spring-boot/docs/current/reference/html/using-boot-auto-configuration.html ) that my config has been found but it doesn't seem to have any effect.

afaulconbridge
  • 1,107
  • 9
  • 21

1 Answers1

0

Jackson works on the getters/setters on a class rather than the member variables themselves. So to make sure the id is in the JSON, simply add a getter for it:

@Entity
public class Thing {

    @Id
    @GeneratedValue(strategy=GenerationType.AUTO)
    private long id; //I want this to appear in the RESTful JSON

    private String someInformation;

    protected Thing(){};

    //this is what is needed
    public long getId() { return id; }

    public String getSomeInformation() { return someInformation; }

    public void setSomeInformation(String someInformation) { this.someInformation = someInformation; }

}
afaulconbridge
  • 1,107
  • 9
  • 21