网站的设计方案,wordpress首页html代码,梵客家装,包子店vi设计在我以前的博客文章中#xff0c;我展示了如何使用Spring Boot配置Jersey多么容易。 我对Spring Boot和Jersey的探索并没有结束#xff0c;我研究了在Spring Boot应用程序中将Spring HATEOAS和Jersey一起使用的可能性。 Spring HATEOS允许创建遵循HATEOAS原理的REST表示形式我展示了如何使用Spring Boot配置Jersey多么容易。 我对Spring Boot和Jersey的探索并没有结束我研究了在Spring Boot应用程序中将Spring HATEOAS和Jersey一起使用的可能性。 Spring HATEOS允许创建遵循HATEOAS原理的REST表示形式并且截至撰写本文时具有使用链接的基本JAX-RS支持。 在这篇博客中我将分享一些示例说明如何将Spring HATEOAS与Jersey集成到Spring Boot应用程序中。 介绍 作为本文的基础我使用了之前创建的示例 https://github.com/kolorobot/spring-boot-jersey-demo 。 为了开始使用Spring HATEOAS我在build.gradle中添加了有效的依赖build.gradle compile(org.springframework.hateoas:spring-hateoas:0.16.0.RELEASE)使用 生成实体对象 Customer 表示的最快方法是使用Spring HATEOAS Resource和Resources助手。 后者包装了CustomerRepository返回的实体的集合。 为了生成链接我使用了JaxRsLinkBuilder 它通过基于Path注释发现路径来帮助构建到JAX-RS资源的资源链接。 Component
Path(/customer)
Produces(MediaType.APPLICATION_JSON)
public class CustomerController {Injectprivate CustomerRepository customerRepository;GETpublic Response findAll() {ResourcesCustomer resources new Resources(customerRepository.findAll(),JaxRsLinkBuilder.linkTo(CustomerController.class).withSelfRel());return Response.ok(resources).build();} 调用上述方法的结果将是带有自相关链接的收集资源 {links: [{rel: self,href: http://localhost:8080/customer}],content: [{id: 1,firstname: Dave,lastname: Matthews,emailAddress: {value: davedmband.com}}]
}使用 Resource Resources PagedResources帮助器非常方便但是在某些情况下需要对创建的资源进行更多控制。 要从实体创建自定义传输对象可以使用ResourceSupport基类 public class CustomerResource extends ResourceSupport {private String fullName;private String email;} 要从实体组装CustomerResource并自动向其添加自相关链接应使用ResourceAssemblerSupport类。 基本上此类负责实例化资源并添加具有rel自指向该资源的链接 public class CustomerResourceAssembler extends ResourceAssemblerSupportCustomer, CustomerResource {public CustomerResourceAssembler() {super(CustomerController.class, CustomerResource.class);}Overridepublic CustomerResource toResource(Customer entity) {CustomerResource resource createResourceWithId(entity.getId(),entity);// initialize the resource return resource;}
} 我在上面的代码中遇到的问题是ResourceAssemblerSupport类在内部使用链接构建器来构建到Spring MVC控制器 ControllerLinkBuilder 的链接。 这导致链接无效。 除了创建从ResourceAssemblerSupport扩展并覆盖其父级行为的新支持类外我没有发现其他方法 public abstract class JaxRsResourceAssemblerSupportT, D extends ResourceSupportextends ResourceAssemblerSupportT, D {private final Class? controllerClass;public JaxRsResourceAssemblerSupport(Class? controllerClass, ClassD resourceType) {super(controllerClass, resourceType);this.controllerClass controllerClass;}Overrideprotected D createResourceWithId(Object id, T entity, Object... parameters) {Assert.notNull(entity);Assert.notNull(id);D instance instantiateResource(entity);instance.add(JaxRsLinkBuilder.linkTo(controllerClass, parameters).slash(id).withSelfRel());return instance;}
} 我真的不喜欢上面的解决方案因为我需要复制和粘贴一些代码但是我没有找到实现我想要的更好的方法。 我的汇编器现在从新创建的JaxRsResourceAssemblerSupport public class CustomerResourceAssembler extends JaxRsResourceAssemblerSupportCustomer, CustomerResource {} 最后我可以修改控制器的方法以返回由我的汇编器汇编的资源。 请注意 ResourceAssemblerSupport提供了方便的方法来将所有给定的实体转换为资源 GET
Path(/resources)
public Response findAll() {IterableCustomer customers customerRepository.findAll();CustomerResourceAssembler assembler new CustomerResourceAssembler();ListCustomerResource resources assembler.toResources(customers);return Response.ok(wrapped).build();
} 要添加具有自相关链接的集合资源链接我需要使用前面提到的Resources类包装它 // wrap to add link
ResourcesCustomerResource wrapped new Resources(resources);
wrapped.add(JaxRsLinkBuilder.linkTo(CustomerController.class).withSelfRel()
); 现在返回的表示看起来更像是HATEOAS {links: [{rel: self,href: http://localhost:8080/customer}],content: [{fullName: Matthews, Dave,email: davedmband.com,links: [{rel: self,href: http://localhost:8080/customer/1}]}]
}使用 EntityLinks接口提供API以根据实体类型创建链接并且EnableEntityLinks或EnableHypermadiaSupport与ExposesResourceFor一起使用时可用于依赖项注入。 ExposesResourceFor公开Spring MVC控制器或JAX-RS资源管理的实体类型。 在配置类中我们需要激活实体链接 SpringBootApplication
EnableEntityLinks
public class Application {} 注意请注意使用实体链接和EnableEntityLinks 以下依赖项必须位于类路径上 compile(org.springframework.plugin:spring-plugin-core:1.1.0.RELEASE) 任何支持实体类型的JAX-RS资源都必须使用ExposesResourceFor进行标记以便可以注入EntityLinks ExposesResourceFor(Customer.class)
public class CustomerController {Injectprivate EntityLinks entityLinks;
} 基本上 EntityLinks接口提供了返回链接到集合资源或单个资源的方法。 例 Link selfRel entityLinks.linkToSingleResource(Customer.class, customer.getId()
).withSelfRel();摘要 Spring HATEOAS不是使用JAX-RS和Jersey构建HATEOAS API的唯一选择但是有可能在Spring Boot应用程序中使用JerseySpring HATEOAS可能是不错的补充尤其是考虑到JAX-RS的设计。 注意本文只是我针对所述主题进行的一项研究。 我尚未在任何项目中使用该方法。 资源资源 项目源代码 https : //github.com/kolorobot/spring-boot-jersey-demo SpringHATEOAS项目页面 https //github.com/spring-projects/spring-hateoas和示例 https : //github.com/olivergierke/spring-hateoas-sample 翻译自: https://www.javacodegeeks.com/2015/01/building-a-hateoas-api-with-jax-rs-and-spring.html