0
Server gibt leer json mit JSONView:JSONView in Spring + Kotlin
[ { }, { } ]
Ich verwende nur Jackson2ObjectMapperBuilder
Bohne jackson
konfigurieren:
@Bean
open fun objectMapperBuilder() = Jackson2ObjectMapperBuilder()
.modulesToInstall(KotlinModule())
.propertyNamingStrategy(PropertyNamingStrategy.CAMEL_CASE_TO_LOWER_CASE_WITH_UNDERSCORES)
.featuresToEnable(SerializationFeature.WRITE_EMPTY_JSON_ARRAYS)
Controller:
@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
}
Fahrer Tabelle Einheit:
@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 = ""
)
Ansichten Objekt:
object Views {
interface Public {}
}
Liste Abhängigkeiten:
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}"
Wie JsonView
richtig in meinem Fall konfigurieren? Vielleicht habe ich etwas vergessen?
Nicht sicher ist dies helfen kann, aber ich sehe oft, dass JSONView Klassen verwendet, anstelle von Schnittstellen. Bitte versuche, 'class SampleView' zu erstellen und benutze es. Vielleicht würde es helfen. – IRus
Ich habe es versucht. Gleiches Problem –
Versuchen Sie, "Datenklasse" mit Klasse zu ersetzen, die in Java geschrieben wird, denke ich, dass es Problem mit Anmerkungen auf Datenklasseneigenschaften gibt. – IRus