简体   繁体   English

Xml 至 java rest Z8A5DA52ED126447D359E70C05AZ721A8A

[英]Xml to java rest api (spring boot)

I'm creating a spring boot rest API which should accept xml .我正在创建一个 spring 引导 rest API 应该接受xml Once i get the data to be accepted by the controller i can jiggle my way forward.一旦我得到要被 controller 接受的数据,我就可以继续前进。 So basically my question is, how do i get the controller to accept the data?所以基本上我的问题是,我如何让 controller 接受数据?

My understanding is that i can use either jaxb or jackson for this, and jackson is to be preferred(?)我的理解是,我可以为此使用jaxbjackson ,而jackson是首选(?)

The controller will look something like controller 看起来像

    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  }

    }

The input i get (which i feed into postman) is我得到的输入(我输入邮递员)是

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

So, my instinct is to add所以,我的直觉是添加

    <dependency>
     <groupId>com.fasterxml.jackson.dataformat</groupId>
     <artifactId>jackson-dataformat-xml</artifactId>
    </dependency>

to the pom file and set doStuff() as到 pom 文件并将 doStuff() 设置为

    public void doStuff(@RequestBody Game game) {}

where i create a pojo Game class (containing numberOfBalls (int), players (list of humans), bonus (list of rounds)) and then creating humans etc. Is this the easy way of doing it?我在哪里创建了一个 pojo 游戏 class(包含numberOfBalls (int), players (人类列表), bonus (回合列表)),然后创建人类等。这是简单的方法吗? A little confused here.这里有点困惑。

Appreciate any help.感谢任何帮助。

Update and solution.更新和解决方案。

Couldn´t make Jackson work (spent many hours).无法使 Jackson 工作(花了很多时间)。

Couldn´t make my own POJOs with annotation work (spent many hours and followed many tutorials).无法使用注释工作制作我自己的 POJO(花了很多时间并遵循了许多教程)。 Almost worked but couldn´t get all data from the xml.几乎可以工作,但无法从 xml 获取所有数据。

Turned out i had the.XSD-file and from that one i could auto-generate the necessary POJOs.原来我有.XSD 文件,我可以从那个文件中自动生成必要的 POJO。 Here is how i did that.这就是我是如何做到的。 (Edit: Eclipse Spring Tool Suite 4.) (编辑:Eclipse Spring 工具套件 4。)

I had java 13 installed, but removed that and downloaded and installed java 8 instead.我安装了 java 13 ,但删除了它并下载并安装了 java 8 。

I set the path to jdk under installed JREs (Window->Preferences->Java->Installed JREs).我在已安装的 JRE(Window->Preferences->Java->Installed JRE)下设置了 jdk 的路径。 Name: jre1.8.... Location C:\Prog...\jdk1.8...名称:jre1.8.... 位置 C:\Prog...\jdk1.8...

To be able to genreate pojos i followed https://www.consulting-bolte.de/index.php/java-se-ee/jaxb/123-setup-eclipse-for-jaxb (select Help -> Install New Software, work with: http://download.eclipse.org/releases/luna ).为了能够生成 pojos,我遵循了 https://www.consulting-bolte.de/index.php/java-se-ee/jaxb/123-setup-eclipse-for-jaxb (选择帮助 -> 安装新软件,使用: http://download.eclipse.org/releases/luna )。 Install everything.安装一切。

Right click on the.xsd-file, generate and pojos was created along with the ObjectFactory.java.右键单击 .xsd 文件,生成并创建 pojos 以及 ObjectFactory.java。

Made services and created my own response myResponse which shold be returned as Json.提供服务并创建了我自己的响应 myResponse,它应该返回为 Json。 Jackson is included in POM and takes care of it. Jackson 包含在 POM 中并负责处理它。

This is how my Controller looks like.这就是我的 Controller 的样子。

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;

    }

}

Hope this was helpful.希望这会有所帮助。

Add the parameter to your method.将参数添加到您的方法中。

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

I believe you won't be needing the additional dependencies since you have already mentioned that your endpoint will consume xml.我相信您将不需要额外的依赖项,因为您已经提到您的端点将使用 xml。 Just make sure you have a model defined in your project in the same structure as the input xml.只需确保在项目中定义了与输入 xml 结构相同的 model。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM