Customized GsonHttpMessageConverter
to complete JSON -> Bean conversion. like this:
@Bean
public static Gson gsonBuilder(){
return new GsonBuilder()
.setFieldNamingPolicy(FieldNamingPolicy.LOWER_CASE_WITH_UNDERSCORES)
.serializeNulls()
.create();
}
@Bean
public GsonHttpMessageConverter gsonHttpMessageConverter(Gson gson) {
GsonHttpMessageConverter converter = new GsonHttpMessageConverter();
converter.setGson(gson);
return converter;
}
In Controller I use it like this:
@PutMapping
Object insert(@RequestBody Book book){
bookService.insertOne(book);
return book;
}
The requested RequestBody data looks like this:
{
"name":"我是书名",
"price":23.33
}
I hope to receive parameters like this in Controller:
@PostMapping
Object operate(String name,Double price){
// 这里有一些操作
return null;
}
Without discussing whether it is reasonable to do so, I would like to ask how you can achieve it?
怪我咯2017-06-23 09:16:33
Depending on your expectations, if you use SSM, you can directly use @requestparam to receive the parameters requested by the front end, or you can customize an object to receive these parameters. Personal understanding^~^...Forgive me for not using springboot