gpt4 book ai didi

java - 使用 spring-boot-2.2.1 删除 HATEOAS 链接中的 _embedded

转载 作者:行者123 更新时间:2023-12-01 19:30:26 30 4
gpt4 key购买 nike

我正在使用 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/

30 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com