I am following the spring-data-rest guide Accessing JPA Data with REST. When I http post a new record it is inserted (and the response is a 201). That is great, but is there a way to configure the REST MVC code to return the newly created object? I'd rather not have to send a search request to find the new instance.
4 Answers
You don't have to search for the created entity. As the HTTP spec suggests, POST
requests returning a status code of 201 Created
are supposed to contain a Location
header which contains the URI of the resource just created.
Thus all you need to do is effectively issuing a GET
request to that particular URI. Spring Data REST also has two methods on RepositoryRestConfiguration.setReturnBodyOnCreate(…)
and ….setReturnBodyOnUpdate(…)
which you can use to configure the framework to immediately return the representation of the resource just created.

- 6,297
- 17
- 77
- 138

- 80,157
- 18
- 225
- 211
-
2Based on this question, I have actually updated ALL of the Spring Data REST getting started guides to include this information, so you don't have to come here to discover the feature. – gregturn Sep 12 '14 at 18:00
-
@gregturn I think this is exactly the solution I've been looking for. – chrylis -cautiouslyoptimistic- Sep 12 '14 at 18:03
-
Excellent! I remember reading about it but when I started testing I forgot where to look. – Mark.ewd Sep 12 '14 at 19:59
-
6Where should RepositoryRestConfiguration.setReturnBodyOnCreate(true) be called? – Splaktar Sep 28 '14 at 04:36
Example with Spring Boot:
@Configuration
@EnableMongoRepositories
@Import(RepositoryRestMvcConfiguration.class)
@EnableAutoConfiguration
public class Application {
public static void main(String[] args) {
ConfigurableApplicationContext ctx = SpringApplication.run(Application.class, args);
RepositoryRestConfiguration restConfiguration = ctx.getBean(RepositoryRestConfiguration.class);
restConfiguration.setReturnBodyOnCreate(true);
}
}
or
@Configuration
@EnableMongoRepositories
@EnableAutoConfiguration
public class Application extends RepositoryRestMvcConfiguration {
public static void main(String[] args) {
SpringApplication.run(Application.class, args);
}
@Override
protected void configureRepositoryRestConfiguration(RepositoryRestConfiguration config) {
super.configureRepositoryRestConfiguration(config);
config.setReturnBodyOnCreate(true);
}
}
Good Luck!

- 36,676
- 11
- 141
- 113
-
5configureRepositoryRestConfiguration deprecated...since 2.4, implement RepositoryRestConfigurer.configureRepositoryRestConfiguration(RepositoryRestConfiguration) either directly or extend RepositoryRestConfigurerAdapter and override the method. – bora.oren Apr 14 '16 at 07:44
If you are using Spring Boot, you can add the following lines to your application.properties
file for POST
(create) and PUT
(update) respectively
spring.data.rest.return-body-on-create=true
spring.data.rest.return-body-on-update=true

- 561
- 1
- 5
- 12
Here's another variant that uses DI rather than extending RepositoryRestMvcConfiguration or using the ConfigurableApplicationContext.
@SpringBootApplication
@EnableConfigurationProperties
@Configuration
@ComponentScan
public class Application {
public static void main(String[] args) {
SpringApplication.run(Application.class, args);
}
@Autowired private RepositoryRestConfiguration repositoryRestConfiguration;
@PostConstruct
public void exposeIds() {
this.repositoryRestConfiguration.setReturnBodyForPutAndPost(true);
}
}

- 1,975
- 1
- 23
- 31
-
is this alternative any better then Andrey's solution, or it's really just an alternative? I mean, is there any gain using either of them? – Sidney de Moraes Jan 27 '16 at 11:06