Spring开机查询数据不返回怎么办?
Spring Boot how to query data without returning it?
抱歉问了这样一个新手问题!
我从 Spring Boot 的文档中知道,我可以这样做:
@RepositoryRestResource(collectionResourceRel = "people", path = "people")
public interface PersonRepository extends PagingAndSortingRepository<Person, Long> {
List<Person> findByLastName(@Param("name") String name);
}
但是如何查询相同的信息,然后对其进行某种逻辑转换,然后再将其返回给客户端?
使用findByLastName,直接返回数据库的结果。
我遇到了类似的问题,据我了解,没有方便的解决方案让我和你这样做。
您将必须实现自己的控制器并在那里解决您的所有逻辑。
查看@RepositoryRestController 并使用您自己的逻辑实现您自己的方法。
我来的解决方案 ac
@RestController
@RepositoryRestController
@RequestMapping(value = "/event/")
public class EventController {
@Autowired
EventService eventService;
@RequestMapping(value = "/upcoming", method = RequestMethod.GET)
List<EventProjection> checkIfUserParticipatesUpcoming(@RequestParam(value = "userId") String userId) {
return eventService.checkIfUserParticipatesUpcoming(userId);
}
}
像您通常在 spring 中所做的那样,在事件服务中完全实现业务逻辑。
Spring-Data-Rest
非常适合基本的东西,但它不像您希望的那样有弹性。
如果有更好的答案,我也很乐意听到。
*小记,我是个罪人。 return 类型应该是 HttpEntity
与服务器 200/201/204 的兼容 return 类型,但我只是处于开发阶段,尚未投入生产。 *
在问了这个问题之后,我随后找到了以下文档,其中概述了如何实现这一目标:
public class ScannerController {
private final ScannerRepository repository;
@Autowired
public ScannerController(ScannerRepository repo) {
repository = repo;
}
@RequestMapping(method = GET, value = "/scanners/search/listProducers")
public @ResponseBody ResponseEntity<?> getProducers() {
List<String> producers = repository.listProducers();
//
// do some intermediate processing, logging, etc. with the producers
//
Resources<String> resources = new Resources<String>(producers);
resources.add(linkTo(methodOn(ScannerController.class).getProducers()).withSelfRel());
// add other links as needed
return ResponseEntity.ok(resources);
}
}
注意:您还必须导入以下包才能使其正常工作:
import static org.springframework.hateoas.mvc.ControllerLinkBuilder.linkTo;
import static org.springframework.hateoas.mvc.ControllerLinkBuilder.methodOn;
抱歉问了这样一个新手问题!
我从 Spring Boot 的文档中知道,我可以这样做:
@RepositoryRestResource(collectionResourceRel = "people", path = "people")
public interface PersonRepository extends PagingAndSortingRepository<Person, Long> {
List<Person> findByLastName(@Param("name") String name);
}
但是如何查询相同的信息,然后对其进行某种逻辑转换,然后再将其返回给客户端?
使用findByLastName,直接返回数据库的结果。
我遇到了类似的问题,据我了解,没有方便的解决方案让我和你这样做。 您将必须实现自己的控制器并在那里解决您的所有逻辑。 查看@RepositoryRestController 并使用您自己的逻辑实现您自己的方法。 我来的解决方案 ac
@RestController
@RepositoryRestController
@RequestMapping(value = "/event/")
public class EventController {
@Autowired
EventService eventService;
@RequestMapping(value = "/upcoming", method = RequestMethod.GET)
List<EventProjection> checkIfUserParticipatesUpcoming(@RequestParam(value = "userId") String userId) {
return eventService.checkIfUserParticipatesUpcoming(userId);
}
}
像您通常在 spring 中所做的那样,在事件服务中完全实现业务逻辑。
Spring-Data-Rest
非常适合基本的东西,但它不像您希望的那样有弹性。
如果有更好的答案,我也很乐意听到。
*小记,我是个罪人。 return 类型应该是 HttpEntity
与服务器 200/201/204 的兼容 return 类型,但我只是处于开发阶段,尚未投入生产。 *
在问了这个问题之后,我随后找到了以下文档,其中概述了如何实现这一目标:
public class ScannerController {
private final ScannerRepository repository;
@Autowired
public ScannerController(ScannerRepository repo) {
repository = repo;
}
@RequestMapping(method = GET, value = "/scanners/search/listProducers")
public @ResponseBody ResponseEntity<?> getProducers() {
List<String> producers = repository.listProducers();
//
// do some intermediate processing, logging, etc. with the producers
//
Resources<String> resources = new Resources<String>(producers);
resources.add(linkTo(methodOn(ScannerController.class).getProducers()).withSelfRel());
// add other links as needed
return ResponseEntity.ok(resources);
}
}
注意:您还必须导入以下包才能使其正常工作:
import static org.springframework.hateoas.mvc.ControllerLinkBuilder.linkTo;
import static org.springframework.hateoas.mvc.ControllerLinkBuilder.methodOn;