java - Spring Boot 接收JSON格式參數(shù)的問題。
問題描述
目前情況:自定義了GsonHttpMessageConverter來完成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;}期望情況:
請求的RequestBody數(shù)據(jù)長這樣:
{ 'name':'我是書名', 'price':23.33}
我希望在Controller中能這樣接收參數(shù):
@PostMappingObject operate(String name,Double price){ // 這里有一些操作 return null;}
在不討論這樣做是否合理的情況下,想請教大家該如何實現(xiàn)?
問題解答
回答1:根據(jù)你的期望情況來看,用ssm的話,直接用@requestparam來接收前端請求過來的參數(shù)即可,也可以自定義對象來接收這些參數(shù)。個人理解^~^ ...原諒我沒有用過springboot
相關(guān)文章:
1. javascript - 關(guān)于css絕對定位在ios瀏覽器被橡皮筋遮擋的問題2. python - beautifulsoup獲取網(wǎng)頁內(nèi)容的問題3. python - 能通過CAN控制一部普通的家用轎車嗎?4. mysql優(yōu)化 - 關(guān)于mysql分區(qū)5. javascript - react input file6. 人工智能 - python 機器學習 醫(yī)療數(shù)據(jù) 怎么學7. centos7 編譯安裝 Python 3.5.1 失敗8. mysql - 分庫分表、分區(qū)、讀寫分離 這些都是用在什么場景下 ,會帶來哪些效率或者其他方面的好處9. html5 - 只用CSS如何實現(xiàn)input框的寬度隨框里輸入的內(nèi)容長短自動適應?10. c++ - 請問MySQL_Connection::isReadOnly 怎么解決?
