java 板


LINE

hello 大家好. 最近在spring mvc上面,透過Postman以Post的方式傳送json的格式(使用body)給controll的程式處理. 參考了網路上的做法, 但是得到以下的錯誤. 想請教一下大家,xml需要做額外設定嗎?例如:web.xml or dispatcher-servlet.xml 或是程式上有甚麼問題?? HTTP Status 400 – Bad Request Type Status Report Description The server cannot or will not process the request due to something that is perceived to be a client error (e.g., malformed request syntax, invalid request message framing, or deceptive request routing). 1.輸入的json資料是(透過Post) {"id" : "101", "book" : "hhh", "name" : "u68t76"} 2.處理的程式如下: @RestController @RequestMapping("/myMain") public class MyMainController { @RequestMapping(value = "/create", method = RequestMethod.POST, consumes="application/json", produces = "application/json") public @ResponseBody List<JsonStr> getData(@RequestBody JsonStr jsonStr) { //debug System.out.println(jsonStr); repository.saveObject(jsonStr);//此段是處理mongoDB System.out.println(repository.getAllObjects());//此段是處理mongoDB return repository.getAllObjects(); } } 然後我的JsonStr是這樣撰寫的 import org.springframework.data.mongodb.core.mapping.Document; @Document(collection="JsonStr") public class JsonStr{ private String id; private String book; private String name; public JsonStr(String id, String book, String name) { this.id = id; this.book = book; this.name = name; } public String getId() { return id; } public String getBook() { return book; } public String getName() { return this.name; } public void setId(String id) { this.id = id; } public void setBook(String book) { this.book = book; } public void setName(String name) { this.name = name; } @Override public String toString() { return "book=" + book + ", name=" + name; } } 補充 web.xml <web-app version="3.0" xmlns="http://java.sun.com/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd"> <listener> <listener-class> org.springframework.web.context.ContextLoaderListener </listener-class> </listener> <display-name>Archetype Created Web Application</display-name> <servlet> <servlet-name>mvc-dispatcher</servlet-name> <servlet-class> org.springframework.web.servlet.DispatcherServlet </servlet-class> <init-param> <param-name>contextConfigLocation</param-name> <param-value>classpath:/spring/mvc-dispatcher-servlet.xml</param-value> </init-param> <load-on-startup>1</load-on-startup> </servlet> <servlet-mapping> <servlet-name>mvc-dispatcher</servlet-name> <url-pattern>/</url-pattern> </servlet-mapping> <context-param> <param-name>contextConfigLocation</param-name> <param-value>classpath:/spring/mvc-dispatcher-servlet.xml</param-value> </context-param> </web-app> mvc-dispatcher-servlet.xml <?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:context="http://www.springframework.org/schema/context" xmlns:mvc="http://www.springframework.org/schema/mvc" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation=" http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd"> <context:component-scan base-package="com.springmvc.demo" /> <mvc:annotation-driven /> <bean id="natureRepository" class="com.springmvc.demo.repository.NatureRepositoryImpl"> <property name="mongoTemplate" ref="mongoTemplate" /> </bean> <bean id="mongoTemplate" class="org.springframework.data.mongodb.core.MongoTemplate"> <constructor-arg name="mongo" ref="mongo" /> <constructor-arg name="databaseName" value="nature" /> </bean> <!-- Factory bean that creates the Mongo instance --> <bean id="mongo" class="org.springframework.data.mongodb.core.MongoFactoryBean"> <property name="host" value="localhost" /> <property name="port" value="27017" /> </bean> <!-- Activate annotation configured components --> <context:annotation-config /> <!-- Scan components for annotations within the configured package --> <context:component-scan base-package="com.orangeslate.naturestore"> <context:exclude-filter type="annotation" expression="org.springframework.context.annotation.Configuration" /> </context:component-scan> <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver"> <property name="prefix"> <value>/WEB-INF/views/</value> </property> <property name="suffix"> <value>.jsp</value> </property> </bean> </beans> --



※ 發信站: 批踢踢實業坊(ptt.cc), 來自: 175.98.141.254
※ 文章網址: https://webptt.com/m.aspx?n=bbs/java/M.1512638334.A.B30.html
1F:→ angleevil: System.out.println(jsonStr);是我插log, 根本沒資料 12/07 17:20
2F:→ angleevil: 列印出來. 可能要麻煩大家幫忙一下 12/07 17:21
※ 編輯: angleevil (175.98.141.254), 12/07/2017 17:22:37
3F:→ wvwvwvwvwv: jsonStr.toString() ? 12/07 18:08
4F:→ haha02: postman的content type有選對嗎? 12/07 22:26
5F:→ Expsun: 你用List接object 12/07 22:27
請問是甚麼意思?? repository.getAllObjects() 會return 一個List<JsonStr> public class NatureRepositoryImpl implements Repository<JsonStr>{ @Autowired(required=true) MongoTemplate mongoTemplate; public void setMongoTemplate(MongoTemplate mongoTemplate) { this.mongoTemplate = mongoTemplate; } @Override public List<JsonStr> getAllObjects() { // TODO Auto-generated method stub return mongoTemplate.findAll(JsonStr.class); } }
6F:→ haha02: 這個參考看看 https://goo.gl/5pbCio 12/07 22:29
太神奇了...竟然是constructor的問題. 移除後就好了!! ※ 編輯: angleevil (61.218.53.138), 12/08/2017 08:34:41 ※ 編輯: angleevil (61.218.53.138), 12/08/2017 08:49:00
7F:→ ssccg: 要給auto data binding lib的要有default constructor是滿 12/08 09:12
8F:→ ssccg: 基本的吧 12/08 09:12
9F:→ ssccg: 這不限於spring(jackson)應該大多數都是這樣 12/08 09:16
10F:→ kyleJ: 不需要把建構子移除 而是要多給一個無參數建構子 預設建構 12/08 19:38
11F:→ kyleJ: 子是Java SE基本語法的東西 建議看看良葛格網站 12/08 19:38







like.gif 您可能會有興趣的文章
icon.png[問題/行為] 貓晚上進房間會不會有憋尿問題
icon.pngRe: [閒聊] 選了錯誤的女孩成為魔法少女 XDDDDDDDDDD
icon.png[正妹] 瑞典 一張
icon.png[心得] EMS高領長版毛衣.墨小樓MC1002
icon.png[分享] 丹龍隔熱紙GE55+33+22
icon.png[問題] 清洗洗衣機
icon.png[尋物] 窗台下的空間
icon.png[閒聊] 双極の女神1 木魔爵
icon.png[售車] 新竹 1997 march 1297cc 白色 四門
icon.png[討論] 能從照片感受到攝影者心情嗎
icon.png[狂賀] 賀賀賀賀 賀!島村卯月!總選舉NO.1
icon.png[難過] 羨慕白皮膚的女生
icon.png閱讀文章
icon.png[黑特]
icon.png[問題] SBK S1安裝於安全帽位置
icon.png[分享] 舊woo100絕版開箱!!
icon.pngRe: [無言] 關於小包衛生紙
icon.png[開箱] E5-2683V3 RX480Strix 快睿C1 簡單測試
icon.png[心得] 蒼の海賊龍 地獄 執行者16PT
icon.png[售車] 1999年Virage iO 1.8EXi
icon.png[心得] 挑戰33 LV10 獅子座pt solo
icon.png[閒聊] 手把手教你不被桶之新手主購教學
icon.png[分享] Civic Type R 量產版官方照無預警流出
icon.png[售車] Golf 4 2.0 銀色 自排
icon.png[出售] Graco提籃汽座(有底座)2000元誠可議
icon.png[問題] 請問補牙材質掉了還能再補嗎?(台中半年內
icon.png[問題] 44th 單曲 生寫竟然都給重複的啊啊!
icon.png[心得] 華南紅卡/icash 核卡
icon.png[問題] 拔牙矯正這樣正常嗎
icon.png[贈送] 老莫高業 初業 102年版
icon.png[情報] 三大行動支付 本季掀戰火
icon.png[寶寶] 博客來Amos水蠟筆5/1特價五折
icon.pngRe: [心得] 新鮮人一些面試分享
icon.png[心得] 蒼の海賊龍 地獄 麒麟25PT
icon.pngRe: [閒聊] (君の名は。雷慎入) 君名二創漫畫翻譯
icon.pngRe: [閒聊] OGN中場影片:失蹤人口局 (英文字幕)
icon.png[問題] 台灣大哥大4G訊號差
icon.png[出售] [全國]全新千尋侘草LED燈, 水草

請輸入看板名稱,例如:Boy-Girl站內搜尋

TOP