Binding using POJOs
Note however, that if your method does not do any blocking I/O then you can just as easily write:
Binding JSON POJOs
@Controller("/people")
public class PersonController {
Map<String, Person> inMemoryDatastore = new ConcurrentHashMap<>();
@Post
public HttpResponse<Person> save(@Body Person person) {
inMemoryDatastore.put(person.getFirstName(), person);
return HttpResponse.created(person);
}
}
Binding JSON POJOs
@Controller("/people")
class PersonController {
ConcurrentHashMap<String, Person> inMemoryDatastore = [:]
@Post
HttpResponse<Person> save(@Body Person person) {
inMemoryDatastore.put(person.getFirstName(), person)
HttpResponse.created(person)
}
}
Binding JSON POJOs
@Controller("/people")
class PersonController {
internal var inMemoryDatastore: MutableMap<String, Person> = ConcurrentHashMap()
@Post
fun save(@Body person: Person): HttpResponse<Person> {
inMemoryDatastore[person.firstName] = person
return HttpResponse.created(person)
}
}
Micronaut will still using non-blocking I/O to read the JSON and only execute your method once the data has been read.
In other words, as a rule reactive types should be used when you plan to do further downstream I/O operations in which case they can greatly simplify composing operations.
The output produced by Jackson can be customized in a variety of manners, from defining Jackson modules to using Jackson’s annotations |