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

作者: angleevil (朦朧尋光)   2017-12-07 17:18:50
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>
<!
作者: angleevil (朦朧尋光)   2017-12-07 17:20:00
System.out.println(jsonStr);是我插log, 根本沒資料列印出來. 可能要麻煩大家幫忙一下
作者: wvwvwvwvwv (殺死丁力這個雜碎a~)   2017-12-07 18:08:00
jsonStr.toString() ?
作者: haha02 (來人!上夾棍!)   2017-12-07 22:26:00
postman的content type有選對嗎?
作者: Expsun (無限燃燒)   2017-12-07 22:27:00
你用List接object
作者: haha02 (來人!上夾棍!)   2017-12-07 22:29:00
這個參考看看 https://goo.gl/5pbCio
作者: ssccg (23)   2017-12-08 09:12:00
要給auto data binding lib的要有default constructor是滿基本的吧這不限於spring(jackson)應該大多數都是這樣
作者: kyleJ (資工人)   2017-12-08 19:38:00
不需要把建構子移除 而是要多給一個無參數建構子 預設建構子是Java SE基本語法的東西 建議看看良葛格網站

Links booklink

Contact Us: admin [ a t ] ucptt.com