Harish Pal
02/10/2025, 11:04 AMUnable to Parse JSON.
Request Payload:
{
"eventTypeId": "USR-05 \" "
}
REST Controller
@Produces(MediaType.APPLICATION_JSON)
@Path("/audit/api/1/events")
open class WriteAuditEventResource {
@POST
@Consumes(MediaType.APPLICATION_JSON)
fun writeEvent(eventRequest: AuditEventRequest): Response {
return Response.status(Response.Status.CREATED).build()
}
}
Problem:
1. In HTTP Logs I can see that the escaped backslash is converted to double backslash (making the JSON invalid). Below are the logs:
{
"eventTypeId": "USR-05 \\" "
}
[o.e.jetty.http.HttpParser ] - parseNext s=CONTENT HeapByteBuffer@64870bee[p=273,l=308,c=8192,r=35]={POST /aud... 35\r\n\r\n<<<{\n "eventTypeId": "USR-05 \\" "\n}>>>\x00\x00\x00\x00\x00\x00\x00\x00\x00...\x00\x00\x00\x00\x00\x00\x00}
[o.e.jetty.server.HttpChannel ] - onContent HttpChannelOverHttp@f084976{s=HttpChannelState@4962b62c{s=HANDLING rs=BLOCKING os=OPEN is=IDLE awp=false se=false i=true al=1},r=1,c=false/false,a=HANDLING,uri=//localhost:8686/audit/api/1/events/,age=35} Content@2bcdd012{HeapByteBufferR@793bf03f[p=273,l=308,c=8192,r=35]={POST /aud... 35\r\n\r\n<<<{\n "eventTypeId": "USR-05 \\" "\n}>>>\x00\x00\x00\x00\x00\x00\x00\x00\x00...\x00\x00\x00\x00\x00\x00\x00}}
[o.e.jetty.server.HttpInput ] - HttpInputOverHTTP@3716942e[c=0,q=0,[0]=null,s=STREAM] addContent Content@2bcdd012{HeapByteBufferR@793bf03f[p=273,l=308,c=8192,r=35]={POST /aud... 35\r\n\r\n<<<{\n "eventTypeId": "USR-05 \\" "\n}>>>\x00\x00\x00\x00\x00\x00\x00\x00\x00...\x00\x00\x00\x00\x00\x00\x00}}
[o.e.jetty.http.HttpParser ] - CONTENT --> CONTENT_END
[o.e.jetty.server.HttpChannel ] - onContentComplete HttpChannelOverHttp@f084976{s=HttpChannelState@4962b62c{s=HANDLING rs=BLOCKING os=OPEN is=IDLE awp=false se=false i=true al=1},r=1,c=false/false,a=HANDLING,uri=//localhost:8686/audit/api/1/events/,age=36}
[o.e.jetty.http.HttpParser ] - CONTENT_END --> END
2. In Java Servlet Filter, if I extract the request payload from HttpServlet then I am getting below string as input:
{
"eventTypeId": "USR-05 " "
}
StackTrace:
[.JsonProcessingExceptionMapper] - Unable to process JSON ! com.fasterxml.jackson.databind.JsonMappingException: Unexpected character ('"' (code 34)): was expecting comma to separate Object entries
! at [Source: (org.glassfish.jersey.message.internal.ReaderInterceptorExecutor$UnCloseableInputStream); line: 1, column: 395] (through reference chain: com.anaplan.audit.write.api.request.AuditEventRequest["eventTypeId"])
! at com.fasterxml.jackson.databind.JsonMappingException.wrapWithPath(JsonMappingException.java:402)
! at com.fasterxml.jackson.databind.JsonMappingException.wrapWithPath(JsonMappingException.java:361)
! at com.fasterxml.jackson.databind.deser.BeanDeserializerBase.wrapAndThrow(BeanDeserializerBase.java:1826)
! at com.fasterxml.jackson.databind.deser.BeanDeserializer.deserialize(BeanDeserializer.java:280)
! at com.fasterxml.jackson.module.afterburner.deser.SuperSonicBeanDeserializer.deserialize(SuperSonicBeanDeserializer.java:155)
! Caused by: com.fasterxml.jackson.core.JsonParseException: Unexpected character ('"' (code 34)): was expecting comma to separate Object entries
! at [Source: (org.glassfish.jersey.message.internal.ReaderInterceptorExecutor$UnCloseableInputStream); line: 1, column: 395]
! at com.fasterxml.jackson.core.JsonParser._constructError(JsonParser.java:2418)
! at com.fasterxml.jackson.core.base.ParserMinimalBase._reportError(ParserMinimalBase.java:749)
! at com.fasterxml.jackson.core.base.ParserMinimalBase._reportUnexpectedChar(ParserMinimalBase.java:673)
! at com.fasterxml.jackson.core.json.UTF8StreamJsonParser.nextFieldName(UTF8StreamJsonParser.java:1061)
! at com.fasterxml.jackson.databind.deser.std.MapDeserializer._readAndBindStringKeyMap(MapDeserializer.java:608)
More Details:
Kotlin Version: 1.3.0
JDK version: 1.8
jackson-module-kotlin: 2.8.9
Could you please help me with this, how can I process this request containing the escaped double quote?Rob Elliot
02/10/2025, 1:02 PMHarish Pal
02/10/2025, 4:14 PMHarish Pal
02/10/2025, 4:19 PMcurl --location 'https://***.com/audit/*/events/' --header 'Content-Type: application/json' --data '{"eventTypeId":"Escape Double Quote \" "}'
Rob Elliot
02/10/2025, 5:25 PMRob Elliot
02/10/2025, 5:30 PMDEBUG [2025-02-10 17:29:18,231] req= org.eclipse.jetty.http.HttpParser: parseNext s=CONTENT DirectByteBuffer@41793ff3[p=229,l=262,c=8192,r=33]={POST /__/...080\r\n\r\n<<<{\n "eventTypeId": "USR-05 \\" "\n}>>>\x00\x00\x00\x00\x00\x00\x00\x00\x00...\x00\x00\x00\x00\x00\x00\x00}
but it's still parsed fine by Jackson, so the double backslash seems to be an artefact of the Jetty logging rather than a sign of the problem.Rob Elliot
02/10/2025, 5:39 PMHarish Pal
02/11/2025, 6:39 AMjackson: 2.14.1
jackson-databind: 2.14.1
jackson-core: 2.14.1
jackson-annotations: 2.14.1
jackson-module-jsonSchema: 2.14.1
jackson-module-kotlin: 2.8.9
dropwizard-jetty: 2.1.10
jetty-server: 9.4.53.v20231009
jetty-servlets: 9.4.53.v20231009
dropwizard-jersey: 2.1.10
jersey-client: 2.26
jersey-media-multipart: 2.26
Harish Pal
02/13/2025, 3:15 PM