java 尝试从XML创建POJO时获取不支持的媒体类型

uhry853o  于 2023-05-12  发布在  Java
关注(0)|答案(1)|浏览(98)

我试图从POST请求中输入的XML创建POJO,但出现“不支持的媒体类型”错误。我不确定是我的字段缺少了某种注解,还是我的类设置不正确。我试着在网上看其他的例子,但我仍然不知道我的混乱在哪里。

控制器

@RestController
@RequestMapping("/api")
public class TestController {

    @PostMapping(value = "/test", consumes = MediaType.APPLICATION_XML_VALUE,
            produces = MediaType.APPLICATION_JSON_VALUE)
    public String test(@RequestBody Race race) { 
        System.out.println("value is: " + race);

        return "{ \"message\": \"doesnt matter for now\" }";
    }
}

POJO
种族

public class Race implements Serializable
{
    private static final long serialVersionUID = 1379428133671921863L;

    @XmlElement
    private int numberOfLaps;
    @XmlElement
    private Item item;

    public Race() {
    }

    public Race(int numberOfLaps, Item item) {
        this.numberOfLaps = numberOfLaps;
        this.item = item;
    }

    public int getNumberOfLaps ()
    {
        return numberOfLaps;
    }

    public void setNumberOfLaps (int numberOfLaps)
    {
        this.numberOfLaps = numberOfLaps;
    }

    public Item getItem ()
    {
        return item;
    }

    public void setItem (Item item)
    {
        this.item = item;
    }

    @Override
    public String toString()
    {
        return "ClassPojo [numberOfLaps = "+numberOfLaps+", item = "+item+"]";
    }
}

项目

public class Item implements Serializable
{
    private static final long serialVersionUID = -2335797924272015448L;

    @XmlElement
    private Laps[] laps;

    public Item() {
    }

    public Item(Laps[] laps) {
        this.laps = laps;
    }

    public Laps[] getlaps ()
    {
        return laps;
    }

    public void setlaps (Laps[] laps)
    {
        this.laps = laps;
    }

    @Override
    public String toString()
    {
        return "ClassPojo [laps = "+laps+"]";
    }
}

圈数

public class Laps implements Serializable
{
    private static final long serialVersionUID = 1L;

    @XmlAttribute
    private int count;
    @XmlElement
    private Lane[] lane;

    public Laps() {
    }
    public Laps(int count, Lane[] lane) {
        this.count = count;
        this.lane = lane;
    }

    public int getCount ()
    {
        return count;
    }

    public void setCount (int count)
    {
        this.count = count;
    }

    public Lane[] getLane ()
    {
        return lane;
    }

    public void setLane (Lane[] lane)
    {
        this.lane = lane;
    }

    @Override
    public String toString()
    {
        return "ClassPojo [count = "+count+", lane = "+lane+"]";
    }
}

车道

public class Lane implements Serializable
{
    private static final long serialVersionUID = -8249718991307207229L;

    @XmlAttribute
    private int number;
    @XmlAttribute
    private String value;

    public Lane() {
    }
    public Lane(int number, String value) {
        this.number = number;
        this.value = value;
    }

    public int getNumber ()
    {
        return number;
    }

    public void setNumber (int number)
    {
        this.number = number;
    }

    public String getContent ()
    {
        return value;
    }

    public void setContent (String content)
    {
        this.value = content;
    }

    @Override
    public String toString()
    {
        return "ClassPojo [number = "+number+", content = "+value+"]";
    }
}

POSTMAN的原始XML输入

<race>
    <countOfLaps>3</countOfLaps>
    <item>
        <laps count="1">
            <lane count="1">1</lane>
            <lane count="2">1</lane>
            <lane count="3">0</lane>
            <lane count="4">-2</lane>
        </laps>
        <laps count="2">
            <lane count="1">1</lane>
            <lane count="2">-1</lane>
            <lane count="3">2</lane>
            <lane count="4">-2</lane>
        </laps>
    </item>
</race>

POM依赖性

<dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
            <exclusions>
                <exclusion>
                    <groupId>org.springframework.boot</groupId>
                    <artifactId>spring-boot-starter-tomcat</artifactId>
                </exclusion>
            </exclusions>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-undertow</artifactId>
        </dependency>
        <dependency>
            <groupId>javax.xml.bind</groupId>
            <artifactId>jaxb-api</artifactId>
            <version>2.1</version>
        </dependency>
        <dependency>
            <groupId>com.sun.xml.bind</groupId>
            <artifactId>jaxb-impl</artifactId>
            <version>2.2.11</version>
        </dependency>
    </dependencies>

全错误信息

{
    "timestamp": 1572020022472,
    "status": 415,
    "error": "Unsupported Media Type",
    "exception": "org.springframework.web.HttpMediaTypeNotSupportedException",
    "message": "Unsupported Media Type",
    "path": "/api/play"
}

cvxl0en2

cvxl0en21#

最后我不得不加上

@XmlRootElement(name = "nameInXml")
@XmlAccessorType(XmlAccessType.FIELD)

对于每个类
这两个依赖:

<dependency>
            <groupId>javax.xml.bind</groupId>
            <artifactId>jaxb-api</artifactId>
            <version>2.3.1</version>
        </dependency>
                <dependency>
            <groupId>org.glassfish.jaxb</groupId>
            <artifactId>jaxb-runtime</artifactId>
            <version>2.3.1</version>
        </dependency>

相关问题