首页 > 解决方案 > 表单数据休息 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.

标签: javaangularjsrestjbossjax-rs

解决方案


如果您的 Content-Type 标头是undefinedRestEasy则无法识别要使用的字符集并将回退到默认值 ( 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); 

推荐阅读