gpt4 book ai didi

java - Spring mvc - 将 xml 文本字符串发送到 Controller

转载 作者:太空宇宙 更新时间:2023-11-04 11:16:18 27 4
gpt4 key购买 nike

我正在开发一个 java spring mvc 应用程序。我需要发送一个 xml 字符串到我的 Controller ,并在 Controller 内将此 xml 作为简单文本字符串获取。但目前还找不到任何解决办法。我尝试过这种方法:

@RequestMapping(value = "/test", method = RequestMethod.POST)
public String test(String post, HttpServletRequest request, HttpServletResponse response){
System.out.println("post: " + post);
}

我的ajax配置中有contentType: 'text/xml'。但变量 post 始终打印为 null

此外,我在方法中尝试了 consumes = MediaType.APPLICATION_XML_VALUEconsumes = MediaType.TEXT_XML_VALUE,但返回了 HTTP Status 415 – 不支持的媒体类型。问题是什么? 如何将简单的 xml 文本发送到我的 Controller ?

最佳答案

您可以使用 RequestParam 读取字符串:

 @RequestMapping(value = "/test", method = RequestMethod.POST)
public String test(
@RequestParam(value="post") String post, Model model){

...
}

关于java - Spring mvc - 将 xml 文本字符串发送到 Controller ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45430792/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com