Reputation: 533
Here is my class which I'm trying to use:
data class GetAppResponse(
val externalId: Long?,
val lastUpdate: LocalDateTime?,
var connectionInfo: ConnectionInfoDto? = null
) : BaseResponse()
data class ConnectionInfoDto(
val catalogUrl: String?,
val catalogPass: String?,
val catalogLogin: String?
)
When I'm trying to serialize it to json:
fun partner() {
partner.stubFor(get(anyUrl())
.willReturn(aResponse()
.withHeader("Content-Type", "application/json")
.withBody("{\"externalId\": 1, " +
"\"lastUpdate\": \"2020-01-01T10:00:00\", " +
"\"connectionInfo\": {" +
"\"catalogUrl\": " + archiveUrl + ", " +
"\"catalogPass\": \"user\", " +
"\"catalogLogin\": \"user\"}")
))
}
I'm getting
nested exception is org.springframework.http.converter.HttpMessageNotReadableException: JSON parse error: Unrecognized token 'http': was expecting (JSON String, Number, Array, Object or token 'null', 'true' or 'false'); nested exception is com.fasterxml.jackson.databind.JsonMappingException: Unrecognized token 'http': was expecting (JSON String, Number, Array, Object or token 'null', 'true' or 'false')
at [Source: (PushbackInputStream); line: 1, column: 94] (through reference chain: GetAppResponse["connectionInfo"])
What is a reason and how to solve the problem?
Upvotes: 0
Views: 4486
Reputation: 140
For example you archiveUrl value is asdsad so your full string body will be
{"externalId": 1, "lastUpdate": "2020-01-01T10:00:00", "connectionInfo": {"catalogUrl": asdsad, "catalogPass": "user", "catalogLogin": "user"}
and you can see that the catalogUrl value is not a string so giving the error.
So if you want to successfully parse the string to your object just change the catalogUrl value to represent in double quotes.
your string body
"{\"externalId\": 1, " +
"\"lastUpdate\": \"2020-01-01T10:00:00\", " +
"\"connectionInfo\": {" +
"\"catalogUrl\": \"" + archiveUrl + "\", " +
"\"catalogPass\": \"user\", " +
"\"catalogLogin\": \"user\"}"
and json response:-
{"externalId": 1, "lastUpdate": "2020-01-01T10:00:00", "connectionInfo": {"catalogUrl": "asdsad", "catalogPass": "user", "catalogLogin": "user"}
Upvotes: 1
Reputation: 2706
Replace:
"\"catalogUrl\": " + archiveUrl + ", " +
with this:
"\"catalogUrl\": \"" + archiveUrl + "\", " +
Upvotes: 1