【问题标题】:Xml to java rest api (spring boot)xml 到 java rest api (spring boot)
【发布时间】:2019-10-02 11:54:26
【问题描述】:

我正在创建一个 Spring Boot Rest API,它应该接受 xml。 一旦我得到控制器接受的数据,我就可以向前移动。 所以基本上我的问题是,我如何让控制器接受数据?

我的理解是我可以使用jaxbjackson 来做这个,而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


    【解决方案1】:

    更新和解决方案。

    无法让杰克逊工作(花了很多时间)。

    无法使我自己的带有注释的 POJO 工作(花了很多时间并学习了很多教程)。几乎可以工作,但无法从 xml 中获取所有数据。

    原来我有 .XSD 文件,我可以从该文件自动生成必要的 POJO。这就是我是如何做到的。 (编辑:Eclipse Spring Tool Suite 4。)

    我安装了 java 13,但删除了它并下载并安装了 java 8。

    我在已安装的 JRE (Window->Preferences->Java->Installed JRE) 下设置了 jdk 的路径。 名称:jre1.8.... 位置 C:\Prog...\jdk1.8...

    为了能够生成pojos,我关注了https://www.consulting-bolte.de/index.php/java-se-ee/jaxb/123-setup-eclipse-for-jaxb (选择帮助 -> 安装新软件,使用:http://download.eclipse.org/releases/luna)。安装一切。

    右键单击 .xsd 文件,生成并创建 pojos 以及 ObjectFactory.java。

    制作服务并创建了我自己的响应 myResponse,它应该作为 Json 返回。 Jackson 包含在 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;
    
        }
    
    }
    

    希望这对您有所帮助。

    【讨论】:

      【解决方案2】:

      将参数添加到您的方法中。

      public void doStuff(@RequestBody Game game){...}
      

      我相信您将不需要额外的依赖项,因为您已经提到您的端点将使用 xml。只需确保您在项目中定义了一个与输入 xml 结构相同的模型。

      【讨论】:

        猜你喜欢
        • 2022-01-23
        • 2021-08-28
        • 2019-03-11
        • 2019-01-22
        • 2020-11-13
        • 2019-12-09
        • 2020-11-15
        • 2020-03-15
        • 2023-03-25
        相关资源
        最近更新 更多