i am calling a legacy rest endpoint. There i have to deserialise a query parameter in a rest endpoint with square braces author[name].
Question: Is it possible to deserialise the attribute author name in a rest endpoint with square braces with jackson annotations (spring boot kotlin)?
@RestController
class AuthorController {
@GetMapping("/author/{id}")
fun getAuthorFromLegacyApi(@PathVariable("id") id: Long, authorDto: AuthorDto) = ResponseEntity.ok(authorDto.name)
}
data class AuthorDto(@JsonProperty("author[name]") val name: String?)
- code: https://github.com/nusmanov/spring-boot-validation-hibernate/blob/main/src/main/kotlin/com/example/bookvalidation/AuthorController.kt
- to test: GET http://localhost:8080/author/5?author[name]=Tom
- there is a junit test see above
CodePudding user response:
Yes, it is possible, if you annotate authorDto with @RequestParam("author[name]"):
@GetMapping("/author/{id}")
fun getAuthorFromLegacyApi(@PathVariable("id") id: Long, @RequestParam("author[name]") authorDto: AuthorDto) = ResponseEntity.ok(authorDto.name)
CodePudding user response:
How about using the map.
@GetMapping("/author/{id}")
fun getAuthorFromLegacyApi(@PathVariable("id") id: Long, @RequestParam authorDto: Map<String, String>): String {
return "Hello $id ${authorDto["author[name]"]} ${authorDto["currency"]}"
}
I tried with author/5?author[name]=Tom¤cy=INR got the response of Hello 5 Tom INR
