我正在使用Jersey(1.9.1)并将JAXB用于我的对象,并尝试添加一个简单的JSON对象字段,理想情况下是Jackson的JsonNode类型,但我也可以使用jettison的JSONObject(或者甚至是GSON的JsonObject / JsonElement)
之所以这样,是因为我想允许发送'自由格式'json(作为我们示例中的obj元素),以及同一对象上的其他元素(使用JAXB注释)< / p>
我的对象:
import org.codehaus.jettison.json.JSONObject;
@XmlRootElement(name="moo")
@XmlAccessorType(XmlAccessType.FIELD)
public class Moo {
@XmlElement
JSONObject obj;
...
}
我的资源:
@Path("/moo")
@POST
@Produces({MediaType.APPLICATION_JSON})
@Consumes({MediaType.APPLICATION_JSON})
public Response postMsg(Moo e) {
System.out.println(e);
return Response.status(200).entity(e).build();
}
有效载荷:
{"obj": {"my": "custom", "object": 1, "here": [1, 2, 3] } }
使用上述内容执行POST请求时,obj
为空JSONObject
。当我尝试使用杰克逊的JsonNode
和GSON的JsonElement
/ JsonObject
时,也发生了同样的事情。
但是,当我使用JSONObject
作为方法的arg时,它能够解析它
@Path("/moo")
@POST
@Produces({MediaType.APPLICATION_JSON})
@Consumes({MediaType.APPLICATION_JSON})
public Response postMsg(JSONObject e) {
System.out.println(e);
return Response.status(200).entity(e).build();
}
我认为这是因为Jersey使用自己的com.sun.jersey.json.impl.provider.entity.JSONObjectProvider
进行编组,而JAXB使用自己的marshller。
在使用JAXB的注释(其他字段)时,有没有办法解决这个问题?我尝试使用自定义XmlAdapter
(使用@XmlJavaTypeAdapter
注释)而没有任何成功(因为我得到的对象是ValueType
是一个DOM元素)
我的最终目标是将字段obj
作为杰克逊代表(JsonNode
)。
任何指示都会非常感激。
答案 0 :(得分:2)
如果我做对了,你想嵌入一个原始的json免费值。
@XmlRootElement(name="moo")
@XmlAccessorType(XmlAccessType.FIELD)
public class Moo {
Object json;
@JsonRawValue
public String getJson() {
// default raw value: null or "[]"
return json == null ? null : json.toString();
}
public void setJson(JsonNode node) {
this.json = node;
}
}
取自here。
答案 1 :(得分:1)
有很多问题 首先是这段代码
import com.fasterxml.jackson.annotation.JsonProperty;
import com.fasterxml.jackson.databind.DeserializationFeature;
import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.SerializationFeature;
import com.fasterxml.jackson.databind.node.ArrayNode;
import com.fasterxml.jackson.databind.node.ObjectNode;
import com.fasterxml.jackson.dataformat.xml.XmlMapper;
import com.sun.xml.bind.marshaller.CharacterEscapeHandler;
import org.w3c.dom.ls.LSResourceResolver;
import org.xml.sax.*;
import javax.xml.bind.JAXBContext;
import javax.xml.bind.Marshaller;
import javax.xml.bind.Unmarshaller;
import javax.xml.bind.annotation.XmlAccessType;
import javax.xml.bind.annotation.XmlAccessorType;
import javax.xml.bind.annotation.XmlElement;
import javax.xml.bind.annotation.XmlRootElement;
import javax.xml.bind.annotation.adapters.XmlAdapter;
import javax.xml.bind.annotation.adapters.XmlJavaTypeAdapter;
import javax.xml.validation.Schema;
import javax.xml.validation.TypeInfoProvider;
import javax.xml.validation.Validator;
import javax.xml.validation.ValidatorHandler;
import java.io.File;
import java.io.IOException;
import java.io.Writer;
public class TestObjectNode {
@XmlRootElement(name = "moo")
@XmlAccessorType(XmlAccessType.FIELD)
static class Moo {
@JsonProperty("obj")
@XmlElement
@XmlJavaTypeAdapter(ObjMarshaller.class)
JsonNode obj;
}
static final ObjectMapper JSON_MAPPER = new ObjectMapper();
static final XmlMapper XML_MAPPER = (XmlMapper) new XmlMapper()
.disable(SerializationFeature.WRAP_ROOT_VALUE)
.disable(DeserializationFeature.UNWRAP_ROOT_VALUE);
public static void main(String[] args) throws Exception {
final String value = "{\"obj\": {\"my\": \"custom\", \"object\": 1, \"here\": [1, 2, 3] } }";
Moo moo = JSON_MAPPER.readValue(value, Moo.class);
System.out.println(moo.obj);
saveToFile(moo);
System.out.println(XML_MAPPER.writeValueAsString(moo));
moo = readFromFile();
System.out.println(moo.obj);
}
private static void saveToFile(Moo moo) throws Exception {
File file = new File("moo.xml");
JAXBContext jaxbContext = JAXBContext.newInstance(Moo.class);
Marshaller jaxbMarshaller = jaxbContext.createMarshaller();
jaxbMarshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true);
jaxbMarshaller.setProperty(CharacterEscapeHandler.class.getName(),
new CharacterEscapeHandler() {
@Override
public void escape(char[] chars, int i, int i1, boolean b, Writer writer) throws IOException {
writer.write(chars, i, i1);
}
});
jaxbMarshaller.marshal(moo, file);
jaxbMarshaller.marshal(moo, System.out);
}
private static Moo readFromFile() throws Exception {
File file = new File("moo.xml");
JAXBContext jaxbContext = JAXBContext.newInstance(Moo.class);
Unmarshaller unmarshaller = jaxbContext.createUnmarshaller();
MySchema mySchema = new MySchema();
unmarshaller.setSchema(mySchema);
Moo moo = (Moo) unmarshaller.unmarshal(file);
String objectNode = mySchema.validatorHandler.getObjectNodeString();
System.out.println("objectNode = " + objectNode);
JsonNode jsonNode = XML_MAPPER.readValue(objectNode, ObjectNode.class);
moo.obj = jsonNode;
return moo;
}
private static class ObjMarshaller extends XmlAdapter<String, JsonNode> {
@Override
public JsonNode unmarshal(String v) throws Exception {
throw new IllegalArgumentException("i'm never here///");
}
@Override
public String marshal(JsonNode v) throws Exception {
return XML_MAPPER.writeValueAsString(v);
}
}
static class MySchema extends Schema {
final ValidatorHandlerImpl validatorHandler = new ValidatorHandlerImpl();
@Override
public Validator newValidator() {
return null;
}
@Override
public ValidatorHandler newValidatorHandler() {
return validatorHandler;
}
}
static class ValidatorHandlerImpl extends ValidatorHandler {
private final StringBuilder objectNode = new StringBuilder();
private boolean insideObjectNode = false;
public String getObjectNodeString() {
return objectNode.toString();
}
@Override
public void setContentHandler(ContentHandler receiver) {
}
@Override
public ContentHandler getContentHandler() {
return null;
}
@Override
public void setErrorHandler(ErrorHandler errorHandler) {
}
@Override
public ErrorHandler getErrorHandler() {
return null;
}
@Override
public void setResourceResolver(LSResourceResolver resourceResolver) {
}
@Override
public LSResourceResolver getResourceResolver() {
return null;
}
@Override
public TypeInfoProvider getTypeInfoProvider() {
return null;
}
@Override
public void setDocumentLocator(Locator locator) {
}
@Override
public void startDocument() throws SAXException {
}
@Override
public void endDocument() throws SAXException {
}
@Override
public void startPrefixMapping(String prefix, String uri) throws SAXException {
}
@Override
public void endPrefixMapping(String prefix) throws SAXException {
}
@Override
public void startElement(String uri, String localName, String qName, Attributes atts) throws SAXException {
if(localName.equals("ObjectNode")) {
insideObjectNode = true;
}
if (insideObjectNode) {
objectNode.append("<" + localName + ">");
}
}
@Override
public void endElement(String uri, String localName, String qName) throws SAXException {
if (insideObjectNode) {
objectNode.append("</" + localName + ">");
}
if(localName.equals("ObjectNode")) {
insideObjectNode = false;
}
}
@Override
public void characters(char[] ch, int start, int length) throws SAXException {
if (insideObjectNode) {
objectNode.append(ch, start, length);
}
}
@Override
public void ignorableWhitespace(char[] ch, int start, int length) throws SAXException {
}
@Override
public void processingInstruction(String target, String data) throws SAXException {
}
@Override
public void skippedEntity(String name) throws SAXException {
}
}
}
提供以下输出
{"my":"custom","object":1,"here":[1,2,3]}
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<moo>
<obj><ObjectNode><my>custom</my><object>1</object><here>1</here><here>2</here><here>3</here></ObjectNode></obj>
</moo>
<Moo><obj><my>custom</my><object>1</object><here>1</here><here>2</here><here>3</here></obj></Moo>
objectNode = <ObjectNode><my>custom</my><object>1</object><here>1</here><here>2</here><here>3</here></ObjectNode>
{"my":"custom","object":"1","here":"3"}
所以,这是因为Jackson XML无法解析“here”作为数组。 所以,我们应该以不同的方式序列化它:
import com.alibaba.fastjson.JSON;
import com.fasterxml.jackson.annotation.JsonProperty;
import com.fasterxml.jackson.databind.DeserializationFeature;
import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.SerializationFeature;
import com.fasterxml.jackson.databind.node.ArrayNode;
import com.fasterxml.jackson.databind.node.ObjectNode;
import com.fasterxml.jackson.databind.node.TextNode;
import com.fasterxml.jackson.dataformat.xml.XmlMapper;
import com.sun.xml.bind.marshaller.CharacterEscapeHandler;
import org.w3c.dom.ls.LSResourceResolver;
import org.xml.sax.*;
import javax.xml.bind.JAXBContext;
import javax.xml.bind.Marshaller;
import javax.xml.bind.Unmarshaller;
import javax.xml.bind.annotation.XmlAccessType;
import javax.xml.bind.annotation.XmlAccessorType;
import javax.xml.bind.annotation.XmlElement;
import javax.xml.bind.annotation.XmlRootElement;
import javax.xml.bind.annotation.adapters.XmlAdapter;
import javax.xml.bind.annotation.adapters.XmlJavaTypeAdapter;
import javax.xml.validation.Schema;
import javax.xml.validation.TypeInfoProvider;
import javax.xml.validation.Validator;
import javax.xml.validation.ValidatorHandler;
import java.io.File;
import java.io.IOException;
import java.io.Writer;
public class TestObjectNode {
@XmlRootElement(name = "moo")
@XmlAccessorType(XmlAccessType.FIELD)
static class Moo {
@JsonProperty("obj")
@XmlElement
@XmlJavaTypeAdapter(ObjMarshaller.class)
JsonNode obj;
}
static final ObjectMapper JSON_MAPPER = new ObjectMapper();
static final XmlMapper XML_MAPPER = (XmlMapper) new XmlMapper()
.disable(SerializationFeature.WRAP_ROOT_VALUE)
.disable(DeserializationFeature.UNWRAP_ROOT_VALUE);
public static void main(String[] args) throws Exception {
final String value = "{\"obj\": {\"my\": \"custom\", \"object\": 1, \"here\": [1, 2, 3] } }";
Moo moo = JSON_MAPPER.readValue(value, Moo.class);
System.out.println(moo.obj);
saveToFile(moo);
System.out.println(XML_MAPPER.writeValueAsString(moo));
moo = readFromFile();
System.out.println(moo.obj);
}
private static void saveToFile(Moo moo) throws Exception {
File file = new File("moo.xml");
JAXBContext jaxbContext = JAXBContext.newInstance(Moo.class);
Marshaller jaxbMarshaller = jaxbContext.createMarshaller();
jaxbMarshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true);
jaxbMarshaller.setProperty(CharacterEscapeHandler.class.getName(),
new CharacterEscapeHandler() {
@Override
public void escape(char[] chars, int i, int i1, boolean b, Writer writer) throws IOException {
writer.write(chars, i, i1);
}
});
jaxbMarshaller.marshal(moo, file);
jaxbMarshaller.marshal(moo, System.out);
}
private static Moo readFromFile() throws Exception {
File file = new File("moo.xml");
JAXBContext jaxbContext = JAXBContext.newInstance(Moo.class);
Unmarshaller unmarshaller = jaxbContext.createUnmarshaller();
MySchema mySchema = new MySchema();
unmarshaller.setSchema(mySchema);
Moo moo = (Moo) unmarshaller.unmarshal(file);
String objectNode = mySchema.validatorHandler.getObjectNodeString();
System.out.println("objectNode = " + objectNode);
JsonNode jsonNode = XML_MAPPER.readValue(objectNode, ObjectNode.class);
Object hereObject = jsonNode.get("here");
if (hereObject instanceof TextNode) {
String hereArray = ((TextNode) hereObject).asText().replace("<![CDATA[", "").replace("]]>", "");
ArrayNode here = JSON_MAPPER.readValue(hereArray, ArrayNode.class);
((ObjectNode) jsonNode).put("here", here);
}
moo.obj = jsonNode;
return moo;
}
private static class ObjMarshaller extends XmlAdapter<String, JsonNode> {
@Override
public JsonNode unmarshal(String v) throws Exception {
return null;
}
@Override
public String marshal(JsonNode v) throws Exception {
Object hereObject = v.get("here");
if (hereObject instanceof ArrayNode) {
ArrayNode here = (ArrayNode) hereObject;
final StringBuilder value = new StringBuilder("<![CDATA[").append('[');
for (int i = 0; i < here.size(); ++i) {
if (i > 0) {
value.append(',');
}
value.append(here.get(i));
}
value.append(']').append("]]>");
((ObjectNode) v).put("here", value.toString());
}
return XML_MAPPER.writeValueAsString(v);
}
}
static class MySchema extends Schema {
final ValidatorHandlerImpl validatorHandler = new ValidatorHandlerImpl();
@Override
public Validator newValidator() {
return null;
}
@Override
public ValidatorHandler newValidatorHandler() {
return validatorHandler;
}
}
static class ValidatorHandlerImpl extends ValidatorHandler {
private final StringBuilder objectNode = new StringBuilder();
private boolean insideObjectNode = false;
public String getObjectNodeString() {
return objectNode.toString();
}
@Override
public void setContentHandler(ContentHandler receiver) {
}
@Override
public ContentHandler getContentHandler() {
return null;
}
@Override
public void setErrorHandler(ErrorHandler errorHandler) {
}
@Override
public ErrorHandler getErrorHandler() {
return null;
}
@Override
public void setResourceResolver(LSResourceResolver resourceResolver) {
}
@Override
public LSResourceResolver getResourceResolver() {
return null;
}
@Override
public TypeInfoProvider getTypeInfoProvider() {
return null;
}
@Override
public void setDocumentLocator(Locator locator) {
}
@Override
public void startDocument() throws SAXException {
}
@Override
public void endDocument() throws SAXException {
}
@Override
public void startPrefixMapping(String prefix, String uri) throws SAXException {
}
@Override
public void endPrefixMapping(String prefix) throws SAXException {
}
@Override
public void startElement(String uri, String localName, String qName, Attributes atts) throws SAXException {
if(localName.equals("ObjectNode")) {
insideObjectNode = true;
}
if (insideObjectNode) {
objectNode.append("<" + localName + ">");
}
}
@Override
public void endElement(String uri, String localName, String qName) throws SAXException {
if (insideObjectNode) {
objectNode.append("</" + localName + ">");
}
if(localName.equals("ObjectNode")) {
insideObjectNode = false;
}
}
@Override
public void characters(char[] ch, int start, int length) throws SAXException {
if (insideObjectNode) {
objectNode.append(ch, start, length);
}
}
@Override
public void ignorableWhitespace(char[] ch, int start, int length) throws SAXException {
}
@Override
public void processingInstruction(String target, String data) throws SAXException {
}
@Override
public void skippedEntity(String name) throws SAXException {
}
}
}
结果:
{"my":"custom","object":1,"here":[1,2,3]}
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<moo>
<obj><ObjectNode><my>custom</my><object>1</object><here><![CDATA[[1,2,3]]]></here></ObjectNode></obj>
</moo>
<Moo><obj><my>custom</my><object>1</object><here><![CDATA[[1,2,3]]]></here></obj></Moo>
objectNode = <ObjectNode><my>custom</my><object>1</object><here><![CDATA[[1,2,3]]]></here></ObjectNode>
{"my":"custom","object":"1","here":[1,2,3]}
这只是一个例子,你的任务是如何实现的。你看,它需要一些硬编码,所以也许最好考虑另一种方法。也许@XmlAnyElement可以帮到你。或者使用之前答案中的文字变体。 此外,如果删除CharacterEscapeHandler.class,您将收到转义的xml字符串。