[英]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应用程序收到的请求无法更改,因为我的团队未维护代码。
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.