1

I am running a jersey server which when it receives a POST request of 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":[] }' the marketAssociation field is being unmarshalled as "marketAssociation:[""] and not an empty array as expected. Everything else is being umarshalled correctly.

I have tried initializing the marketAssociation array in the class to an empty array to no success.

API Route

@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());
        }
    }

The SocialAccountType class

@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;
}

There are getter and setter for the class but they are generic and I dont think are relevant.

I expect that the marketAssociation should simply be an empty array marketAssociation: [] and not marketAssociation:[""]

This is the custom JAXB context resolver I am using

@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);
    }
}
Kammryn Dancy
  • 135
  • 1
  • 8

1 Answers1

0

Which JSON processor are you using? From what I gather, the default (JAXB) does some weirdness with empty arrays and switching to Jackson often fixes such problems. See the third answer here:

Cannot unmarshal a JSON array of objects using Jersey Client

adammtlx
  • 871
  • 7
  • 13
  • I am using the default processor, but I have created my own context resolver as to include custom configuration. I will add the configuration being used to my question. – Kammryn Dancy May 21 '19 at 21:19