簡體   English   中英

Xml 至 java rest Z8A5DA52ED126447D359E70C05AZ721A8A

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

我正在創建一個 spring 引導 rest API 應該接受xml 一旦我得到要被 controller 接受的數據,我就可以繼續前進。 所以基本上我的問題是,我如何讓 controller 接受數據?

我的理解是,我可以為此使用jaxbjackson ,而jackson是首選(?)

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  }

    }

我得到的輸入(我輸入郵遞員)是

    <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 游戲 class(包含numberOfBalls (int), players (人類列表), bonus (回合列表)),然后創建人類等。這是簡單的方法嗎? 這里有點困惑。

感謝任何幫助。

更新和解決方案。

無法使 Jackson 工作(花了很多時間)。

無法使用注釋工作制作我自己的 POJO(花了很多時間並遵循了許多教程)。 幾乎可以工作,但無法從 xml 獲取所有數據。

原來我有.XSD 文件,我可以從那個文件中自動生成必要的 POJO。 這就是我是如何做到的。 (編輯:Eclipse Spring 工具套件 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 中並負責處理它。

這就是我的 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;

    }

}

希望這會有所幫助。

將參數添加到您的方法中。

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

我相信您將不需要額外的依賴項,因為您已經提到您的端點將使用 xml。 只需確保在項目中定義了與輸入 xml 結構相同的 model。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM