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/cn.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灯, 水草

请输入看板名称,例如:BabyMother站内搜寻

TOP