post 请求中作为 JSON 字符串传递的 FormDataParam 对象未正确反序列化
FormDataParam Object passed as JSON string in post request does not get deserialized properly
我有一个休息电话,它将一些参数作为 FormDataParam。当我将 json 字符串中的对象 EngineConfigMeta 传递给邮递员的 rest 调用时,在 restcall 级别,对象未正确反序列化。
休息电话
@Path( "/add-config" )
@POST
@Consumes( MediaType.MULTIPART_FORM_DATA )
@Produces( MediaType.APPLICATION_JSON )
public Response addConfig( @FormDataParam( "config" ) EngineConfigMeta config,
@FormDataParam( "file" ) InputStream configFileInputStream,
@FormDataParam( "file" ) FormDataContentDisposition cdh)
{
return Response.ok(Response.Status.OK).entity(buildJson(config.getVersion())).build();
}
EngineConfigMeta.java
public class EngineConfigMeta {
private String tenantName;
private long version;
EngineConfigMeta(String tenantName, long version) {
this.tenantName = tenantName;
this.version = version;
}
..getters and setters
}
这就是我使用邮递员将参数传递给 rest 调用的方式 -
Postman screenshot
现在的问题是,当我调试 rest 调用的代码时,我将所有 json 字符串分配给了 EngineConfigMeta pojo 上的一个 属性 -
EngineConfigMeta{tenantName={"tenantName": "abc", "version": 2}, version=0}
正如您在上面看到的,整个对象 json 字符串都分配给了 tenantName 属性。所以反序列化在这里没有正确发生。
请帮帮我
这是因为客户端需要为单独的"config"
部分设置Content-Type
头。如果您不这样做,那么它将默认为 text/plain
。因为你有一个接受字符串的构造函数,Jersey 只是假设将构造函数参数的值分配给传入的部分数据。
在 Postman 中,我认为您不能设置单个部分的 Content-Type。您需要做的是使用 FormDataBodyPart
在服务器端手动设置类型。然后你可以手动获取EngineConfigMeta
。
public Response post(@FormDataParam("config") FormDataBodyPart part) {
part.setMediaType(MediaType.APPLICATION_JSON_TYPE);
EngineConfigMeta meta = part.getValueAs(EngineConfigMeta.class);
}
另请参阅:
- File upload along with other object in Jersey restful web service
我有一个休息电话,它将一些参数作为 FormDataParam。当我将 json 字符串中的对象 EngineConfigMeta 传递给邮递员的 rest 调用时,在 restcall 级别,对象未正确反序列化。
休息电话
@Path( "/add-config" )
@POST
@Consumes( MediaType.MULTIPART_FORM_DATA )
@Produces( MediaType.APPLICATION_JSON )
public Response addConfig( @FormDataParam( "config" ) EngineConfigMeta config,
@FormDataParam( "file" ) InputStream configFileInputStream,
@FormDataParam( "file" ) FormDataContentDisposition cdh)
{
return Response.ok(Response.Status.OK).entity(buildJson(config.getVersion())).build();
}
EngineConfigMeta.java
public class EngineConfigMeta {
private String tenantName;
private long version;
EngineConfigMeta(String tenantName, long version) {
this.tenantName = tenantName;
this.version = version;
}
..getters and setters
}
这就是我使用邮递员将参数传递给 rest 调用的方式 - Postman screenshot
现在的问题是,当我调试 rest 调用的代码时,我将所有 json 字符串分配给了 EngineConfigMeta pojo 上的一个 属性 -
EngineConfigMeta{tenantName={"tenantName": "abc", "version": 2}, version=0}
正如您在上面看到的,整个对象 json 字符串都分配给了 tenantName 属性。所以反序列化在这里没有正确发生。
请帮帮我
这是因为客户端需要为单独的"config"
部分设置Content-Type
头。如果您不这样做,那么它将默认为 text/plain
。因为你有一个接受字符串的构造函数,Jersey 只是假设将构造函数参数的值分配给传入的部分数据。
在 Postman 中,我认为您不能设置单个部分的 Content-Type。您需要做的是使用 FormDataBodyPart
在服务器端手动设置类型。然后你可以手动获取EngineConfigMeta
。
public Response post(@FormDataParam("config") FormDataBodyPart part) {
part.setMediaType(MediaType.APPLICATION_JSON_TYPE);
EngineConfigMeta meta = part.getValueAs(EngineConfigMeta.class);
}
另请参阅:
- File upload along with other object in Jersey restful web service