How would I express XML tag attributes in JSON?

后端 未结 13 2177
遥遥无期
遥遥无期 2020-12-07 20:35

I am designing an API for my webapp.

I was thinking to support only JSON responses (not XML) because more streamlined.

But I have just bumped to this XML:

13条回答
  •  轻奢々
    轻奢々 (楼主)
    2020-12-07 21:20

    I've ran into a scenario that needs both XML and JSON for input and output based on what was being passed in. I found a way that works with XML Attributes/Properties and JSON. Now note, it's how it coded in Java, makes it work this way.

    My XML Example:

    
        05/05/2017 13:45:22
        2da236d2-3852-4a09-8067-198193d2828b
        This is my message
    
    

    My JSON Example:

    {
        "datetime":"05/05/2017 13:45:22",
        "sessionid":"2da236d2-3852-4a09-8067-198193d2828b",
        "message": {
            "content":"This is a testa",
            "msgType":"Debug"
        }
    }
    

    How I made it work via code Log.java:

    package log;
    
    import com.fasterxml.jackson.dataformat.xml.annotation.JacksonXmlProperty;
    import com.fasterxml.jackson.dataformat.xml.annotation.JacksonXmlRootElement;
    
    @JacksonXmlRootElement(localName = "Log")
    public class Log {
        @JacksonXmlProperty(localName = "datetime")
        private String datetime;
        @JacksonXmlProperty(localName = "sessionid")
        private String sessionid;
        @JacksonXmlProperty(localName = "message")
        private Message message;
    
        public Log() {
            this.sessionid = "0";
            this.datetime = "";
            this.message = new Message();
        }
    
        public String getDatetime() {
            return datetime;
        }
    
        public void setDatetime(String datetime) {
            this.datetime = datetime;
        }
    
        public String getSessionid() {
            return sessionid;
        }
    
        public void setSessionid(String sessionid) {
            this.sessionid = sessionid;
        }
    
        public Message getMessage() {
            return message;
        }
    
        public void setMessage(Message message) {
            this.message = message;
        }
    }
    

    Message.java, Note the @JacksonXmlText below, which is key:

    package log;
    
    import com.fasterxml.jackson.dataformat.xml.annotation.JacksonXmlProperty;
    import com.fasterxml.jackson.dataformat.xml.annotation.JacksonXmlText;
    
    public class Message {
        @JacksonXmlProperty(localName = "msgType", isAttribute = true)
        private String msgType;
        @JacksonXmlText
        private String content;
    
        public Message() {
            this.content = "";
        }
    
        public String getMsgType() {
            return msgType;
        }
    
        public void setMsgType(String msgType) {
            switch(msgType.toLowerCase())
            {
            case "test":
            case "debug":
            case "warn":
            case "error":
                break;
            default:
                msgType = "Unknown";
                break;
            }
    
            this.msgType = msgType;
        }
    
        public String getContent() {
            return content;
        }
    
        public void setContent(String content) {
            this.content = content;
        }
    }
    

    Caller within LogController.java:

    ..
    @RequestMapping(value = "/Logger", produces={"application/xml", "application/json"}, consumes={"application/xml", "application/json"})
    public ResponseEntity Logger(@RequestBody String logInfo, @RequestHeader("Content-Type") String contentType) {
        try 
        {
            String xml = "";
            Log logObj = null; 
            HttpHeaders responseHeaders = new HttpHeaders();
            responseHeaders.add("Content-Type", contentType);
    
            if (contentType.toLowerCase().contains("json"))
            {
               ObjectMapper mapper = new ObjectMapper();
               logObj = mapper.readValue(logInfo, Log.class);
               xml = mapper.writeValueAsString(logObj);
            }
            else if (contentType.toLowerCase().contains("xml"))
            {
               XmlMapper xmlMapper = new XmlMapper();
               logObj = xmlMapper.readValue(logInfo, Log.class);
               xml = xmlMapper.writeValueAsString(logObj);
            }
            else
               return new ResponseEntity(HttpStatus.BAD_REQUEST);
    
            //TODO GWL
            //Save Log data, via Async Web Service, Data, or System 
            return new ResponseEntity(xml, responseHeaders, HttpStatus.OK);
        } 
        catch( Exception ex)
        {
            ex.printStackTrace();
            return new ResponseEntity(HttpStatus.BAD_REQUEST);
        }
    }
    

提交回复
热议问题