相关疑难解决方法(0)

如何混合Spring Data Repositories和Spring Rest Controller

目前,我通过使用@RepositoryRestResource注释它们来将一些Spring数据库公开为RESTful服务,如下所示:

@RepositoryRestResource(collectionResourceRel = "thing1", path = "thing1")
public interface Thing1Repository extends PagingAndSortingRepository<Thing1, String> {}

@RepositoryRestResource(collectionResourceRel = "thing2", path = "thing2")
public interface Thing2Repository extends CrudRepository<Thing2, String> {}
Run Code Online (Sandbox Code Playgroud)

一切都很好.当你点击我的第一个端点时,还会显示我公开的所有Spring Data Repositories,如下所示:

{
   _links: {
      thing1: {
         href: "http://localhost:8080/thing1{?page,size,sort}",
         templated: true
      },
      thing2: {
         href: "http://localhost:8080/thing2"
      }
   }
}
Run Code Online (Sandbox Code Playgroud)

现在我有一些我想公开的端点,这些端点无法由Spring Data Repositories表示,所以我使用的是RestController.

这是一个简单的例子:

@RestController
@ExposesResourceFor(Thing3.class)
@RequestMapping("/thing3")
public class Thing3Controller {

  @Autowired 
  EntityLinks entityLinks;

  @Autowired 
  Thing3DAO thing3DAO;

  //just assume Thing3.class extends ResourceSupport. I know this is wrong, but it makes the example shorter …
Run Code Online (Sandbox Code Playgroud)

java spring spring-data spring-data-rest spring-hateoas

14
推荐指数
1
解决办法
1万
查看次数

如何在Spring Data REST中添加指向root资源的链接?

如何在Spring Data REST 资源根列表中公开外部资源(不通过存储库管理)?我在Restbucks中按照模式定义了一个控制器

spring-data-rest

13
推荐指数
1
解决办法
6126
查看次数

Canonical _links with Spring HATEOAS

我们正在构建一个类似于spring.io指南" 使用REST访问JPA数据 "的RESTful Web服务.要重现下面的示例输出,只需向Person添加ManyToOne -Relation,如下所示:

// ...

@Entity
public class Person {

  @Id
  @GeneratedValue(strategy = GenerationType.AUTO)
  private long id;

  private String firstName;
  private String lastName;

  @ManyToOne
  private Person father;

  // getters and setters
}
Run Code Online (Sandbox Code Playgroud)

添加一些样本数据后的GET请求产生:

{
  "firstName" : "Paul",
  "lastName" : "Mustermann",
  "_links" : {
    "self" : {
      "href" : "http://localhost:8080/people/1"
    },
    "father" : {
      "href" : "http://localhost:8080/people/1/father"
    }
  }
}
Run Code Online (Sandbox Code Playgroud)

但是,鉴于保罗的父亲存有ID 2,我们期望的结果将是其关系的规范网址:

// ...
    "father" : {
      "href" : "http://localhost:8080/people/2"
    }
// ...
Run Code Online (Sandbox Code Playgroud)

如果对某些人是空的,这当然会引起问题(好吧,这在这里没有多大意义......;)),但在这种情况下,我们根本不想在JSON中呈现链接. …

spring spring-data-jpa spring-data-rest spring-hateoas

5
推荐指数
1
解决办法
2879
查看次数

Spring Data Rest将链接添加到根-RepositoryLinksResource无法转换为Resource

我正在尝试按照以下答案添加到API的根文档的自定义链接: 根请求的自定义响应int带有RepositoryRestResource-s和常规控制器的Spring REST HATEOAS

这是我的自定义资源处理器:

import org.springframework.data.rest.webmvc.RepositoryLinksResource;
import org.springframework.hateoas.Link;
import org.springframework.hateoas.ResourceProcessor;
import org.springframework.hateoas.mvc.ControllerLinkBuilder;
import org.springframework.stereotype.Component;

@Component
public class CustomRootLinksResourceProcessor implements ResourceProcessor<RepositoryLinksResource> {

    @Override
    public RepositoryLinksResource process(RepositoryLinksResource resource) {
         Link link = ControllerLinkBuilder
                .linkTo(ControllerLinkBuilder
                        .methodOn(CustomerController.class)
                        .register(null))
                .withRel("registerCustomer");

        resource.add(link);

        return resource;
    }
}
Run Code Online (Sandbox Code Playgroud)

但是,当我查询API时,会收到以下异常

LOGBACK:19:01:03.425 [http-nio-8080-exec-2] ERROR o.s.d.r.w.RepositoryRestExceptionHandler - org.springframework.data.rest.webmvc.RepositoryLinksResource cannot be cast to org.springframework.hateoas.Resource
java.lang.ClassCastException: org.springframework.data.rest.webmvc.RepositoryLinksResource cannot be cast to org.springframework.hateoas.Resource
    at org.springframework.hateoas.mvc.ResourceProcessorInvoker$DefaultProcessorWrapper.invokeProcessor(ResourceProcessorInvoker.java:224)
    at org.springframework.hateoas.mvc.ResourceProcessorInvoker.invokeProcessorsFor(ResourceProcessorInvoker.java:141)
    at org.springframework.hateoas.mvc.ResourceProcessorInvoker.invokeProcessorsFor(ResourceProcessorInvoker.java:124)
    at org.springframework.hateoas.mvc.ResourceProcessorHandlerMethodReturnValueHandler.handleReturnValue(ResourceProcessorHandlerMethodReturnValueHandler.java:113)
    at org.springframework.web.method.support.HandlerMethodReturnValueHandlerComposite.handleReturnValue(HandlerMethodReturnValueHandlerComposite.java:81)
    at org.springframework.web.servlet.mvc.method.annotation.ServletInvocableHandlerMethod.invokeAndHandle(ServletInvocableHandlerMethod.java:132)
    at org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter.invokeHandlerMethod(RequestMappingHandlerAdapter.java:827)
    at org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter.handleInternal(RequestMappingHandlerAdapter.java:738)
    at org.springframework.web.servlet.mvc.method.AbstractHandlerMethodAdapter.handle(AbstractHandlerMethodAdapter.java:85)
    at org.springframework.web.servlet.DispatcherServlet.doDispatch(DispatcherServlet.java:963) …
Run Code Online (Sandbox Code Playgroud)

java spring spring-data-rest

4
推荐指数
1
解决办法
785
查看次数