- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我正在使用 spring-boot-2.2.1 和 spring-HATEOAS。超媒体链接工作正常,但是我在返回链接时看到 _embedded
属性,请在 github here 中查找以下代码以供引用和项目,
端点:
a) 将返回 CollectionModel
=> localhost:8099/api/v1/capability/list/noembedded
和
b) 将返回 List< EntityModel< Capability>> localhost:8099/api/v1/capability/list/
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.2.1.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>com.example</groupId>
<artifactId>spring-boot-unittest</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>spring-boot-unittest</name>
<description>Demo project for Spring Boot unit test</description>
<properties>
<java.version>1.8</java.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-hateoas</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-devtools</artifactId>
<scope>runtime</scope>
<optional>true</optional>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<optional>true</optional>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
<exclusions>
<exclusion>
<groupId>org.junit.vintage</groupId>
<artifactId>junit-vintage-engine</artifactId>
</exclusion>
</exclusions>
</dependency>
<dependency>
<groupId>org.springframework.data</groupId>
<artifactId>spring-data-mongodb</artifactId>
</dependency>
<dependency>
<groupId>com.querydsl</groupId>
<artifactId>querydsl-mongodb</artifactId>
<version>4.1.4</version>
</dependency>
<dependency>
<groupId>com.querydsl</groupId>
<artifactId>querydsl-apt</artifactId>
<version>4.1.4</version>
</dependency>
<!-- Embedded MongoDB for Testing -->
<dependency>
<groupId>de.flapdoodle.embed</groupId>
<artifactId>de.flapdoodle.embed.mongo</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.springdoc</groupId>
<artifactId>springdoc-openapi-ui</artifactId>
<version>1.2.21</version>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
<plugin>
<groupId>com.mysema.maven</groupId>
<artifactId>apt-maven-plugin</artifactId>
<version>1.1.3</version>
<executions>
<execution>
<goals>
<goal>process</goal>
</goals>
<configuration>
<outputDirectory>target/generated-sources/java</outputDirectory>
<!--<processor>com.querydsl.mongodb.morphia.MorphiaAnnotationProcessor</processor>-->
<processor>org.springframework.data.mongodb.repository.support.MongoAnnotationProcessor</processor>
</configuration>
</execution>
</executions>
</plugin>
</plugins>
</build>
</project>
Controller .Java
@RestController
@RequestMapping(value = "/api/v1/capability")
@RequiredArgsConstructor
@CrossOrigin
public class CapabilityController {
private final CapabilityService capabilityService;
private final CapabilityResourceAssembler capabilityResourceAssembler;
@GetMapping(value = "/list")
public CollectionModel<EntityModel<Capability>> getAllCapabilities() {
List<EntityModel<Capability>> capabilities = capabilityService.listCapabilities().stream()
.map(capability -> new EntityModel<>(capability,
linkTo(methodOn(CapabilityController.class).getCapabilityById(capability.getId())).withRel("getThisCapability"),
linkTo(methodOn(CapabilityController.class).getAllCapabilities()).withRel("getAllCapabilities")
)).collect(Collectors.toList());
return new CollectionModel<>(capabilities);
}
}
实际响应
{
"_embedded": {
"capabilityList": [
{
"id": "sample",
"techStack": "Java",
"numOfDevelopers": 25,
"numOfAvailableDevelopers": 10,
"_links": {
"getThisCapability": {
"href": "http://localhost:8099/api/v1/capability/sample"
},
"getAllCapabilities": {
"href": "http://localhost:8099/api/v1/capability/list"
},
"deleteThisCapability": {
"href": "http://localhost:8099/api/v1/capability/sample"
},
"createCapability": {
"href": "localhost:8099/api/v1/capability"
}
}
}
]
}
}
预期响应:
[
{
"id": "sample",
"techStack": "Java",
"numOfDevelopers": 25,
"numOfAvailableDevelopers": 10,
"_links": {
"getThisCapability": {
"href": "http://localhost:8099/api/v1/capability/sample"
},
"getAllCapabilities": {
"href": "http://localhost:8099/api/v1/capability/list"
}
}
}
]
我试过了
spring.data.rest.defaultMediaType = application/json
spring.hateoas.use-hal-as-default-json-media-type=false
但运气不佳,我仍然能够在响应中看到 _embedded
属性。谁能帮我找出问题所在。
我之前使用的是spring-boot-1.5.10
,我能够在没有_embedded
的情况下正确渲染链接,请参阅here .
在主类中添加以下注释后,如果我返回列表,它工作正常
@EnableHypermediaSupport(type = EnableHypermediaSupport.HypermediaType.HAL)
本地主机:9771/api/v1/capability/list
这会产生以下结果:
[
{
"capabilityId": "sample",
"techStack": "Java",
"numOfDevelopers": 25,
"numOfAvailableDevelopers": 10,
"_links": {
"getThisCapability": {
"href": "http://localhost:9771/api/v1/capability/sample"
},
"getAllCapabilities": {
"href": "http://localhost:9771/api/v1/capability/list"
}
}
}
]
不幸的是,它在最新版本中不起作用。任何帮助将不胜感激。
最佳答案
在使用 Spring Boot 1.5 时,您依赖于 Spring HATEOAS 的限制,这会导致旨在特定于 HAL 的 Jackson ObjectMapper
被用作应用程序范围的 ObjectMapper
。这种污染意味着 HAL 格式在不应该的情况下被应用于响应。
该限制已在 Spring HATEOAS 1.0 中得到解决,其特定于 HAL 的 ObjectMapper
不再污染整个应用程序。如果您希望主应用程序 ObjectMapper
应用 HAL 样式序列化,您可以通过自定义选择重新加入:
@Bean
public ObjectMapper objectMapper(Jackson2ObjectMapperBuilder builder,
HypermediaMappingInformation mappingInformation) {
ObjectMapper objectMapper = builder.build();
mappingInformation.configureObjectMapper(objectMapper);
return objectMapper;
}
虽然我认为上述方法可行,但我会回应 Daniel 的担忧。在他们的回答中提出了有关响应格式和 HAL 规范合规性的问题。
关于java - 使用 spring-boot-2.2.1 删除 HATEOAS 链接中的 _embedded,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59901818/
使用很棒的 HATEOAS 链接功能我正在尝试输出模板化的 url 以突出显示用户可用的过滤器参数 示例 Controller 方法 @RequestMapping(value = "/persons
我正在使用 spring-hateoas:0.18.0.RELEASE 与 spring-boot:1.2.5.RELEASE 为了调用我的 Web 服务并通过 HAL 链接,我正在使用 特拉弗森客户
我使用 Spring HATEOAS 在我的应用程序中创建 REST HATEOAS API。到目前为止它运行良好,但当涉及到嵌套资源时我陷入困境。将此类层次结构映射到 REST HATEOAS 资源
我想弄清楚如何使用 templated: true 建立 HAL 链接.如果我使用 BasicLinkBuilder.linkToCurrentMapping().slash("api/public/
我们使用 HATEOAS 取得了很好的效果,但是我们一直在关注性能,并且从链接的构建中得到了非常糟糕的结果,即看起来像这样的代码 resource.add(linkTo(methodOn(SomeCo
我有一个带有请求参数的方法,我正在尝试从另一个资源链接到这个方法。我希望链接是这样的: "rel":{ "href":".../resources{?param}", "templated":
我想链接到具有以下签名的方法: public SomeResponse getSomeObjects(@RequestParam(value = "foo", defaultValue = "bar"
我正在构建一个REST API。我有一个由bean组成的域模型,无法扩展ResourceSupport。使用Spring-HATEOAS将它们公开为资源的最佳方法是什么? 如果不可能,在bean生成的
我有这个问题已经在我的脑海里盘旋了一段时间。让我们假设我们已经在不同的层上构建了我们的项目,后端和前端。所以,从前端,我们想要一个客户,它来自 hal+json格式: GET /customers/1
我有一个关于 Spring HATEOAS 的表示模型处理器的问题。我们正在尝试在将模型序列化给客户端之前对其进行处理。我们的用例是丰富 imageUrl领域UserModel对象在运行时,因为我们必
想象一下,我有一个完全实现的REST API,它也提供了HATEOAS。 假设我浏览了根目录,除了自我链接之外,还返回了其他两个链接(例如,一个用于/users和一个用于/orders)。据我所知,H
据说在定义良好的 RESTful 系统中,客户端只需要知道根 URI 或几个众所周知的 URI,客户端将通过这些初始 URI 发现所有其他链接。我确实理解这种方法的好处(解耦客户端),但对我来说不利的
遵循 HATEOAS 原则,每个状态都应该是超链接的,对改变资源状态的链接进行建模的最佳方法是什么? 让我们以订单为例: { id : 12, state: 'pending', .
我试图对 HATEOAS 有一个清晰、简洁的理解,而且我绝不是 WRT REST 专家。 (我想我明白了,感谢这个 http://www.looah.com/source/view/2284 )。 有
我有理由相信我了解 HATEOAS 设计的服务器端 - 在响应中返回状态 URL - 但我对如何设计客户端来接受这些感到有点困惑。 例如,我们在//somehost.com/resource/1 访问
我正在阅读有关 spring-hateoas 的内容,看起来 1.0 有很多变化。我想切换到 1.0 但是我的 pom.xml 正在引入 0.25.2。 我试过用 org
我正在努力了解 HATEOAS。 让我们通过一个例子来工作。客户端将浏览器加载到 getemails.com。为简单起见,让我们假设对 getemails.com 的调用访问了服务器并返回一个电子邮件
我有 Spring Data Rest 和 Hateoas 作为我的支持。它在代理后面。 后端网址:backend.com 代理网址:proxy.com 当我查询代理网址时,例如http://prox
我可以有一些 HATEOAS 的示例/典型用例吗? ?我同意它可以是一个非常强大的概念,提供了很大的灵活性,但我不确定如何正确地从 HATEOAS 中受益。如果您可以分享您的经验/用例,那就太好了。
HATEOAS(作为应用程序状态引擎的超媒体)建议是否暗示查询字符串不是 RESTful? 编辑:下面建议查询字符串可能与状态没有太大关系,因此这个问题令人费解。我建议 URI 具有查询字符串没有意义
我是一名优秀的程序员,十分优秀!