【发布时间】: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