Java 在带有弹簧靴座和摆动装置的收割台中使用utf-8字符时响应未加载
我有一个springboot应用程序。当我测试我的rest服务时,头中没有utf-8字符,一切正常。我可以使用Swagger generate命令来测试它:Java 在带有弹簧靴座和摆动装置的收割台中使用utf-8字符时响应未加载,java,rest,spring-boot,swagger,Java,Rest,Spring Boot,Swagger,我有一个springboot应用程序。当我测试我的rest服务时,头中没有utf-8字符,一切正常。我可以使用Swagger generate命令来测试它: curl -X GET --header 'Accept: application/json' --header 'user: me' --header 'reason: zst' --header 'Authorization: Basic dG9tYXM6dG9tYXMxMjM=' 'http://localhost:8080/my-a
curl -X GET --header 'Accept: application/json' --header 'user: me' --header 'reason: zst' --header 'Authorization: Basic dG9tYXM6dG9tYXMxMjM=' 'http://localhost:8080/my-app/uuid/756531B55A3D029A0894D7C9C4ACDF3EC0'
但当我大摇大摆地使用utf-8字符时,我没有得到响应。斯威格只是在装东西。当我查看firefox的控制台时,我只看到一些get请求:
http://localhost:8080/my-app/webjars/springfox-swagger-ui/images/throbber.gif
没有回应
当我尝试编辑上面的curl
命令时:
curl -X GET --header 'Accept: application/json' --header 'user: me' --header 'reason: žšť' --header 'Authorization: Basic dG9tYXM6dG9tYXMxMjM=' 'http://localhost:8080/my-app/uuid/756531B55A3D029A0894D7C9C4ACDF3EC0'
一切正常,所以我想我的后端没有问题。有什么方法可以修复/调试此问题吗?根据,消息头定义如下:
message-header = field-name ":" [ field-value ]
field-name = token
field-value = *( field-content | LWS )
field-content = <the OCTETs making up the field-value
and consisting of either *TEXT or combinations
of token, separators, and quoted-string>
换句话说,头的值只能是
ISO-8859-1
编码的,如果您想对未包含在这个字符集中的字符进行编码(这里就是这种情况),那么您应该根据RFC 2047也被称为MIME(多用途Internet邮件扩展名)对其进行编码第三部分:非ASCII文本的消息头扩展
因此而不是
reason: žšť
应该是
reason: =?utf-8?q?=C5=BE=C5=A1=C5=A5?=
在实践中,甚至建议在不只有US-ASCII
字符时对值进行编码
最后要检查的是,您的JAX-RS实现是否支持开箱即用的RFC 2047,如果不支持,则需要手动解码,例如使用实用程序方法
这里有一个具体的例子:
@GET
public Response showHeader(@HeaderParam("reason") String reason)
throws UnsupportedEncodingException {
// Decode it
reason = MimeUtility.decodeText(reason);
// Return the value of the decoded String
return Response.status(Response.Status.OK)
.entity(String.format("Value of reason = '%s'", reason))
.build();
}
使用带有--header'原因的curl调用此资源方法:=?utf-8?q?=C5=BE=C5=A1=C5=A5?='
给出了预期的结果:
Value of reason = 'žšť'
NB:要从js前端对标题值进行编码,您可以使用该库,下面是一个很好的答案。thx man。我会尽快得到你的奖励。还有一个问题。作为前端,我们使用角度。是否有一些方法可以根据您上面提到的RFC对本文进行编码?
@GET
public Response showHeader(@HeaderParam("reason") String reason)
throws UnsupportedEncodingException {
// Decode it
reason = MimeUtility.decodeText(reason);
// Return the value of the decoded String
return Response.status(Response.Status.OK)
.entity(String.format("Value of reason = '%s'", reason))
.build();
}
Value of reason = 'žšť'