我正在运行一个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);
}
}
最佳答案
您正在使用哪个JSON处理器?据我所知,默认值(JAXB)对空数组有些奇怪,切换到Jackson常常可以解决此类问题。在这里查看第三个答案:
Cannot unmarshal a JSON array of objects using Jersey Client