对Jersey客户端的POST请求无法解组包含空数组的JSON对象?

时间:2019-05-21 19:50:48

标签: java arrays json jersey unmarshalling

我正在运行一个jersey服务器,当它接收到curl -X POST \ http://localhost:8080/ca/accountSettings \ -H 'Content-Type: application/json' \ -d '{ "socialAccountTypeId":"adqasocialaccount", "imgUrl":"https://test.com/logo.png", "descriptionDefault":"TEST Account", "descriptionLangId":"EN", "nameDefault":"Social Account", "nameLangId":"en", "types":["test"], "credentialsType":"OAUTH_20", "marketAssociation":[] }'的POST请求时,marketAssociation字段将被解组为“ marketAssociation:[“”],而不是一个预期的空数组。

我尝试将类中的marketAssociation数组初始化为空数组,但未成功。

API路由

@POST
    @Path("/ca/accountSettings")
    public Response addAccountSettings(SocialAccountType socialAccountType)
    {
        String endpoint = "POST /ca/accountSettings";
        Response response = null;
        try
        {
            WCResponse wcResponse = this.sssApi.addSocialAccountType(socialAccountType);
catch (Exception e)
        {
            TestApi.log.error("Exception in " + endpoint, e.getMessage());
            response = response.error(HttpServletResponse.SC_INTERNAL_SERVER_ERROR, e.getMessage());
        }
    }

SocialAccountType类

@XmlAccessorType(XmlAccessType.FIELD)
@XmlType(name = "SocialAccountType", propOrder = {
    "socialAccountTypeId",
    "imgUrl",
    "descriptionDefault",
    "descriptionLangId",
    "nameDefault",
    "nameLangId",
    "types",
    "credentialsType",
    "marketAssociation",
    "creationTime"
})
@XmlRootElement(name = "SocialAccountType")
public class SocialAccountType {

    @XmlElement(required = true)
    protected String socialAccountTypeId;
    @XmlElement(required = true)
    protected String imgUrl;
    @XmlElement(required = true)
    protected String descriptionDefault;
    @XmlElement(required = true)
    protected String descriptionLangId;
    @XmlElement(required = true)
    protected String nameDefault;
    @XmlElement(required = true)
    protected String nameLangId;
    protected List<String> types;
    @XmlElement(required = true)
    protected CredentialsType credentialsType;
    protected List<String> marketAssociation;
    protected String creationTime;
}

该类有getter和setter方法,但它们是通用的,我认为不相关。

我希望marketAssociation应该只是一个空数组 marketAssociation:[]而不是marketAssociation:[“”]

这是我正在使用的自定义JAXB上下文解析器

@Provider
@Singleton
public class JAXBContextResolver implements ContextResolver<JAXBContext>
{

    private JAXBContext context;
    @SuppressWarnings("rawtypes")
    private Class[] types = { Device.class, EndUser.class, SocialAccountType.class, SocialAccountTypeList.class };

    public JAXBContextResolver() throws Exception
    {
        this.context = new JSONJAXBContext(JSONConfiguration
                                                   .mapped()
                                                   .arrays("userList", "deviceList", "roles", "socialAccountType", "marketAssociation", "types")
                                                   .build(), types);
    }

    @Override
    @SuppressWarnings("rawtypes")
    public JAXBContext getContext(Class<?> objectType)
    {
        for (Class c : types)
        {
            if (c.equals(objectType))
                return (context);
        }
        return (null);
    }
}

1 个答案:

答案 0 :(得分:0)

您使用哪个JSON处理器?据我所知,默认值(JAXB)对空数组有些怪异,而切换到Jackson常常可以解决此类问题。在这里查看第三个答案:

Cannot unmarshal a JSON array of objects using Jersey Client