Spring + Kotlin中的JsonView [英] JsonView in Spring + Kotlin
本文介绍了Spring + Kotlin中的JsonView的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
服务器使用JsonView返回空的json:
Server returns empty json with JsonView:
[ { }, { } ]
我仅使用Jackson2ObjectMapperBuilder
bean来配置jackson
:
I use only Jackson2ObjectMapperBuilder
bean to configure jackson
:
@Bean
open fun objectMapperBuilder() = Jackson2ObjectMapperBuilder()
.modulesToInstall(KotlinModule())
.propertyNamingStrategy(PropertyNamingStrategy.CAMEL_CASE_TO_LOWER_CASE_WITH_UNDERSCORES)
.featuresToEnable(SerializationFeature.WRITE_EMPTY_JSON_ARRAYS)
控制器:
@RestController
@RequestMapping("/api/drivers")
class DriversController @Autowired constructor(val driverService: DriverService) {
@JsonView(Views.Public::class)
@RequestMapping("/nearest")
fun nearest(): List<Driver> {
val drivers = driverService.findNearest(49.437551, 32.025263)
return drivers
}
驱动程序表实体:
@Entity
@Table(name = "ct_drivers")
data class Driver(
@Id
@Column(name = "id")
@GeneratedValue(strategy = GenerationType.IDENTITY)
var id: Int = 0,
@JsonView(Views.Public::class)
@Column(name = "first_name")
var firstName: String = "",
@JsonView(Views.Public::class)
@Column(name = "last_name")
var lastName: String = "",
@Column(name = "phone")
var phone: String = ""
)
查看对象:
object Views {
interface Public {}
}
依赖项列表:
compile 'org.springframework.boot:spring-boot-starter-web'
compile 'org.springframework.boot:spring-boot-devtools'
compile 'org.springframework.boot:spring-boot-starter-data-jpa'
compile 'org.springframework.boot:spring-boot-starter-thymeleaf'
compile 'org.springframework.security.oauth:spring-security-oauth2'
compile 'org.springframework:spring-messaging'
compile 'org.springframework:springloaded:1.2.5.RELEASE'
compile 'org.springframework:spring-tx'
compile 'org.springframework:spring-orm:4.2.5.RELEASE'
compile 'org.hibernate:hibernate-core:5.1.0.Final'
compile 'org.hibernate:hibernate-entitymanager:5.1.0.Final'
compile 'org.hibernate:hibernate-spatial:5.1.0.Final'
compile 'com.corundumstudio.socketio:netty-socketio:1.7.8'
compile 'org.postgresql:postgresql:9.3-1101-jdbc41'
compile 'com.github.salomonbrys.kotson:kotson:2.1.0'
compile 'com.fasterxml.jackson.module:jackson-module-kotlin:2.7.3'
compile 'org.apache.httpcomponents:httpcore:4.3.3'
compile 'org.apache.httpcomponents:httpclient:4.3.3'
compile "org.jetbrains.kotlin:kotlin-stdlib:${kotlinVersion}"
在我的情况下如何正确配置JsonView
?可能我忘记了什么吗?
How to configure JsonView
correctly in my case? May be I forgot something?
推荐答案
在添加以下依赖项之前,我一直遇到相同的问题:
I was running into the same problem until I added the following dependency:
compile 'com.fasterxml.jackson.module:jackson-module-kotlin'
这篇关于Spring + Kotlin中的JsonView的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文