Json 为RestController配置ObjectMapper
我的Spring boot应用程序中有两个控制器:RestControllerA使用snake_case返回JSON,RestController2使用camelCase返回JSON 有没有一种方法可以轻松地配置它?我现在正在做这样丑陋的事情:Json 为RestController配置ObjectMapper,json,spring-boot,kotlin,spring-restcontroller,Json,Spring Boot,Kotlin,Spring Restcontroller,我的Spring boot应用程序中有两个控制器:RestControllerA使用snake_case返回JSON,RestController2使用camelCase返回JSON 有没有一种方法可以轻松地配置它?我现在正在做这样丑陋的事情: @RestController class RestControllerA( @Qualifier("objectMapperSnakeCase") private val objectMapperSnakeCase: ObjectMap
@RestController
class RestControllerA(
@Qualifier("objectMapperSnakeCase")
private val objectMapperSnakeCase: ObjectMapper <-- I shouldn't have to inject this in.
) {
@GetMapping("/test-endpoint-1")
fun endpoint1(): ResponseEntity<String> { <-- I shouldn't have to return a string.
val employee = Employee(...)
val jsonString = objectMapperSnakeCase.writeValueAsString(employee)
return ResponseEntity.ok().body(jsonString)
}
}
@RestController
class RestControllerB(
@Qualifier("objectMapperCamelCase")
private val objectMapperCamelCase: ObjectMapper <-- I shouldn't have to inject this in.
) {
@GetMapping("/test-endpoint-2")
fun endpoint2(): ResponseEntity<String> { <-- I shouldn't have to return a string.
val employee = Employee(...)
val jsonString = objectMapperCamelCase.writeValueAsString(employee)
return ResponseEntity.ok().body(jsonString)
}
}
@RestController
类RestControllerA(
@限定符(“objectMapperSnakeCase”)
私有val objectMapperSnakeCase:ObjectMapper
@RestController
class RestControllerA {
@GetMapping("/test-endpoint-1")
@ObjectMapperToUse("objectMapperSnakeCase") <-- Nice and easy.
fun endpoint1(): Employee {
return Employee(...)
}
}
// Similar for RestControllerB with objectMapperCamelCase