简体   繁体   中英

Java + jackson parsing error Unrecognized character escape

I need to do a POST json string , using HttpClient. Following will be the code i have. From the other end the Json is mapped to an object.

HttpClient client = HttpClientBuilder.create().build();
HttpPost post = new HttpPost(url);
String jsonData = "{ \"provider\" : null , \"password\" : \"a\", \"userid\" : \"mlpdemo\\mlpdemoins\" }";
post.setEntity(new ByteArrayEntity(         jsonData.toString().getBytes("UTF8")));
HttpResponse response = client.execute(post);

Here all others are correctly mapping expect the userId. Here the problem is with the backward slash( mlpdemo\\mlpdemins ). I guess. If I send a single string as the user id it will be mapped without any issues. Eg:-

String jsonData = "{ \"provider\" : null , \"password\" : \"a\", \"userid\" : \"mlpdemoins\" }";

This works .

But I need this ( mlpdemo\\mlpdemins )to be sent through the POSt. Please help me out.

String jsonData = "{ \"provider\" : null , \"password\" : \"a\", \"userid\" : \"mlpdemo\\mlpdemoins\" }";

Here is the exception Im getting.

com.fasterxml.jackson.core.JsonParseException: Unrecognized character escape 'm' (code 109)
 at [Source: java.io.InputStreamReader@29f0a0a2; line: 1, column: 62]
BadRequestException (0ea35150-f33a-4932-a31e-8a1048af53ad): 400 Bad Request, com.strategicgains.restexpress.serialization.DeserializationException: com.fasterxml.jackson.core.JsonParseException: Unrecognized character escape 'm' (code 109)
 at [Source: java.io.InputStreamReader@29f0a0a2; line: 1, column: 62]
    at com.strategicgains.restexpress.Request.getBodyAs(Request.java:165)
    at com.strategicgains.restexpress.Request.getBodyAs(Request.java:181)

设置你的映射器

mapper.configure(JsonParser.Feature.ALLOW_BACKSLASH_ESCAPING_ANY_CHARACTER, true); 

mlpdemo\\mlpdemoins is an invalid string you can't use it in JSON . But you can use mlpdemo\\\\mlpdemoins easily.

below code works fine for me :

String jsonData = "{ \"provider\" : null , \"password\" : \"a\", \"userid\" : \"mlpdemo\\\\mlpdemoins\" }";

ObjectMapper mapper=new ObjectMapper();

System.out.println(mapper.readTree(jsonData));

It will produce this output JSON :

{"provider":null,"password":"a","userid":"mlpdemo\\mlpdemoins"}

Since version 2.10 (released Sep, 2019), JsonParser.Feature.ALLOW_BACKSLASH_ESCAPING_ANY_CHARACTER has been deprecated, together with some other properties:

@deprecated Since 2.10 use {@link com.fasterxml.jackson.core.json.JsonReadFeature#ALLOW_BACKSLASH_ESCAPING_ANY_CHARACTER} instead

While the properties at the time still exist in the current version ( 2.11.3 ), it has actually been removed from the GIT master branch , suggesting that it will probably be removed when version 3.0.0 is released.

The correct way to configure this property is instead by using JsonReadFeature instead. Instead of creating an ObjectMapper and call configure() on it, use JsonMapper and its builder() method:

ObjectMapper mapper = JsonMapper.builder()
    .enable(JsonReadFeature.ALLOW_BACKSLASH_ESCAPING_ANY_CHARACTER)
    .build();

This returns a JsonMapper , which is a sub class of ObjectMapper , with the property enabled.


The related GitHub issue can be found here , and this issue suggests some alternatives on how to achieve the configuration.

In my case I was using Spring so setting this property in the application.yml did the job:

spring:
  jackson:
    parser:
      allow-backslash-escaping-any-character: true

This GitHub comment was helpful: https://github.com/FasterXML/jackson-core/issues/586#issuecomment-672199588

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM