java - 表单数据休息 Web 服务中的德语/特殊字符支持
问题描述
我目前正在开发Angular JS
+中的小项目Java
,用户正在使用 rest webservice 使用他的个人资料图片注册他的信息。一切正常,除了特殊字符(Ä Ö Ü ä ö)
。
爪哇:
@POST
@Path("add_employee")
@Consumes(MediaType.MULTIPART_FORM_DATA)
@Produces(MediaType.APPLICATION_JSON)
public Response addEmployee(MultipartFormDataInput input) {
try {
Map<String, List<InputPart>> formDataMap = input.getFormDataMap();
if (formDataMap != null && !formDataMap.isEmpty()) {
InputPart inputPart = formDataMap.get("EmployeeProxy").get(0);
ObjectMapper mapper = new ObjectMapper();
//receiving wrong json below=>
EmployeeProxy admbo = mapper.readValue(inputPart.getBodyAsString(), EmployeeProxy.class);
List<InputPart> profilePic = formDataMap.get("profilePic");
.
.
.
} else {
return Response.status(Response.Status.NOT_FOUND).build();
}
} catch (Exception ex) {
return Response.status(Response.Status.INTERNAL_SERVER_ERROR).build();
}
}
角 JS:
var fd = new FormData();
fd.append('EmployeeProxy', angular.copy(JSON.stringify($scope.empInfo)));
fd.append('profilePic', $scope.myFile);
$http.post(Server.url + 'add_employee', fd, {
transformRequest: angular.identity,
headers: {'Content-Type': undefined}
}).then(function (response) {
});
发送 Json : {"empName": "Ä Ö Ü ä ö","empSurname": "XYZ","emailId": "abc@gmail.com"}
接收 Json : {"empName": "�� �� �� �� �� ��","empSurname": "XYZ","emailId": "abc@gmail.com"}
请在下图中找到请求标头信息:
如果我APPLICATION_JSON
不使用MULTIPART_FORM_DATA
.
解决方案
如果您的 Content-Type 标头是undefined
,RestEasy
则无法识别要使用的字符集并将回退到默认值 ( us-ascii
)。
另请参阅:覆盖多部分消息的默认后备内容类型
阅读后编辑:它应该是指定 Content-Type 的多部分正文,以便RestEasy
解析各个字符串。
在 FormData 的文档中,可以通过以下方式完成:
角 JS:
fd.append('EmployeeProxy', new Blob([angular.copy(JSON.stringify($scope.empInfo))], { type: "text/plain; charset=iso-8859-1"}));
爪哇:
String json = IOUtils.toString(inputPart.getBody(InputStream.class, null), StandardCharsets.UTF_8);
EmployeeProxy admbo = mapper.readValue(json, EmployeeProxy.class);
推荐阅读
- python - train_test_split 中的 X 参数是没有目标类的整个数据还是包含目标类?
- php - 如何从 SQL 获取数组值
- php - Curl 扩展在 Docker 中失败,出现错误“CURL_OPENSSL_3 not found”
- javascript - Rails 5 - 根据先前的 collection_select 更改 collection_select 的表单值
- react-native - 是否有可能在 react-native 的抽屉导航器中嵌套选项卡导航器?
- java - Spring AccessDeniedException 中的自定义消息
- java - 泛型方法是否可以从您传递给它的类型中调用方法
- r - 是否可以确定一行中的最大值是否唯一?
- css - 更改材质滑块拇指大小
- amazon-web-services - 无法从浏览器访问具有弹性 IP 地址的 AWS EC2 实例