gpt4 book ai didi

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

转载 作者:太空宇宙 更新时间:2023-11-04 09:38:14 25 4
gpt4 key购买 nike

我正在运行一个 Jersey 服务器,当它收到 curl -X POST\的 POST 请求时
http://localhost:8080/ca/accountSettings\
-H '内容类型:application/json'\
-d'{
"socialAccountTypeId":"adqasocialaccount",
"imgUrl":"https://test.com/logo.png",
"descriptionDefault":"测试帐户",
"descriptionLangId":"EN",
"nameDefault":"社交帐户",
"nameLangId":"zh",
“类型”:[“测试”],
"credentialsType":"OAUTH_20",
“市场协会”:[]
}'
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

关于java - 对 Jersey 客户端的 POST 请求无法解码包含空数组的 JSON 对象?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56245467/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com