java - Problem with Spring Boot receiving JSON format parameters.
習慣沉默
習慣沉默 2017-06-23 09:14:33
0
1
1197

current situation:

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;
}

Expected situation:

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?

習慣沉默
習慣沉默

reply all(1)
刘奇

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

Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template