使用 RestTemplate 将 E 等特殊字符传递给 JSON Web 服务

Pan*_*her 4 spring json special-characters jackson resttemplate

我正在从数据库获取数据并调用其余的 json web 服务。我正在使用 spring RestTemplate 来调用 Web 服务。我添加了 jackson jar,spring 使用它来创建 json。\n但是,当我们得到像拉丁 e 这样带有尖锐 \xc3\x89 的字符时,我遇到问题。Jackson 没有转义它并按原样通过,这里 web 服务在另一端失败,给出 500 内部错误。我需要传递它的 unicode,如 \\u0209。\n我尝试使用 StringEcapeUtils 来转换它,它被转换为 \\u0209。\n但这一次RestTemplate再次将其转义为 \\u0209,并且在目标系统上以 \\u0209 形式接收。

\n\n

目标系统是另一个我们无法更改任何内容的系统。

\n\n

我的restTemplate 代码是:-

\n\n
MultiValueMap<String, String> headers =\n        new LinkedMultiValueMap<String, String>();\n        headers.add("Content-Type", MediaType.APPLICATION_JSON_VALUE); //Note Content-Type as opposed to Accept\n\n        HttpEntity<Object> entity = new HttpEntity<Object>(myRequest, headers);\n\n        myResponse = restTemplate.postForObject(url, entity, responseClass );\n
Run Code Online (Sandbox Code Playgroud)\n

And*_*rup 5

我猜服务器不知道如何解释您发送的内容。你有没有尝试过

MediaType mediaType = 
    new MediaType( "application" , "json" , Charset.forName( "UTF-8" ) ); 
headers.add( "Content-Type", mediaType );
Run Code Online (Sandbox Code Playgroud)

干杯,