Spring integration 如何让Spring Integration Http.inboundGateway返回HAL Json?
我将以下属性设置为true“spring.hateoas.use hal作为默认json媒体类型”&添加了org.springframework.boot:spring boot starter hateoas作为依赖项 代码Spring integration 如何让Spring Integration Http.inboundGateway返回HAL Json?,spring-integration,spring-hateoas,spring-integration-http,Spring Integration,Spring Hateoas,Spring Integration Http,我将以下属性设置为true“spring.hateoas.use hal作为默认json媒体类型”&添加了org.springframework.boot:spring boot starter hateoas作为依赖项 代码 @Bean public IntegrationFlow myUserFlow() { return IntegrationFlows .from(Http.inboundGateway("/user")
@Bean
public IntegrationFlow myUserFlow() {
return IntegrationFlows
.from(Http.inboundGateway("/user")
.requestMapping(r -> r.methods(HttpMethod.GET))
.get()
)
.handle((payload, headers) -> new MyUser("Joe Blogs")
.add(Link.of("http://localhost:8080/account", LinkRelation.of("account"))))
.get();
}
GET http://localhost:8080/user
HTTP/1.1 200
connection: Keep-Alive, keep-alive
Content-Type: application/hal+json;charset=UTF-8
Transfer-Encoding: chunked
Date: Mon, 11 Jan 2021 18:31:13 GMT
Keep-Alive: timeout=60
{
"name": "Joe Blogs",
"links": [
{
"rel": "account",
"href": "http://localhost:8080/account"
}
]
}
Response code: 200; Time: 19ms; Content length: 87 bytes
响应
@Bean
public IntegrationFlow myUserFlow() {
return IntegrationFlows
.from(Http.inboundGateway("/user")
.requestMapping(r -> r.methods(HttpMethod.GET))
.get()
)
.handle((payload, headers) -> new MyUser("Joe Blogs")
.add(Link.of("http://localhost:8080/account", LinkRelation.of("account"))))
.get();
}
GET http://localhost:8080/user
HTTP/1.1 200
connection: Keep-Alive, keep-alive
Content-Type: application/hal+json;charset=UTF-8
Transfer-Encoding: chunked
Date: Mon, 11 Jan 2021 18:31:13 GMT
Keep-Alive: timeout=60
{
"name": "Joe Blogs",
"links": [
{
"rel": "account",
"href": "http://localhost:8080/account"
}
]
}
Response code: 200; Time: 19ms; Content length: 87 bytes
我希望以HAL格式返回响应,例如
{
"name": "Joe Blogs",
"_links": [
{
"account":{
"href": "http://localhost:8080/account"
}
}
]
}
为什么不是这样?我怎样才能做到
示例应用程序:解决方案如下:
public IntegrationFlow myUserFlow(RequestMappingHandlerAdapter requestMappingHandlerAdapter) {
return IntegrationFlows
.from(Http.inboundGateway("/user")
.messageConverters(requestMappingHandlerAdapter.getMessageConverters().toArray(HttpMessageConverter[]::new))
问题是因为SpringIntegrationHTTP通道适配器不是由SpringBoot自动配置的,它们肯定不知道您的HAL定制
因此,我们需要等待,直到Spring Boot和hateoas定制处理了RequestMappingHandlerAdapter
。然后我们将其转换器注入我们的Http.inboundGateway()
我想说的是,我们可能会考虑在Spring Boot中使用一些自动的工具来实现定制,但是因为我们实际上并没有讨论一些基础设施,比如MVC的@RequestMapping
,而是一些具体的bean,所以最好还是坚持使用显式配置
我不确定为什么我们不能改用
HttpMessageConverters
,但看起来像是requestmappinghandleadapter
以后配置的,因为这种bean(integrationflow
)已经解析和创建了。谢谢你