1

I've this class

@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"
  }
}

I want to change json key name from "category" to "name" I don't know why it isn't working? When I used java it was work as well (@JsonProperty)

Dmytro Mitin
  • 48,194
  • 3
  • 28
  • 66
overmyhea
  • 15
  • 6

1 Answers1

3

Add

libraryDependencies += "com.fasterxml.jackson.module" % "jackson-module-scala_2.12" % "2.9.0"

to build.sbt. This dependency isn't added by Spring Boot.

Then the following code works:

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)

{"name":"name1","type":"category1"}

Based on the answer.


You haven't registered the module. Modify your Spring Boot configuration:

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

Based on the answer.

screenshot

Dmytro Mitin
  • 48,194
  • 3
  • 28
  • 66
  • It doesn't work. I mean when I use println(it is work) but when I return object in controller I get category instead of type. Ofc I can return String, but I want to return json. Basically list of TimeTableIndexItem. – overmyhea Sep 07 '17 at 14:26
  • https://github.com/stoton/zsat-timetable-backend it's my code without this changes what are you talking about. Look at controller – overmyhea Sep 07 '17 at 15:02
  • yeah, sorry for not replay – overmyhea Sep 10 '17 at 08:26