Java:jackson将xml和json转为JavaBean对象

依赖


<dependency>
    <groupId>com.fasterxml.jackson.dataformatgroupId>
    <artifactId>jackson-dataformat-xmlartifactId>
    <version>2.10.1version>
dependency>
<dependency>
    <groupId>org.codehaus.woodstoxgroupId>
    <artifactId>woodstox-core-aslartifactId>
    <version>4.4.1version>
dependency>


<dependency>
    <groupId>com.fasterxml.jackson.coregroupId>
    <artifactId>jackson-databindartifactId>
    <version>2.10.0version>
dependency>

book.xml


<book>
    <id>1id>
    <name>三国演义name>
    <author>罗贯中author>
book>

book.json

{
  "id": 1,
  "name": "三国演义",
  "author": "罗贯中"
}

Book.java

package com.example.demo.pojo;

public class Book {
    private Integer id;
    private String name;
    private String author;

    public Integer getId() {
        return id;
    }

    public void setId(Integer id) {
        this.id = id;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public String getAuthor() {
        return author;
    }

    public void setAuthor(String author) {
        this.author = author;
    }

    @Override
    public String toString() {
        return "Book{" +
                "id=" + id +
                ", name='" + name + '\'' +
                ", author='" + author + '\'' +
                '}';
    }
}

测试类

package com.example.demo;

import com.example.demo.pojo.Book;
import com.fasterxml.jackson.databind.DeserializationFeature;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.dataformat.xml.JacksonXmlModule;
import com.fasterxml.jackson.dataformat.xml.XmlMapper;
import org.junit.jupiter.api.Test;

import java.io.IOException;
import java.io.InputStream;

public class JacksonTest {

    @Test
    public void testParseXml() throws IOException {
        InputStream input = JacksonTest.class.getResourceAsStream("/book.xml");
        JacksonXmlModule module = new JacksonXmlModule();
        XmlMapper mapper = new XmlMapper(module);
        Book book = mapper.readValue(input, Book.class);

        System.out.println(book);
        // Book{id=1, name='三国演义', author='罗贯中'}

        // 转xml
        String xml = mapper.writeValueAsString(book);
        System.out.println(xml);
        //    1三国演义罗贯中
    }

    @Test
    public void testParseJson() throws IOException {
        InputStream input = JacksonTest.class.getResourceAsStream("/book.json");
        ObjectMapper mapper = new ObjectMapper();

        // 反序列化时忽略不存在的JavaBean属性
        mapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);
        Book book = mapper.readValue(input, Book.class);

        System.out.println(book);
        // Book{id=1, name='三国演义', author='罗贯中'}

        //JavaBean变为JSON
        String json = mapper.writeValueAsString(book);
        System.out.println(json);
        //    {"id":1,"name":"三国演义","author":"罗贯中"}
    }
}

参考
https://www.liaoxuefeng.com/wiki/1252599548343744/1320418596093986

你可能感兴趣的:(Java:jackson将xml和json转为JavaBean对象)