有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

在JavaSpringBoot中使用RESTXML文件

我有一个SpringBoot项目,其中我需要通过REST使用xml文件。 我只想以xml文件的形式检索REST消息的有效负载,并将其存储在本地

在internet上,有许多教程使用xml文件并将其转换为java对象,这主要归功于Jersey。 然而,我不想将这个xml文件转换成java对象;我只需要恢复xml并存储它

我猜它将如下所示:

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

共 (1) 个答案

  1. # 1 楼答案

    @POST不是spring注释,而是Jersey注释

    对于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>";
        }
    
    }