[問題]透過post,傳送json格式到spring mvc的controller的問題(已解)

看板java作者 (朦朧尋光)時間7年前 (2017/12/07 17:18), 7年前編輯推噓0(0011)
留言11則, 6人參與, 7年前最新討論串1/1
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 " rel="nofollow">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 " rel="nofollow">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://www.ptt.cc/bbs/java/M.1512638334.A.B30.html

12/07 17:20, 7年前 , 1F
System.out.println(jsonStr);是我插log, 根本沒資料
12/07 17:20, 1F

12/07 17:21, 7年前 , 2F
列印出來. 可能要麻煩大家幫忙一下
12/07 17:21, 2F
※ 編輯: angleevil (175.98.141.254), 12/07/2017 17:22:37

12/07 18:08, 7年前 , 3F
jsonStr.toString() ?
12/07 18:08, 3F

12/07 22:26, 7年前 , 4F
postman的content type有選對嗎?
12/07 22:26, 4F

12/07 22:27, 7年前 , 5F
你用List接object
12/07 22:27, 5F
請問是甚麼意思?? 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); } }

12/07 22:29, 7年前 , 6F
這個參考看看 https://goo.gl/5pbCio
12/07 22:29, 6F
太神奇了...竟然是constructor的問題. 移除後就好了!! ※ 編輯: angleevil (61.218.53.138), 12/08/2017 08:34:41 ※ 編輯: angleevil (61.218.53.138), 12/08/2017 08:49:00

12/08 09:12, 7年前 , 7F
要給auto data binding lib的要有default constructor是滿
12/08 09:12, 7F

12/08 09:12, 7年前 , 8F
基本的吧
12/08 09:12, 8F

12/08 09:16, 7年前 , 9F
這不限於spring(jackson)應該大多數都是這樣
12/08 09:16, 9F

12/08 19:38, 7年前 , 10F
不需要把建構子移除 而是要多給一個無參數建構子 預設建構
12/08 19:38, 10F

12/08 19:38, 7年前 , 11F
子是Java SE基本語法的東西 建議看看良葛格網站
12/08 19:38, 11F
文章代碼(AID): #1QAGT-im (java)
文章代碼(AID): #1QAGT-im (java)