如何在Spring Boot中处理RequestParam中的花括号 [英] How to handle curly braces in RequestParam in spring boot

查看:476
本文介绍了如何在Spring Boot中处理RequestParam中的花括号的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个带有GET服务的Spring Boot应用程序。

I have a spring boot application with a GET service.

@RequestMapping(value = "/abc/track/{type}", method = RequestMethod.GET)

    public void DummFunc(                            
          @RequestParam(value="subs", required = false) String sub,
,  HttpServletResponse response) {}

subs 的值

如果我将以下值作为参数传递给子变量

If I pass following as value to parameter subs

{%22endpoint%22:%22https://def.abc.com/tyu/send/eD3vpGNQW28:APA91bHOo8rYrV0xccdQz3okjZJG-QGrJX8LG6ahJnEUpMNfGedqi3hJxQsJx_8BMbH6oDjaSPPEXqzNWchWrGSkhuTkSdemikkys1U22Ipd7MsRw0owWbw89V2fslIVAJ6G5lkyvYuQ%22,%22expirationTime%22:null,%22keys%22:{%22p256dh%22:%22BK0pktn50CMsTQtqwdPlKlJtdFs0LFeXX14T1zgoz6QWnvSTp5dxtChnUP5P1JX0TsjcopbdPKyN31HfABMUyic%22,%22auth%22:%22qbO_z0vGao9wD-E5g8VU-A%22}}

无法捕获请求和控制未放入内部

It fails to capture the request and control does not come inside of the function.

如果我们改为将值作为参数传递给子变量:

If we instead pass as value to parameter subs:

%7B%22endpoint%22:%22https://def.abc.com/tyu/send/dX5q5eV7hFQ:APA91bHib-0QXrMzjatcvTR_uaIeeJK8lf6GmXUC9Jxv0Oxth-BzD4GmWnd4-YpDZv8qSFZ0eSg9mB2YkRvkc5ezdXW5KeaHjuQZfdyDxyBXjJgE-25Xbtlk37pdm8vfLk20k0k_VxW9%22,%22expirationTime%22:null,%22keys%22:%7B%22p256dh%22:%22BCCvcBLpRqp4u0auP688_MUJLsAiwWlQSn5kpyl8YVsEo_J-KpSdnhCmVIE_BhDXBcFYflPK52hqhYf3EaOCyuY%22,%22auth%22:%22iKuW_ESkCZnubWcQu_JK8w%22%7D%7D

工作正常。


  1. 为什么会这样?第一次编码有什么问题?

  1. Why is this happening? What's wrong with first encoding?

由于服务器无法处理请求,因此返回400。我需要捕获此类请求,然后通过编码处理它们他们正确。前进的方向是什么?

Since server is not able to handle the request, it returns 400. I need to capture such requests and then handle them by encoding them properly. What can be way forward?

我对Spring boot / Spring和Java本身是陌生的。

I am new to Spring boot/Spring and Java itself. Would be great if I can get some insight.

此外,我可以在此处在线解码两个文件,而不会出现任何问题- https://www.urldecoder.org/

Also, I can decode both of them online here without any issues- https://www.urldecoder.org/

编辑:基本上,具有问题的请求通过具有 {} 而不是%7B %7D

Basically, the request that has issue getting through has { and } instead of %7Band %7D.

我的问题不是应用程序因400错误请求而失败,如何捕获此类错误

推荐答案

这与Java无关,也与Spring本身,但是HTML URL编码参考。 URL只能使用ASCII字符集通过Internet发送。

This is not related to Java nor the Spring itself but the HTML URL Encoding Reference. URLs can only be sent over the Internet using the ASCII character set.

不安全字符定义在 RFC-1738 ,以下是列表:

The unsafe characters are defined in the beginning of RFC-1738 and here is the list:


<>#%{} | \ ^〜[]`包括空格/空白。

" < > # % { } | \ ^ ~ [ ] ` including the blank/empty space.

,还有保留字符,其中属于以下字符,用于区分参数,键值表示形式,端口等。

Aside from those, there are also reserved characters where belong the following ones and are used to distinguish the parameters, the key-value representation, the port etc.


; /?:@ =&

; / ? : @ = &

您使用的不安全字符是 {} 等于%7B %7D

The unsafe characters you have used are { and } which are equal to %7B and %7D.

本质上,您应该关注客户端以您描述的方式发送给您的数据服务器必须要求正确的格式和URL算了尽管浏览器和REST客户端会自动对这些字符进行编码,但是以编程方式发送它们可能会导致错误。我知道,Spring中仅有的两个可用解决方案是通过注册 CharacterEncodingFilter bean(已经已回答)或Spring-Boot配置:

Essentially, you should not be concerned about the data the client sends you in the way you describe. The server must demand the correct form and URL passed. Although, the browsers and REST clients encode those characters automatically, sending them programmatically might cause errors. The only two available solutions in Spring I am aware of is through registering the CharacterEncodingFilter bean (already answered) or the Spring-Boot configuration:

spring.http.encoding.charset=UTF-8
spring.http.encoding.enabled=true
spring.http.encoding.force=true

您需要先启用编码,然后对HTTP请求和响应强制执行。

You need to enable the encoding first and force on HTTP requests and responses.

这篇关于如何在Spring Boot中处理RequestParam中的花括号的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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