如何在自定义 Spring Data Rest 控制器中正确处理 POST?
how to properly handle POST in a custom Spring Data Rest controller?
在我的 Spring Data Rest 应用程序中,我有一个标准存储库:
@RepositoryRestResource(collectionResourceRel = "people", path = "people")
public interface PersonRepository extends PagingAndSortingRepository<Person, Long> {
List<Person> findByLastName(@Param("name") String name);
}
我还有一个自定义控制器,它将根据 HTTP POST:
实现一些额外的逻辑
@RestController
@RequestMapping("/people")
public class PersonController {
@RequestMapping(value = "/**", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_VALUE)
public ResponseEntity<String> savePerson(@RequestBody Person person, UriComponentsBuilder b, @RequestParam Map<String, ?> id) {
UriComponents uriComponents =
b.path("/people/").buildAndExpand();
HttpHeaders responseHeaders = new HttpHeaders();
responseHeaders.setLocation(uriComponents.toUri());
responseHeaders.set("MyResponseHeader", "MyValue");
return new ResponseEntity<String>("Hello World\n\n", responseHeaders, HttpStatus.CREATED);
}
}
在这个控制器中保存我的 "Person" 实体的正确方法是什么,因为我没有明确使用 Hibernate 实体管理器?
"person" 参数只是一个 POJO,因此它没有任何持久性 CRUD 方法。
如果 PersonRepository 中使用的 Person class 与您在控制器中将 RequestBody 映射到的任何对象相同,那么在控制器方法中您可以只执行 personRepository.save(person) - - 假设 personRepository 是 PersonRepository class.
的自动装配实例
我猜你正在试验 spring 数据剩余 https://spring.io/guides/gs/accessing-data-rest/ 。如果是这种情况,您的 class 路径中可能有内存数据库 com.h2database:h2。这就是为什么在给定的示例中,一切都在您甚至没有配置数据库或向您的人员添加任何 JPA 注释的情况下正常工作 class。因此,您仍然可以从您的自定义控制器执行 personRepository.save(person) 而无需在您的 Person class.
中添加任何 JPA 注释
在我的 Spring Data Rest 应用程序中,我有一个标准存储库:
@RepositoryRestResource(collectionResourceRel = "people", path = "people")
public interface PersonRepository extends PagingAndSortingRepository<Person, Long> {
List<Person> findByLastName(@Param("name") String name);
}
我还有一个自定义控制器,它将根据 HTTP POST:
实现一些额外的逻辑@RestController
@RequestMapping("/people")
public class PersonController {
@RequestMapping(value = "/**", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_VALUE)
public ResponseEntity<String> savePerson(@RequestBody Person person, UriComponentsBuilder b, @RequestParam Map<String, ?> id) {
UriComponents uriComponents =
b.path("/people/").buildAndExpand();
HttpHeaders responseHeaders = new HttpHeaders();
responseHeaders.setLocation(uriComponents.toUri());
responseHeaders.set("MyResponseHeader", "MyValue");
return new ResponseEntity<String>("Hello World\n\n", responseHeaders, HttpStatus.CREATED);
}
}
在这个控制器中保存我的 "Person" 实体的正确方法是什么,因为我没有明确使用 Hibernate 实体管理器? "person" 参数只是一个 POJO,因此它没有任何持久性 CRUD 方法。
如果 PersonRepository 中使用的 Person class 与您在控制器中将 RequestBody 映射到的任何对象相同,那么在控制器方法中您可以只执行 personRepository.save(person) - - 假设 personRepository 是 PersonRepository class.
的自动装配实例我猜你正在试验 spring 数据剩余 https://spring.io/guides/gs/accessing-data-rest/ 。如果是这种情况,您的 class 路径中可能有内存数据库 com.h2database:h2。这就是为什么在给定的示例中,一切都在您甚至没有配置数据库或向您的人员添加任何 JPA 注释的情况下正常工作 class。因此,您仍然可以从您的自定义控制器执行 personRepository.save(person) 而无需在您的 Person class.
中添加任何 JPA 注释