Xml到JavaRESTAPI(SpringBoot)
我正在创建一个SpringBootRESTAPI,它应该接受Xml到JavaRESTAPI(SpringBoot),java,rest,api,spring-boot,Java,Rest,Api,Spring Boot,我正在创建一个SpringBootRESTAPI,它应该接受xml。 一旦我得到控制器接受的数据,我就可以继续前进。 所以基本上我的问题是,如何让控制器接受数据 我的理解是,我可以使用jaxb或jackson来实现这一点,而jackson是首选(?) 控制器看起来像 package com.example.rest; import org.springframework.http.HttpStatus; import org.springframework.web.bi
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 }
}
我得到的输入(输入到postman中)是
在这里,我创建了一个pojo游戏类(包含numberOfBalls
(int)、玩家
(人类列表)、bonus
(回合列表)),然后创建人类等。
这是简单的方法吗?这里有点混乱
感谢您的帮助。将参数添加到您的方法中
public void doStuff(@RequestBody Game game){...}
我相信您不需要额外的依赖项,因为您已经提到您的端点将使用xml。只需确保在项目中以与输入xml相同的结构定义了一个模型。更新和解决方案
无法让杰克逊工作(花了很多时间)
无法使用注释制作我自己的POJO(花费了很多时间并遵循了很多教程)。几乎可以工作,但无法从xml获取所有数据
事实证明我有一个.XSD文件,从这个文件我可以自动生成必要的POJO。我是这样做的。(编辑:Eclipse Spring工具套件4。)
我安装了Java13,但删除了它,下载并安装了Java8
我在installed JRE(窗口->首选项->Java->installed JRE)下设置了jdk的路径。
名称:jre1.8。。。。
位置C:\Prog…\jdk1.8
为了能创造我跟随的Pojo
(选择帮助->安装新软件,使用:)。安装所有东西
右键单击.xsd文件,生成POJO,POJO与ObjectFactory.java一起创建
创建服务并创建我自己的响应myResponse,该响应应作为Json返回。杰克逊被包括在POM中并负责这项工作
这就是我的控制器的样子
package com.example.rest;
import java.io.StringReader;
import javax.xml.bind.JAXBContext;
import javax.xml.bind.JAXBElement;
import javax.xml.bind.JAXBException;
import javax.xml.bind.Unmarshaller;
import org.springframework.beans.factory.annotation.Autowired;
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.RestController;
@RestController
@RequestMapping("/myapi")
public class myController {
@Autowired
private MyService myService;
@RequestMapping(method = RequestMethod.POST, path = "/xmlentry", consumes = "application/xml", produces = "application/json")
public MyResponse doStuff(@RequestBody String xmlString) {
try {
JAXBContext jc = JAXBContext.newInstance("com.example.rest");
Unmarshaller um = jc.createUnmarshaller();
//GameType: Generated pojo, root element
JAXBElement<GameType> gameType = (JAXBElement<GameType>) um.unmarshal(new StringReader(xmlString));
//gameType is now populated with xml data
return myService.getMyResponse(gameType);
} catch (JAXBException e) {
}
return null;
}
}
package com.example.rest;
导入java.io.StringReader;
导入javax.xml.bind.JAXBContext;
导入javax.xml.bind.JAXBElement;
导入javax.xml.bind.JAXBException;
导入javax.xml.bind.Unmarshaller;
导入org.springframework.beans.factory.annotation.Autowired;
导入org.springframework.web.bind.annotation.RequestBody;
导入org.springframework.web.bind.annotation.RequestMapping;
导入org.springframework.web.bind.annotation.RequestMethod;
导入org.springframework.web.bind.annotation.RestController;
@RestController
@请求映射(“/myapi”)
公共类myController{
@自动连线
私人MyService-MyService;
@RequestMapping(method=RequestMethod.POST,path=“/xmlcentry”,consumes=“application/xml”,products=“application/json”)
公共MyResponse doStuff(@RequestBody String-xmlString){
试一试{
JAXBContext jc=JAXBContext.newInstance(“com.example.rest”);
解组器um=jc.createUnmarshaller();
//游戏类型:生成的pojo,根元素
JAXBElement游戏类型=(JAXBElement)um.unmarshal(新StringReader(xmlString));
//游戏类型现在由xml数据填充
返回myService.getMyResponse(游戏类型);
}捕获(JAXBEException e){
}
返回null;
}
}
希望这是有帮助的
public void doStuff(@RequestBody Game game) {}
public void doStuff(@RequestBody Game game){...}
package com.example.rest;
import java.io.StringReader;
import javax.xml.bind.JAXBContext;
import javax.xml.bind.JAXBElement;
import javax.xml.bind.JAXBException;
import javax.xml.bind.Unmarshaller;
import org.springframework.beans.factory.annotation.Autowired;
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.RestController;
@RestController
@RequestMapping("/myapi")
public class myController {
@Autowired
private MyService myService;
@RequestMapping(method = RequestMethod.POST, path = "/xmlentry", consumes = "application/xml", produces = "application/json")
public MyResponse doStuff(@RequestBody String xmlString) {
try {
JAXBContext jc = JAXBContext.newInstance("com.example.rest");
Unmarshaller um = jc.createUnmarshaller();
//GameType: Generated pojo, root element
JAXBElement<GameType> gameType = (JAXBElement<GameType>) um.unmarshal(new StringReader(xmlString));
//gameType is now populated with xml data
return myService.getMyResponse(gameType);
} catch (JAXBException e) {
}
return null;
}
}