0

I'm playing around with replacing Java with Scala in my Spring MVC application and so far everything works fine except for serialization of case case class in return body.

Controller looks like this:

@RestController
@RequestMapping(Array("/level"))
class GameMapController {

@Autowired
private val gameMapService: GameMapService = null

@RequestMapping(value = Array("/{levelNumber}"), method = Array(RequestMethod.GET))
def getGameMap(@PathVariable levelNumber: Int): ResponseEntity[GameMap] = {
  new ResponseEntity[GameMap](gameMapService.getGameMap(levelNumber), HttpStatus.OK)
}

and case class to be returned in body looks like this:

@Document
case class GameMap(@Id id: String, levelNumber: Int, width: Int, height: Int)

I checked if GameMap object have any properties set but i still receive empty Json in response. What did I miss? Do I need to provide some custom deseralization for Scala class?

3
  • Not too sure how to work Spring MVC with Scala, but are you actually returning anything in your method Commented Jul 28, 2016 at 2:20
  • Can you debug if you are getting proper levelNumber and for that you are getting proper GameMap from function gameMapService.getGameMap() ? Commented Jul 28, 2016 at 3:47
  • I did. It's not a problem of not retrieving any data. Commented Jul 28, 2016 at 10:48

1 Answer 1

2

This is quite a old thread. But answering this as I faced same issue myself.

I solved this by adding Scala Annotation @BeanProperty

@Document
case class GameMap(@Id @BeanProperty id: String, @BeanProperty levelNumber: Int, @BeanProperty width: Int, @BeanProperty height: Int)

This creates a getter & setters following the Java Bean convention which is recognize by mapper.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.