Spring Boot:防止对@RequestParam 进行解码 [英] Spring Boot: Prevent decoding for @RequestParam

查看:75
本文介绍了Spring Boot:防止对@RequestParam 进行解码的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个在 Tomcat 8.5 上运行的 Spring-Boot (1.5.3) 应用程序.在其中,我有一个带有单个参数的 RestController.

I have a Spring-Boot (1.5.3) application running on a Tomcat 8.5. Within it, i have a RestController with a single parameter.

这个参数是自动解码的——这对我的应用程序中的所有其他控制器都很好,但对于这个,我需要原始请求数据,因为它们将在另一个请求中使用.请注意,我只想禁用此控制器/请求的自动解码,并在其他地方保持正常行为.

This parameter is decoded automatically - which is fine for all other controllers within my application, but for this one, i need the raw request data, since they will be used within another request. Please note that i wold like to only disable the auto-decoding for this Controller/request and keep the normal behaviour everywhere else.

@RestController
@RequestMapping("/rest/test")
public class TestController {

  @RequestMapping("/test")
  public ResponseEntity<String> test(@RequestParam final String test){
    return ResponseEntity.ok("Requested: " + test);
  }
}

现在,如果我用

curl localhost/rest/test/test?test=%C3%B6%20%C3%A4

我收到输出:

Requested: ö ä

我想要

Requested: %C3%B6%20%C3%A4

有什么想法吗?

推荐答案

如果您只需要在一个地方进行这种行为,您可以使用标准 Java URLEncoder 位于控制器主体内部:

If you need this behavior only in a single place, you can encode the parameter back to the original form using the standard Java URLEncoder inside of the controller body:

@RequestMapping("/test")
public ResponseEntity<String> test(@RequestParam final String test) {
  String encodedParam = URLEncoder.encode(test, "UTF-8");
  return ResponseEntity.ok("Requested: " + encodedParam);
}

这篇关于Spring Boot:防止对@RequestParam 进行解码的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆