我有一个休息电话,它正在使用一些参数作为FormDataParam。当我将json字符串中的Object 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
}


这就是我将参数传递给使用邮递员进行休息电话的方式-
Postman screenshot

现在的问题是,当我调试rest调用的代码时,我将所有分配给EngineConfigMeta pojo的仅一个属性的json字符串-

EngineConfigMeta{tenantName={"tenantName": "abc", "version": 2}, version=0}


正如您在上面看到的那样,整个对象json字符串已分配给tenantName属性。因此,反序列化在此处无法正确进行。

请帮我。

最佳答案

这是因为客户端需要为各个Content-Type部分设置"config"标头。如果不这样做,则默认为text/plain。因为您有一个接受String的构造函数,所以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

10-05 18:05