看板 java 關於我們 聯絡資訊
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://www.ptt.cc/bbs/java/M.1512638334.A.B30.html
angleevil: System.out.println(jsonStr);是我插log, 根本沒資料 12/07 17:20
angleevil: 列印出來. 可能要麻煩大家幫忙一下 12/07 17:21
※ 編輯: angleevil (175.98.141.254), 12/07/2017 17:22:37
wvwvwvwvwv: jsonStr.toString() ? 12/07 18:08
haha02: postman的content type有選對嗎? 12/07 22:26
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); } }
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
ssccg: 要給auto data binding lib的要有default constructor是滿 12/08 09:12
ssccg: 基本的吧 12/08 09:12
ssccg: 這不限於spring(jackson)應該大多數都是這樣 12/08 09:16
kyleJ: 不需要把建構子移除 而是要多給一個無參數建構子 預設建構 12/08 19:38
kyleJ: 子是Java SE基本語法的東西 建議看看良葛格網站 12/08 19:38