简体   繁体   English

在Java spring-boot中使用rest xml文件

[英]Consume rest xml file in java spring-boot

I have a spring boot project in which I need to consume xml file through REST. 我有一个Spring Boot项目,我需要在其中通过REST使用xml文件。 I just want to retrieve payload of my REST message as an xml file and store it locally. 我只想将REST消息的有效负载检索为xml文件并将其存储在本地。

On the internet, there are many tutorials to consume and convert xml file into java object, thanks to Jersey mainly. 在互联网上,主要有泽西岛,有很多教程可以使用xml文件并将其转换为java对象。 Nevertheless, I do not want to convert this xml file into java object; 不过,我不想将此xml文件转换为java对象; I just need to recover the xml and store it. 我只需要恢复xml并将其存储。

I guess it will look like below : 我想它将看起来像下面:

@POST
@Consumes(MediaType.APPLICATION_XML)
public void post(...) {
    //retrieve payload of my xml rest message
} 

@POST is not a spring annotation but Jersey one. @POST不是spring注释,而是Jersey注释。

With Spring annotations it would be something like this: 使用Spring注释将是这样的:

import java.nio.file.Paths;
import java.nio.file.Files;
import org.springframework.web.bind.annotation.*;

@RestController
public class YourController {

    @RequestMapping(value = "/requestpath",  method = RequestMethod.POST)
    @ResponseBody
    public String home(@RequestBody byte[] requestBody) throws Exception {
        String fileName = "target.filename.xml";
        Files.write(Paths.get(fileName), requestBody);
        return "<message>OK</message>";
    }

}

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

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