JSON 到 java 在 JAX-RS 中解组,在 POST 中返回 { }
JSON to java unmarshelling in JAX-RS returning { } in POST
我正在尝试构建一个在 POST 方法中接受的 JAX-RS rest api a (JSON) UnMarshelled class 现在我仅 returning 相同的 class(编组为 JSON)。
当我 return 对象时,我在浏览器(邮递员客户端)中得到一个普通的 { }。我正在使用 question:.
中提供的数据
我的理解是否正确,我发送的 JSON 应该按原样返回?如果是,不确定为什么只收到 { } 或我完全错过了什么。
下面是我的代码:
@Path("/元数据")
public class 元数据资源 {
//Anil: This is the method that will be called when a post at /metadata comes
@Consumes(MediaType.APPLICATION_JSON)
@POST
@Produces(MediaType.APPLICATION_JSON)
public ObjectA CreateMetadata_JSON(ObjectA metadata) {
return metadata;
}
Class 对象 A:
@XmlRootElement
@XmlAccessorType(XmlAccessType.FIELD)
@XmlType(name = "objectA")
public class ObjectA {
@XmlElement(required = true)
protected String propertyOne;
@XmlElement(required = true)
protected String propertyTwo;
@XmlElement(required = true)
protected ObjectB objectB;
}
和Class对象B:
@XmlAccessorType(XmlAccessType.FIELD)
@XmlType(name = "objectB")
public class ObjectB {
@XmlElement(required = true)
protected String propertyA;
@XmlElement(required = true)
protected boolean propertyB;
}
正在发送的 json 对象是:
{
"objectA" :
{
"propertyOne" : "some val",
"propertyTwo" : "some other val",
"objectB" :
{
"propertyA" : "some val",
"propertyB" : "true"
}
}
}
这是因为根对象包装器
{
"objectA" :
}
通常,预期的 JSON 对象就是您拥有的一切,不包括 上述根值包装器。如果需要完全按照您的方式使用 JSON,则需要将 JSON 提供程序配置为 un-wrap 根值。如果您不这样做,那么提供者将无法识别 JSON 中的任何属性,您将得到一个包含一堆空值的对象。因此,当您序列化同一个对象时,提供者会忽略空值,您会留下一个空的 JSON 对象 { }
.
所以简单的解决方案就是使用
{
"propertyOne" : "some val",
"propertyTwo" : "some other val",
"objectB" :
{
"propertyA" : "some val",
"propertyB" : "true"
}
}
如果您需要根值包装器,那么我需要知道您使用的 JSON 提供程序,然后我才能尝试帮助您如何配置它以解包根值。
更新
对于MOXy,如果要将其配置为wrap/unwrap根值,可以setIncludeRoot
为真,在MoxyJsonConfig
。您需要提供 ContextResolver
才能被发现。
@Provider
public class MoxyConfigResolver implements ContextResolver<MoxyJsonConfig> {
private final MoxyJsonConfig config;
public MoxyConfigResolver() {
config = new MoxyJsonConfig();
config.setIncludeRoot(true);
}
@Override
public MoxyJsonConfig getContext(Class<?> type) {
return config;
}
}
我正在尝试构建一个在 POST 方法中接受的 JAX-RS rest api a (JSON) UnMarshelled class 现在我仅 returning 相同的 class(编组为 JSON)。
当我 return 对象时,我在浏览器(邮递员客户端)中得到一个普通的 { }。我正在使用 question:.
中提供的数据我的理解是否正确,我发送的 JSON 应该按原样返回?如果是,不确定为什么只收到 { } 或我完全错过了什么。
下面是我的代码:
@Path("/元数据") public class 元数据资源 {
//Anil: This is the method that will be called when a post at /metadata comes
@Consumes(MediaType.APPLICATION_JSON)
@POST
@Produces(MediaType.APPLICATION_JSON)
public ObjectA CreateMetadata_JSON(ObjectA metadata) {
return metadata;
}
Class 对象 A:
@XmlRootElement
@XmlAccessorType(XmlAccessType.FIELD)
@XmlType(name = "objectA")
public class ObjectA {
@XmlElement(required = true)
protected String propertyOne;
@XmlElement(required = true)
protected String propertyTwo;
@XmlElement(required = true)
protected ObjectB objectB;
}
和Class对象B:
@XmlAccessorType(XmlAccessType.FIELD)
@XmlType(name = "objectB")
public class ObjectB {
@XmlElement(required = true)
protected String propertyA;
@XmlElement(required = true)
protected boolean propertyB;
}
正在发送的 json 对象是:
{
"objectA" :
{
"propertyOne" : "some val",
"propertyTwo" : "some other val",
"objectB" :
{
"propertyA" : "some val",
"propertyB" : "true"
}
}
}
这是因为根对象包装器
{
"objectA" :
}
通常,预期的 JSON 对象就是您拥有的一切,不包括 上述根值包装器。如果需要完全按照您的方式使用 JSON,则需要将 JSON 提供程序配置为 un-wrap 根值。如果您不这样做,那么提供者将无法识别 JSON 中的任何属性,您将得到一个包含一堆空值的对象。因此,当您序列化同一个对象时,提供者会忽略空值,您会留下一个空的 JSON 对象 { }
.
所以简单的解决方案就是使用
{
"propertyOne" : "some val",
"propertyTwo" : "some other val",
"objectB" :
{
"propertyA" : "some val",
"propertyB" : "true"
}
}
如果您需要根值包装器,那么我需要知道您使用的 JSON 提供程序,然后我才能尝试帮助您如何配置它以解包根值。
更新
对于MOXy,如果要将其配置为wrap/unwrap根值,可以setIncludeRoot
为真,在MoxyJsonConfig
。您需要提供 ContextResolver
才能被发现。
@Provider
public class MoxyConfigResolver implements ContextResolver<MoxyJsonConfig> {
private final MoxyJsonConfig config;
public MoxyConfigResolver() {
config = new MoxyJsonConfig();
config.setIncludeRoot(true);
}
@Override
public MoxyJsonConfig getContext(Class<?> type) {
return config;
}
}