java - Spring Boot 接收J(rèn)SON格式參數(shù)的問(wèn)題。
問(wèn)題描述
目前情況:自定義了GsonHttpMessageConverter來(lái)完成JSON -> Bean的轉(zhuǎn)換。像這樣:
@Beanpublic static Gson gsonBuilder(){ return new GsonBuilder() .setFieldNamingPolicy(FieldNamingPolicy.LOWER_CASE_WITH_UNDERSCORES) .serializeNulls() .create();}@Beanpublic GsonHttpMessageConverter gsonHttpMessageConverter(Gson gson) { GsonHttpMessageConverter converter = new GsonHttpMessageConverter(); converter.setGson(gson); return converter;}
在Controller中我這樣用:
@PutMappingObject insert(@RequestBody Book book){ bookService.insertOne(book); return book;}期望情況:
請(qǐng)求的RequestBody數(shù)據(jù)長(zhǎng)這樣:
{ 'name':'我是書名', 'price':23.33}
我希望在Controller中能這樣接收參數(shù):
@PostMappingObject operate(String name,Double price){ // 這里有一些操作 return null;}
在不討論這樣做是否合理的情況下,想請(qǐng)教大家該如何實(shí)現(xiàn)?
問(wèn)題解答
回答1:根據(jù)你的期望情況來(lái)看,用ssm的話,直接用@requestparam來(lái)接收前端請(qǐng)求過(guò)來(lái)的參數(shù)即可,也可以自定義對(duì)象來(lái)接收這些參數(shù)。個(gè)人理解^~^ ...原諒我沒(méi)有用過(guò)springboot
相關(guān)文章:
1. [python2]local variable referenced before assignment問(wèn)題2. 求救一下,用新版的phpstudy,數(shù)據(jù)庫(kù)過(guò)段時(shí)間會(huì)消失是什么情況?3. mysql - 如何在有自增id的情況下,讓其他某些字段能不重復(fù)插入4. python小白,關(guān)于函數(shù)問(wèn)題5. django - Python error: [Errno 99] Cannot assign requested address6. angular.js - 百度支持_escaped_fragment_嗎?7. java - 線上應(yīng)用,如果數(shù)據(jù)庫(kù)操作失敗的話應(yīng)該如何處理?8. node.js - win 下 npm install 遇到了如下錯(cuò)誤 會(huì)導(dǎo)致 無(wú)法 run dev么?9. python小白 關(guān)于類里面的方法獲取變量失敗的問(wèn)題10. Python2中code.co_kwonlyargcount的等效寫法
