Scala with spring boot-更改json键的问题

Scala with spring boot-更改json键的问题,json,spring,scala,spring-boot,Json,Spring,Scala,Spring Boot,我上过这门课 @JsonSerialize case class TimeTableIndexItem(@BeanProperty @JsonProperty("name") var name: String, @BeanProperty @JsonProperty("type") var category: String) extends Serializable { override def toString: String =

我上过这门课

@JsonSerialize
case class TimeTableIndexItem(@BeanProperty @JsonProperty("name") var name: String,
                              @BeanProperty @JsonProperty("type") var category: String) extends Serializable {
  override def toString: String = {
    s"$name $category"
  }
}
我想把json键名从“category”改为“name”,我不知道为什么它不起作用?当我使用java时,它也可以工作(@JsonProperty)

Add

libraryDependencies += "com.fasterxml.jackson.module" % "jackson-module-scala_2.12" % "2.9.0"
建立.sbt。Spring Boot没有添加此依赖项

那么下面的代码就可以工作了:

import com.fasterxml.jackson.databind.ObjectMapper
import com.fasterxml.jackson.module.scala.DefaultScalaModule

val objectMapper = new ObjectMapper
objectMapper.registerModule(DefaultScalaModule)
val item = TimeTableIndexItem("name1", "category1")
val s = objectMapper.writeValueAsString(item)
println(s)
{“名称”:“名称1”,“类型”:“类别1”}

基于


您尚未注册模块。修改Spring引导配置:

@Configuration
@EnableAutoConfiguration
@ComponentScan
class SampleConfig {
  @Bean
  @Primary
  def objectMapper(): ObjectMapper = {
    val objectMapper = new ObjectMapper
    objectMapper.registerModule(DefaultScalaModule)
    objectMapper
  }
}
基于


它不起作用。我的意思是,当我使用println时(它是工作的),但当我在控制器中返回对象时,我得到的是类别而不是类型。Ofc我可以返回字符串,但我想返回json。基本上是TimeTableIndexItem的列表。这是我的代码,没有这些变化你在说什么。看管制员耶,很抱歉没有重播