简体   繁体   English

内容类型为application / x-www-form-urlencoded的Http Put请求在Spring中不起作用

[英]Http Put request with content type application/x-www-form-urlencoded not working in Spring

I have a spring-boot application that the only thing it does is receive http requests. 我有一个spring-boot应用程序,它唯一要做的就是接收http请求。

This is my spring cotroller: 这是我的春季自助餐:

@RestController
public class WebController {

    @Autowired
    private CallRecording callRecording;

    @PutMapping(path = "/cdrpostbox/callrecording/{hostedAccountId}/{mp3FileName}", consumes = MediaType.APPLICATION_FORM_URLENCODED_VALUE)
    public ResponseEntity<Object> callRecording(@PathVariable("hostedAccountId") String hostedAccountId, @PathVariable("mp3FileName") String mp3FileName, MultipartFile file) {
        return ResponseEntity.status(callRecording.service(hostedAccountId, mp3FileName, file)).body(null);
    }

}

I'm using Postman to send the request. 我正在使用邮递员发送请求。 The request that my spring application receives can't be changed, because the code is not maintained by my team. 我的spring应用程序收到的请求无法更改,因为我的团队未维护代码。

request headers 请求头

request body 请求主体

I found quite a few questions in here about the similar problems, but not quite the same problem that I have to solve. 我在这里发现了很多类似问题的问题,但不是必须解决的问题。 I tried adding and removing @RequestBody , replacing @RequestBody with @RequestParam , I tried the MultiValueMap , but it keeps returning the same error. 我尝试添加和删除@RequestBody ,将@RequestBody替换为@RequestParam ,尝试了MultiValueMap ,但它始终返回相同的错误。

java.lang.IllegalArgumentException: URLDecoder: Illegal hex characters in escape (%) pattern - For input string: "x"

I can't even debug the code because it fails before it reaches the controller. 我什至无法调试代码,因为它在到达控制器之前就失败了。

What am I missing? 我想念什么?

The content-type in the header is urlencoded, but you send binary data. 标头中的content-type是urlencoded,但是您发送二进制数据。

--- edit -编辑

If you want to get the file as a MultipartFile, then send the file as a form-data from postman, set the name of the data to file and add @RequestParam to the file argument in your method. 如果要以MultipartFile形式获取文件,则从邮递员以表单数据形式发送文件,将数据名称设置为file并将@RequestParam添加到方法中的file参数。

After a bit more investigation, I found the solution. 经过更多调查后,我找到了解决方案。

@RestController
@EnableWebMvc
public class WebController {

    @Autowired
    private CallRecording callRecording;

    @PutMapping(path = "/cdrpostbox/callrecording/{hostedAccountId}/{mp3FileName:.+}")
    public ResponseEntity<Object> callRecording(@PathVariable("hostedAccountId") String hostedAccountId, @PathVariable("mp3FileName") String mp3FileName, @RequestBody byte[] requestBody) {
        return ResponseEntity.status(callRecording.service(hostedAccountId, mp3FileName, requestBody)).body(null);
    }

It was missing the @EnableWebMvc . 它缺少@EnableWebMvc I also changed the mapping {mp3FileName:.+} because it was truncating the end of the request, you can check the SO question here . 我还更改了映射{mp3FileName:.+}因为它已截断了请求的结尾,您可以在此处检查SO问题。

Thanks @Selindek for the help 感谢@Selindek的帮助

暂无
暂无

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

相关问题 内容类型为 application/x-www-form-urlencoded 的 HTTP Post 请求在 Spring 引导服务中不起作用 - HTTP Post request with content type application/x-www-form-urlencoded not working in Spring boot service 内容类型为 application/x-www-form-urlencoded 的 Http Post 请求在 Spring 中不起作用 - Http Post request with content type application/x-www-form-urlencoded not working in Spring 内容类型为 application/x-www-form-urlencoded 的发布请求在 Spring 中不起作用 - Post request with content type application/x-www-form-urlencoded not working in Spring Spring MVC中内容类型应用程序/ x-www-form-urlencoded的请求参数的顺序 - Order of request parameters for content-type application/x-www-form-urlencoded in Spring MVC Spring Boot - 不支持内容类型“application/x-www-form-urlencoded;charset=UTF-8” - Spring Boot - Content type 'application/x-www-form-urlencoded;charset=UTF-8' not supported 更改请求的内容类型以处理使用application / x-www-form-urlencoded发送的XML - Changing Content-Type of the request to process XML sent using application/x-www-form-urlencoded 请求正文作为 json 发送,即使内容类型设置为 application/x-www-form-urlencoded - The request body is sent as json even though the content type is set as application/x-www-form-urlencoded 在Spring Boot中使用内容类型application / x-www-form-urlencoded的请求的自定义反序列化器 - Custom deserializer for requests with content-type application/x-www-form-urlencoded in Spring Boot 用Java处理Content-Type =“ application / x-www-form-urlencoded”的SOAP请求 - Processing SOAP request with Content-Type = “application/x-www-form-urlencoded” in Java 如果请求实体的内容类型不是application / x-www-form-urlencoded,则使用@FormParam。 - The @FormParam is utilized when the content type of the request entity is not application/x-www-form-urlencoded]
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM