我一直在研究如何使用Spring和REST的各种示例.我们的最终目标是Spring HATEOAS/HAL设置
我已经看到了两种在Spring中呈现REST的不同方法
通过HATEOAS/HAL
控制器内部
通过@RestController
存储库中的内容
我正在努力寻找的是你为什么要使用一个而不是另一个.在尝试实施HAL时哪个最好?
我们的数据库后端是Neo4j.
spring spring-mvc spring-data spring-data-rest spring-hateoas
我想使用REST API的HAL格式来包含嵌入式资源.我正在使用Spring HATEOAS作为我的API,Spring HATEOAS似乎支持嵌入式资源; 但是,没有关于如何使用它的文档或示例.
有人可以举例说明如何使用Spring HATEOAS来包含嵌入式资源吗?
问题是,Spring HATEOAS与Spring Data Rest之间有什么区别?
我觉得两者都可以这样做,Spring Data Rest(作为Spring Data的一部分)看起来更有活力.
https://github.com/spring-projects/spring-hateoas https://github.com/spring-projects/spring-data-rest
你什么时候使用其中一个?
我正在使用spring boot来创建REST应用程序.我有一个DTO,如下所示:
public class Subject {
private String uid;
private String number;
private String initials;
private Date dateOfBirth;
Run Code Online (Sandbox Code Playgroud)
我使用Spring-Hateos并且我的控制器的重新类型是ResponseEntity<Resources<Resource<Subject>>>
.我需要以"yyyy-mm-dd"格式显示日期.
我正在使用Spring 4.0.0.RELEASE,Spring Data Commons 1.7.0.M1,Spring Hateoas 0.8.0.RELEASE
我的资源是一个简单的POJO:
public class UserResource extends ResourceSupport { ... }
Run Code Online (Sandbox Code Playgroud)
我的资源汇编程序将User对象转换为UserResource对象:
@Component
public class UserResourceAssembler extends ResourceAssemblerSupport<User, UserResource> {
public UserResourceAssembler() {
super(UserController.class, UserResource.class);
}
@Override
public UserResource toResource(User entity) {
// map User to UserResource
}
}
Run Code Online (Sandbox Code Playgroud)
在我的UserController中,我想Page<User>
从我的服务中检索然后将其转换为PagedResources<UserResource>
使用PagedResourcesAssembler
,如下所示:https://stackoverflow.com/a/16794740/1321564
@RequestMapping(value="", method=RequestMethod.GET)
PagedResources<UserResource> get(@PageableDefault Pageable p, PagedResourcesAssembler assembler) {
Page<User> u = service.get(p)
return assembler.toResource(u);
}
Run Code Online (Sandbox Code Playgroud)
这不会调用UserResourceAssembler
,只User
返回内容而不是我的自定义UserResource
.
返回单个资源有效:
@Autowired
UserResourceAssembler …
Run Code Online (Sandbox Code Playgroud) 我将我的项目从 spring-boot 2.1.9 移到了 2.2.0。
在启动项目时,我面临以下error
消息。
Caused by: org.springframework.beans.factory.NoUniqueBeanDefinitionException: No qualifying bean of type 'org.springframework.plugin.core.PluginRegistry<org.springframework.hateoas.client.LinkDiscoverer, org.springframework.http.MediaType>' available: expected single matching bean but found 17: modelBuilderPluginRegistry,modelPropertyBuilderPluginRegistry,typeNameProviderPluginRegistry,syntheticModelProviderPluginRegistry,documentationPluginRegistry,apiListingBuilderPluginRegistry,operationBuilderPluginRegistry,parameterBuilderPluginRegistry,expandedParameterBuilderPluginRegistry,resourceGroupingStrategyRegistry,operationModelsProviderPluginRegistry,defaultsProviderPluginRegistry,pathDecoratorRegistry,apiListingScannerPluginRegistry,relProviderPluginRegistry,linkDiscovererRegistry,entityLinksPluginRegistry
Caused by: org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'linkDiscoverers' defined in class path resource [org/springframework/hateoas/config/HateoasConfiguration.class]: Unsatisfied dependency expressed through method 'linkDiscoverers' parameter 0; nested exception is org.springframework.beans.factory.NoUniqueBeanDefinitionException: No qualifying bean of type 'org.springframework.plugin.core.PluginRegistry<org.springframework.hateoas.client.LinkDiscoverer, org.springframework.http.MediaType>' available: expected single matching bean but found 17: modelBuilderPluginRegistry,modelPropertyBuilderPluginRegistry,typeNameProviderPluginRegistry,syntheticModelProviderPluginRegistry,documentationPluginRegistry,apiListingBuilderPluginRegistry,operationBuilderPluginRegistry,parameterBuilderPluginRegistry,expandedParameterBuilderPluginRegistry,resourceGroupingStrategyRegistry,operationModelsProviderPluginRegistry,defaultsProviderPluginRegistry,pathDecoratorRegistry,apiListingScannerPluginRegistry,relProviderPluginRegistry,linkDiscovererRegistry,entityLinksPluginRegistry
***************************
APPLICATION FAILED TO START
***************************
Description:
Parameter 0 of …
Run Code Online (Sandbox Code Playgroud) 我最近将 spring boot 从 1.x 升级到 2.y,并面临这个问题,其中 hatoas 链接是使用http
schema 而不是https
.
后来我发现,在 spring boot 2.2+ 中,必须使用以下属性
server.forward-headers-strategy=NATIVE
Run Code Online (Sandbox Code Playgroud)
其中可以有NATIVE
或FRAMEWORK
或 之一NONE
。
NONE
属性非常简单,它完全禁用前向标头的使用。
NATIVE
但是vs没有明确的文档FRAMEWORK
。我在很多地方都看到提到NATIVE
在大多数情况下效果最好。但没有解释当我们使用这些属性时幕后到底发生了什么。
这里的文档没有提供足够的信息让我在 Native/Framework 之间进行选择。它只说明谁处理相应值的转发标头。Servlet 容器?还是Spring框架?但这又回到了第 1 步。我应该让容器来处理它吗?或者框架?我什么时候应该选择其中一种而不是另一种?
我正在将 REST Web 应用程序与外部 tomcat 一起使用并Hateoas
生成链接。
我如何决定是否使用 NATIVE
或FRAMEWORK
财产?什么时候应该优先选择其中之一?为什么?
我的 springboot 版本:2.4.6
我已经尝试过的参考资料:
编辑:
我尝试了这两种解决方案并且framework
对我有用,但native
在外部 tomcat 环境中不起作用。我创建了一个新的 Spring Boot Web 应用程序,其中嵌入了 Tomcat,并且两者都native
可以 …
假设我有两个存储库:
@RepositoryRestResource(collectionResourceRel = "person", path = "person")
public interface PersonRepository extends PagingAndSortingRepository<Person, Long> {
List<Person> findByLastName(@Param("name") String name);
}
Run Code Online (Sandbox Code Playgroud)
和
@RepositoryRestResource(collectionResourceRel = "person1", path = "person1")
public interface PersonRepository1 extends PagingAndSortingRepository<Person1, Long> {
List<Person1> findByLastName(@Param("name") String name);
}
Run Code Online (Sandbox Code Playgroud)
使用一个常规控制器:
@Controller
public class HelloController {
@RequestMapping("/hello")
@ResponseBody
public HttpEntity<Hello> hello(@RequestParam(value = "name", required = false, defaultValue = "World") String name) {
Hello hello = new Hello(String.format("Hello, %s!", name));
hello.add(linkTo(methodOn(HelloController.class).hello(name)).withSelfRel());
return new ResponseEntity<>(hello, HttpStatus.OK);
}
}
Run Code Online (Sandbox Code Playgroud)
现在,回复http://localhost:8080/
是:
{
"_links" : …
Run Code Online (Sandbox Code Playgroud) 我有一个用Spring-Data制作的数据访问层.我现在正在创建一个Web应用程序.这个控制器方法应返回格式为JSON 的Spring-Data页面.
这样的页面是一个列表,其中包含额外的分页信息,例如记录总数等等.
这是可能的,如果是的话怎么样?
与此直接相关,我可以定义属性名称的映射吗?例如.意思是我需要定义如何在JSON中命名分页信息属性(与页面不同).这可能吗?怎么样?
我正在使用支持HATEOAS的Spring Data REST.我是这个范例的新手.
在GET
我的RESTful Web服务的响应中,我经常在名为的节点内收到结果_embedded
.
我想知道:什么是_embedded
节点?它是REST规范的一部分吗?或者是HATEOAS规范的一部分?或者它是否特定于Spring的实现?
这是JSON结果的示例GET http://localhost:8080/mywebservice/features
:
{
"_links":
{
"search": { "href": "http://localhost:8080/mywebservice/features/search" }
},
"_embedded":
{
"features":
[
{
"feature": "GROUND",
"name": "Terreno",
"data_type": "String",
"_links":
{
"self" : { "href": "http://localhost:8080/mywebservice/features/GROUND" },
"values": { "href": "http://localhost:8080/mywebservice/features/GROUND }
}
},
...
]
}
}
Run Code Online (Sandbox Code Playgroud)
我注意到我_embedded
在响应中几乎总是有节点:如果我请求集合,但即使通过搜索请求单个资源(例如GET http://localhost:8080/mywebservice/persons/search/findByEmail?email=example@example@.com
).
_embedded
仅当请求是针对特定资源时,我才会获取节点,例如在执行时GET http://localhost:8080/mywebservice/features/GROUND
.
spring-hateoas ×10
spring ×8
java ×6
spring-boot ×4
hateoas ×3
rest ×3
spring-data ×3
spring-mvc ×3
json ×2
pagination ×1
swagger-2.0 ×1
tomcat ×1