【问题标题】:POST request to Jersey client cannot unmarshal JSON object that contains an empty array?对 Jersey 客户端的 POST 请求无法解组包含空数组的 JSON 对象?
【发布时间】:2019-10-08 06:31:30
【问题描述】:

我正在运行一个球衣服务器,当它收到 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);
    }
}

【问题讨论】:

    标签: java arrays json jersey unmarshalling


    【解决方案1】:

    您使用的是哪个 JSON 处理器?据我所知,默认(JAXB)对空数组有些奇怪,切换到杰克逊通常可以解决这些问题。在此处查看第三个答案:

    Cannot unmarshal a JSON array of objects using Jersey Client

    【讨论】:

    • 我使用的是默认处理器,但我创建了自己的上下文解析器以包含自定义配置。我将在我的问题中添加正在使用的配置。
    猜你喜欢
    • 2012-03-26
    • 2012-06-29
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-09-18
    • 2018-08-22
    相关资源
    最近更新 更多