gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-01 22:27:20 25 4
gpt4 key购买 nike

我尝试从 POST 请求中输入的 XML 创建 POJO,但收到“不支持的媒体类型”错误。我不确定我的字段是否缺少某种注释,或者我是否以某种方式错误地设置了我的类。我尝试过在网上查看其他示例,但我仍然不明白我的困惑在哪里。

Controller

@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+"]";
}
}

原始 XML 输入到 POSTMAN

<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"
}

Postman

最佳答案

最后我不得不添加

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

每个类(class)

以及这两个依赖项:

        <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>

关于java - 尝试从 XML 创建 POJO 时获取不支持的媒体类型,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58562784/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com