[英]How to assign a @Projection to a @GetMapping spring servlet endpoint?
I have an @Entity Person
class, and want to expose that via a webservice. 我有一个
@Entity Person
类,并且想通过Web服务公开它。 There should be a method just exposing all details, and and endpoint only exposing a view excerpt. 应该有一个只公开所有细节的方法,而端点只公开一个视图摘录。
Can I use Spring @Projection
for that purpose without having to manually extract the fields I want to expose? 我可以为此使用Spring
@Projection
而不用手动提取要公开的字段吗? I'd prefer just returning a List<Person>
but render only certain details for certain endpoints. 我宁愿只返回
List<Person>
但仅呈现某些端点的某些详细信息。
@RestController
public class BookingInfoServlet {
@Autowired
private PersonRepository dao;
@GetMapping("/persons")
public List<Person> persons() {
return dao.findAll();
}
//TODO how can I assign the Projection here?
@GetMapping("/personsView")
public List<Person> persons() {
return dao.findAll();
}
//only expose certain properties
@Projection(types = Person.class)
public interface PersonView {
String getLastname();
}
}
@Entity
public class Person {
@id
long id;
String firstname, lastname, age, etc;
}
interface PersonRepository extends CrudRepository<Person, Long> {
}
Note that @Projection
only works with spring data rest. 请注意,@
@Projection
仅适用于spring数据其余部分。 I believe you could try this: 我相信您可以尝试以下方法:
@Projection(name = "personView", types = Person.class)
public interface PersonView {
String getLastname();
}
And on your repo, you need something like this: 在您的仓库中,您需要这样的东西:
@RepositoryRestResource(excerptProjection = PersonView.class)
interface PersonRepository extends CrudRepository<Person, Long> {
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.