Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to configure Spring Data REST to return the representation of the resource created for a POST request?

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.

like image 514
Mark.ewd Avatar asked Sep 10 '14 21:09

Mark.ewd


People also ask

Is used for exposing spring data repositories over REST using Spring data REST?

Spring Data REST can be used to expose HATEOAS RESTful resources around Spring Data repositories. Without writing a lot of code, we can expose RESTful API around Spring Data Repositories.

What does the @RepositoryRestResource annotation do?

@RepositoryRestResource is used to set options on the public Repository interface - it will automatically create endpoints as appropriate based on the type of Repository that is being extended (i.e. CrudRepository/PagingAndSortingRepository/etc).

How does Spring data REST work?

Spring Data REST is a framework that builds itself on top of the applications data repositories and expose those repositories in the form of REST endpoints. In order to make it easier for the clients to discover the HTTP access points exposed by the repositories, Spring Data REST uses hypermedia driven endpoints.


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.

like image 109
Oliver Drotbohm Avatar answered Nov 22 '22 14:11

Oliver Drotbohm


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!

like image 33
Andrew Avatar answered Nov 22 '22 14:11

Andrew


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
like image 45
Strengthiness Avatar answered Nov 22 '22 14:11

Strengthiness


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);
    }
}
like image 28
Bruce Edge Avatar answered Nov 22 '22 15:11

Bruce Edge