Tag: spring data rest

如何使用Spring Data Rest和PagingAndSortingRepository处理exception?

假设我有一个类似的存储库: public interface MyRepository extends PagingAndSortingRepository { @Query(“….”) Page findByCustomField(@Param(“customField”) String customField, Pageable pageable); } 这很好用。 但是,如果客户端发送已形成的请求(例如,搜索不存在的字段),则Spring将exception作为JSON返回。 揭示@Query等 // This is OK http://example.com/data-rest/search/findByCustomField?customField=ABC // This is also OK because “secondField” is a valid column and is mapped via the Query http://example.com/data-rest/search/findByCustomField?customField=ABC&sort=secondField // This throws an exception and sends the exception to the client http://example.com/data-rest/search/findByCustomField?customField=ABC&sort=blahblah 抛出exception并发送给客户端的示例: […]

Spring Data REST如何在线添加嵌入式资源

我正在将Spring Data REST和Hateoas与HAL浏览器结合使用。 这非常有效,但是现在我想用一组特定实体对其关联对象进行JSON转储。 我使用@Projection然后再次卡住了。 仅供参考:正常行为(包含嵌入式和链接等)应保留在新端点(没有嵌入式和链接)之外。 为了进一步说明我的问题/问题: class Person { String name; List companies; } class Company { String name; Address address; } class Address { String street; } 现在我想看到这样的事情: { “name”: “John”, “companies”: [ { “name”: “Stackoverflow”, “address”: {“street”: “Highway blvd.”} }, { “name”: “Oracle”, “address”: {“street”: “Main rd.”} } ] } 虽然我得到了这个: { […]

如何在@HandleBeforeSave事件中获取旧实体值以确定属性是否已更改?

我正试图在@HandleBeforeSave事件中获取旧实体。 @Component @RepositoryEventHandler(Customer.class) public class CustomerEventHandler { private CustomerRepository customerRepository; @Autowired public CustomerEventHandler(CustomerRepository customerRepository) { this.customerRepository = customerRepository; } @HandleBeforeSave public void handleBeforeSave(Customer customer) { System.out.println(“handleBeforeSave :: customer.id = ” + customer.getId()); System.out.println(“handleBeforeSave :: new customer.name = ” + customer.getName()); Customer old = customerRepository.findOne(customer.getId()); System.out.println(“handleBeforeSave :: new customer.name = ” + customer.getName()); System.out.println(“handleBeforeSave :: old […]

在自定义@RepositoryRestController方法中填充实体链接

我使用Spring-data-rest来为一些JPA实体提供读取API。 对于写入,我需要发出Command对象而不是直接写入DB,因此我使用@RepositoryRestController和各种命令处理方法添加了一个自定义控制器: @RequestMapping(method = RequestMethod.POST) public @ResponseBody MyEntity post(@RequestBody MyEntity entity) { String createdId = commands.sendAndWait(new MyCreateCommand(entity)); return repo.findOne(createdId); } 我希望输出能够像spring-data-rest控制器的任何其他响应一样得到丰富,特别是我希望它将HATEOAS链接添加到它自身及其关系中。

如何在Spring-data-rest中将Page 映射到Page

当我使用PagingAndSortingRepository.findAll(Pageable)访问数据库时,我得到Page 。 但是,我想将DTO暴露给客户端而不是实体。 我可以通过将实体注入到它的构造函数中来创建DTO,但是如何将Page对象中的实体映射到DTO? 根据spring文档,Page提供了只读操作。 另外,Page.map不可能,因为我们不支持java 8.如何手动创建带有映射对象的新页面?

Spring Data REST不包括资源中的实体链接

由Oliver Gierke解决方案解决 看起来这是Spring 4.2.0中的已知错误,升级到4.2.1提供了预期的function 原始问题 我正在努力将我的开发团队转移到Spring + WebMVC + Data-REST + Data-JPA + Spring HATEOAS for web applications。 我目前的应用程序只是维护我们正在进行的应用程序列表。 我遇到了我的默认Spring Data REST设置问题。 我的资源不包括其特定视图中的链接资源,而它们包含在集合视图中。 我不确定这是否是预期的行为,所以我将在本文末尾包括相关的配置等。 jv.local是我的开发框,apps-list / app是spring-data-rest绑定的地方(下面包含配置) 例: curl jv.local:8080/apps-list/app/departments 返回: { “_links” : { “self” : { “href” : “http://jv.local:8080/apps-list/app/departments{?page,size,sort}”, “templated” : true } }, “_embedded” : { “departments” : [ { “name” : […]

Spring Data Data中的分页用于嵌套资源

访问以下url时,我会得到分页作为回应 /api/userPosts/ { “_links” : { “self” : { “href” : “/api/userPosts{?page,size,sort}”, “templated” : true }, “next” : { “href” : api/userPosts?page=1&size=20{&sort}”, “templated” : true } }, “_embedded” : { “userPosts” : [ { … 但是,在访问以下URL时,Spring Data REST没有开箱即用的分页 – /api/users/4/userPosts { “_embedded” : { “userPosts” : [ { UserRepository和UserPostRepository都是具有分页的JPARepository。 因此,第二个URL导致GC开销超出错误,因为返回的结果的行数很大。 @RepositoryRestResource(excerptProjection = UserProjection.class) public interface […]

调用spring数据rest存储库方法不返回链接

我有存储库“ClientRepository”: public interface ClientRepository extends PagingAndSortingRepository { } 当我请求http:// localhost:8080 / clients / 1时,服务器响应 { “algorithmId” : 1, “lastNameTxt” : “***”, “firstNameTxt” : “**”, “middleNameTxt” : “**”, “_links” : { “self” : { “href” : “http://localhost:8080/clients/1121495168” }, “client” : { “href” : “http://localhost:8080/clients/1121495168” } } } 响应具有预期的链接。 当我在另一个控制器中调用存储库inheritance的方法findOne时 @RestController public class SearchRestController { @Autowired public […]

如何避免Joda对象中的内容字段?

我在文档中使用Joda对象(DateTime和DateTimeZone),每当我通过REST接口访问它时,我会得到包含这样的字段的条目 lastAggregationDate: { content: “2016-07-12T17:58:43.643Z” } 代替 lastAggregationDate: “2016-07-12T17:58:43.643Z” 我宣布了Joda Jackson依赖项,我看到这些类型的de / serializers,所以我很困惑这里有什么工作。 我在一个稍微修改过的Spring示例项目中重复了这种行为,但是使用了Java的本机日期类型而不是Joda的。 我已经将一个出生属性的日期添加到Person对象,并修改了shouldRetrieveEntity测试以查找$.dateOfBirth.content 。 我已经确认正在使用序列化程序,看起来LocalDate对象被视为资源而不是简单属性。

解析自定义控制器中的实体URI(Spring HATEOAS)

我有一个基于spring-data-rest的项目,它也有一些自定义端点。 为了发送POST数据,我正在使用json { “action”: “REMOVE”, “customer”: “http://localhost:8080/api/rest/customers/7” } 对于spring-data-rest来说这很好,但不适用于自定义控制器。 例如: public class Action { public ActionType action; public Customer customer; } @RestController public class ActionController(){ @Autowired private ActionService actionService; @RestController public class ActionController { @Autowired private ActionService actionService; @RequestMapping(value = “/customer/action”, method = RequestMethod.POST) public ResponseEntity doAction(@RequestBody Action action){ ActionType actionType = action.action; Customer customer […]