【发布时间】:2019-10-02 11:54:26
【问题描述】:
我正在创建一个 Spring Boot Rest API,它应该接受 xml。
一旦我得到控制器接受的数据,我就可以向前移动。
所以基本上我的问题是,我如何让控制器接受数据?
我的理解是我可以使用jaxb 或jackson 来做这个,而jackson 是首选(?)
控制器看起来像
package com.example.rest;
import org.springframework.http.HttpStatus;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseStatus;
import org.springframework.web.bind.annotation.RestController;
@RestController
@RequestMapping("/myapi")
public class myController {
@RequestMapping(method = RequestMethod.POST, path = "/xmlentry", consumes = "application/xml")
public void doStuff() {// do stuff }
}
我得到的输入(我输入邮递员)是
<Game>
<numberOfBalls>8</numberOfBalls>
<players>
<human>
<id>1001</id>
<name>John</name>
<skill>40</skill>
</human>
<human>
<id>2001</id>
<name>Jake</name>
<skill>58</skill>
</human>
<human>
<id>3001</id>
<name>Jane</name>
<skill>50</skill>
</human>
</players>
<bonus>
<round nr="1">
<id number="1001">1</id>
<id number="2001">1</id>
<id number="3001">4</id>
</round>
<round nr="2">
<id number="1001">6</id>
<id number="2001">0</id>
<id number="3001">1</id>
</round>
</bonus>
</Game>
所以,我的直觉是添加
<dependency>
<groupId>com.fasterxml.jackson.dataformat</groupId>
<artifactId>jackson-dataformat-xml</artifactId>
</dependency>
到 pom 文件并将 doStuff() 设置为
public void doStuff(@RequestBody Game game) {}
我创建了一个 pojo 游戏类(包含 numberOfBalls(int)、players(人类列表)、bonus(回合列表)),然后创建人类等。
这是简单的方法吗?这里有点困惑。
感谢任何帮助。
【问题讨论】:
标签: java rest api spring-boot