Micronaut,声明式 Http-Client,JSON 序列化包装 HTTP Post 对象
Micronaut, declarative Http-Client, JSON serialization wraps HTTP Post object
我正在使用 micronaut 编写与另一个服务通信的服务。
我正在使用声明式 http 客户端。
在发送带有声明性的请求对象时 http-client
,在序列化时,某些东西将对象包装为
的参数名称
我有一个对象需要序列化
@Getter // lombok
@Setter
@Serdeable
public class RequestObject {
private Long id;
}
http-client
使用 documentation 如下所示。 (不包括Jackson,只使用Micronaut SerDes)
@Client(id = "thirdservice")
@Header(name = ACCEPT, value = "application/vnd.github.v3+json, application/json")
public interface RemotServiceClient extends RemotServiceOperations {
@Post("/api/endpoint")
ObjectResponse createPostRequest(RequestObject requestObject); // this parameter name is used to wrap
所以当我希望客户端序列化时 RequestObject
我会假设序列化看起来像:
{ "id": "123"}
而是序列化
{ "requestObject" : { "id": "123"}}
我正在阅读有关 @JsonRootName
默认情况下禁用的内容。另外当我设置 @JsonRootName("differentValue)
时没有效果。
我猜这是 http-client
中的某种设置,我现在找不到。
任何提示表示赞赏。
此外,我想避免从对象手动序列化 json,就像描述的那样 here。
通过io.micronaut.http.annotation.Body
注解来注解方法参数:
@Post("/api/endpoint")
ObjectResponse createPostRequest(
@Body RequestObject requestObject
);
那么它将按照您的预期进行序列化:
{ "id": "123"}
文档https://docs.micronaut.io/latest/guide/index.html#clientParameters
编写服务器(控制器)时的行为相同。
在文档中更好地解释了变量分辨率:https://docs.micronaut.io/latest/guide/index.html#_variable_resolution
我正在使用 micronaut 编写与另一个服务通信的服务。
我正在使用声明式 http 客户端。
在发送带有声明性的请求对象时 http-client
,在序列化时,某些东西将对象包装为
我有一个对象需要序列化
@Getter // lombok
@Setter
@Serdeable
public class RequestObject {
private Long id;
}
http-client
使用 documentation 如下所示。 (不包括Jackson,只使用Micronaut SerDes)
@Client(id = "thirdservice")
@Header(name = ACCEPT, value = "application/vnd.github.v3+json, application/json")
public interface RemotServiceClient extends RemotServiceOperations {
@Post("/api/endpoint")
ObjectResponse createPostRequest(RequestObject requestObject); // this parameter name is used to wrap
所以当我希望客户端序列化时 RequestObject
我会假设序列化看起来像:
{ "id": "123"}
而是序列化
{ "requestObject" : { "id": "123"}}
我正在阅读有关 @JsonRootName
默认情况下禁用的内容。另外当我设置 @JsonRootName("differentValue)
时没有效果。
我猜这是 http-client
中的某种设置,我现在找不到。
任何提示表示赞赏。
此外,我想避免从对象手动序列化 json,就像描述的那样 here。
通过io.micronaut.http.annotation.Body
注解来注解方法参数:
@Post("/api/endpoint")
ObjectResponse createPostRequest(
@Body RequestObject requestObject
);
那么它将按照您的预期进行序列化:
{ "id": "123"}
文档https://docs.micronaut.io/latest/guide/index.html#clientParameters
编写服务器(控制器)时的行为相同。
在文档中更好地解释了变量分辨率:https://docs.micronaut.io/latest/guide/index.html#_variable_resolution