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.
相关问题
- java.lang.IllegalArgumentException: Cannot set to
- Design RESTful service with multiple ids
- Axios OPTIONS instead of POST Request. Express Res
- Plain (non-HTML) error pages in REST api
- Spring Data MongoDB - lazy access to some fields
相关文章
- java JDK动态代理和cglib动态代理最后获取的代理对象都为null的问题
- org.xml.sax.SAXParseException; lineNumber: 7; colu
- SpringMVC如何把File封装到Map中?
- Spring: controller inheritance using @Controller a
- How to load @Configuration classes from separate J
- Java spring framework - how to set content type?
- Java/Spring MVC: provide request context to child
- Spring 5 Web Reactive - Hot Publishing - How to us
Here's another variant that uses DI rather than extending RepositoryRestMvcConfiguration or using the ConfigurableApplicationContext.
You don't have to search for the created entity. As the HTTP spec suggests,
POST
requests returning a status code of201 Created
are supposed to contain aLocation
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 onRepositoryRestConfiguration.setReturnBodyOnCreate(…)
and….setReturnBodyOnUpdate(…)
which you can use to configure the framework to immediately return the representation of the resource just created.Example with Spring Boot:
or
Good Luck!
If you are using Spring Boot, you can add the following lines to your
application.properties
file forPOST
(create) andPUT
(update) respectively