Home > Software design >  Spring Boot REST API - Deserialise a query attribute with square braces with Jackson
Spring Boot REST API - Deserialise a query attribute with square braces with Jackson

Time:01-14

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?)

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&currency=INR got the response of Hello 5 Tom INR

  •  Tags:  
  • Related