Harish Pal
02/10/2025, 11:26 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: *, column: ***] (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
toadzky
02/10/2025, 7:59 PMHarish Pal
02/11/2025, 7:30 AMtoadzky
02/11/2025, 4:10 PMHarish Pal
02/12/2025, 4:07 PMtoadzky
02/12/2025, 4:36 PMHarish Pal
02/13/2025, 3:14 PMtoadzky
02/17/2025, 4:00 PMtoadzky
02/17/2025, 4:00 PMtoadzky
02/17/2025, 4:05 PM@Test
fun `do the thing`() {
data class Test(val str: String)
val mapper = jacksonObjectMapper()
print(mapper.writeValueAsString(Test("escaped \" quote \" ")))
}
i get
{
"str": "escaped \" quote \" "
}
which is exactly what it should beHarish Pal
02/20/2025, 5:14 AMtoadzky
02/20/2025, 3:49 PMHarish Pal
02/21/2025, 8:46 AM