我正在使用JSON对象使用Spring MVC.当我想从RESTClient发送JSON对象时,我得到了
HTTP状态400 - 客户端发送的请求语法错误().
这是我的控制器
ObjectMapper mapper=new ObjectMapper();
@RequestMapping(value = "/addTask", method = RequestMethod.GET)
public ModelAndView addTask(@RequestParam("json") String json) throws JsonParseException, JsonMappingException, IOException
{
System.out.println("Json object from REST : "+json);
Task task=(Task) mapper.readValue(json, Task);
service.addService(task);
return new ModelAndView("Result");
}
Run Code Online (Sandbox Code Playgroud)
我的请求网址: http://localhost:8080/Prime/addTask
我的Json对象:
{"taskName":"nothing","taskId":1234,"taskDesc":"什么都没做"}
我也尝试在RESTClient中指定"Content-Type:application/json"但仍然得到相同的错误
我收到了这个错误
java.lang.IllegalStateException: Neither BindingResult nor plain target object for bean name 'command' available as request attribute
Run Code Online (Sandbox Code Playgroud)
这是堆栈跟踪
org.apache.jasper.JasperException:java.lang.IllegalStateException:BindingResult和bean名称'command'的普通目标对象都不能用作请求属性org.apache.jasper.servlet.JspServletWrapper.handleJspException(JspServletWrapper.java:534)org. apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:452)org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:389)org.apache.jasper.servlet.JspServlet.service(JspServlet.java: 333)javax.servlet.http.HttpServlet.service(HttpServlet.java:722)根本原因java.lang.IllegalStateException:无法将BindingResult和bean名称'command'的普通目标对象用作请求属性org.springframework.web.servlet. support.BindStatus.(BindStatus.java:141)org.springframework.web.servlet.tags.form.AbstractDataBoundFormElementTag.getBindStatus(AbstractDataBoundFormElementTag.java:178)org.springframework.web.servlet.tags.form.AbstractDataBoundFormElementTag.getPropertyPath(AbstractDataBoundFormE lementTag.java:198)org.springframework.web.servlet.tags.form.LabelTag.autogenerateFor(LabelTag.java:129)org.springframework.web.servlet.tags.form.LabelTag.resolveFor(LabelTag.java:119) org.springframework.web.servlet.tags.form.LabelTag.writeTagContent(LabelTag.java:89)org.springframework.web.servlet.tags.form.AbstractFormTag.doStartTagInternal(AbstractFormTag.java:102)org.springframework.web. servlet.tags.RequestContextAwareTag.doStartTag(RequestContextAwareTag.java:79)org.apache.jsp.student_jsp._jspx_meth_form_005flabel_005f0(student_jsp.java:182)org.apache.jsp.student_jsp._jspx_meth_form_005fform_005f0(student_jsp.java:117)org.apache. jsp.student_jsp._jspService(student_jsp.java:79)org.apache.jasper.runtime.HttpJspBase.service(HttpJspBase.java:70)javax.servlet.http.HttpServlet.service(HttpServlet.java:722)org.apache. jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:419)org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:389)org.apache.jasper.se rvlet.JspServlet.service(JspServlet.java:333)javax.servlet.http.HttpServlet.service(HttpServlet.java:722)
这是我的web.xml文件
<?xml version="1.0" encoding="UTF-8"?>
<web-app id="WebApp_ID" version="2.4"
xmlns="http://java.sun.com/xml/ns/j2ee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee
http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd">
<display-name>Spring MVC Form Handling</display-name>
<welcome-file-list>
<welcome-file>student.jsp</welcome-file>
</welcome-file-list>
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<servlet>
<servlet-name>HelloWeb</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>/WEB-INF/HelloWeb-Servlet.xml</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>HelloWeb</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>
</web-app>
Run Code Online (Sandbox Code Playgroud)
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:mvc="http://www.springframework.org/schema/mvc"
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.Prime" /> …
Run Code Online (Sandbox Code Playgroud)