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

后端 未结 4 1763
粉色の甜心
粉色の甜心 2021-02-07 03:12

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 w

4条回答
  •  不思量自难忘°
    2021-02-07 04:11

    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);
        }
    }
    

提交回复
热议问题