Sup guys. Learning Scala with Spring. Have searched a lot and couldn't find anything that really helped. I developed on Node.js before and I'm confused a bit now.
I do not understand how I should send JSON in Response. I want something like that (at least I'd do similar on js):
@RestController
@RequestMapping(path = Array("/api"))
class Auth {
@GetMapping(path = Array("/users"))
def getString(@RequestParam(value = "id") id: String): User = {
val user: User = Users.searchUser(id)
user
}
}
So the problem is: I can't send JSON. I need to serialize it, as I understand, because Scala can't do such a thing by itself. I found next next type for Response : MediaType.APPLICATION_JSON_VALUE, e.g.
@PostMapping(path = Array("/users"), produces = Array(MediaType.APPLICATION_JSON_VALUE))
And for testing I overrided toString function of class, that contains information about users, so it builds json-similar string. It works, but it is not the solution, it's awful. Also I have an error if I try using MediaType.APPLICATION_JSON (Type mismatch: expected %any_scala_type%, actual: MediaType).
How should I create JSON / serialize objects into JSON to send it to client?