简体   繁体   English

Spring Controller无法解析JSON

[英]Spring Controller not parsing JSON

Ok, i' m trying to receive a JSON object (this one {"name":"sad","note":"//"}) but nothing displayed from the STS console, the browser(Chrome) gives " POST http://localhost:8080/sequenziatore/proc 400 (Bad Request) " , the System.out.println never occur without any error given..If i change the type of the RequestBody with @RequestBody String proc, it receive the String correctly as JSON..So it is not able to parser the JSON..I checked this post JQuery, Spring MVC @RequestBody and JSON - making it work together but nothing...The code im using is this: 好的,我正在尝试接收一个JSON对象(这个{“name”:“sad”,“note”:“//”})但是没有从STS控制台显示,浏览器(Chrome)给出了“ POST http://localhost:8080/sequenziatore/proc 400 (Bad Request) “,System.out.println永远不会发生任何错误给定..如果我用@RequestBody String proc更改RequestBody的类型,它会正确接收String作为JSON ..因为它无法解析JSON ..我检查了这篇文章JQuery,Spring MVC @RequestBody和JSON - 使它一起工作但没有...我使用的代码是这样的:

@Controller
@RequestMapping(value = "/proc")
public class ProcessController {
@RequestMapping(method=RequestMethod.POST , consumes = "application/json", produces = "application/json")
    @ResponseBody
    public void CheckLogin(@RequestBody Processo proc) {
            System.out.println(proc.name);
    }
}

With this other file... web.xml: 使用这个其他文件... web.xml:

<?xml version="1.0" encoding="ISO-8859-1"?>
<!DOCTYPE web-app PUBLIC "-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN" "http://java.sun.com/dtd/web-app_2_3.dtd">
 <web-app>
   <display-name>Archetype Created Web Application</display-name>
   <servlet>
        <servlet-name>spring</servlet-name>
        <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
        <load-on-startup>1</load-on-startup>
    </servlet>
 <servlet-mapping>
   <servlet-name>spring</servlet-name>
   <url-pattern>/</url-pattern>
 </servlet-mapping>
 <welcome-file-list>
        <welcome-file>index.jsp</welcome-file>
    </welcome-file-list>
</web-app>

spring-servlet.xml: 为spring-servlet.xml:

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
    xmlns:mvc="http://www.springframework.org/schema/mvc"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xmlns:context="http://www.springframework.org/schema/context"
    xsi:schemaLocation="
        http://www.springframework.org/schema/beans
        http://www.springframework.org/schema/beans/spring-beans.xsd
        http://www.springframework.org/schema/mvc
        http://www.springframework.org/schema/mvc/spring-mvc.xsd
        http://www.springframework.org/schema/context
        http://www.springframework.org/schema/context/spring-context-3.0.xsd">
<context:component-scan base-package="com.presenter"/>
<mvc:resources mapping="/resources/**" location="/resources/" />
<mvc:annotation-driven />
</beans>

in the pom.xml i have this dependency: 在pom.xml中我有这种依赖:

<dependency>
            <artifactId>jackson-mapper-asl</artifactId>
            <groupId>org.codehaus.jackson</groupId>
            <version>1.9.9</version>
        </dependency>

EDIT added error given from log4j: 编辑添加了log4j给出的错误:

Error resolving argument [0] [type=com.resources.Processo]
HandlerMethod details: 
Controller [com.presenter.ProcessController]
Method [public void com.presenter.ProcessController.CheckLogin(com.resources.Processo)]

org.springframework.http.converter.HttpMessageNotReadableException: Could not read JSON: No suitable constructor found for type [simple type, class com.resources.Processo]: can not instantiate from JSON object (need to add/enable type information?)
 at [Source: org.apache.catalina.connector.CoyoteInputStream@281c2495; line: 1, column: 2]; nested exception is org.codehaus.jackson.map.JsonMappingException: No suitable constructor found for type [simple type, class com.resources.Processo]: can not instantiate from JSON object (need to add/enable type information?)
 at [Source: org.apache.catalina.connector.CoyoteInputStream@281c2495; line: 1, column: 2]
    at org.springframework.http.converter.json.MappingJacksonHttpMessageConverter.readJavaType(MappingJacksonHttpMessageConverter.java:187)
    at org.springframework.http.converter.json.MappingJacksonHttpMessageConverter.read(MappingJacksonHttpMessageConverter.java:179)
    at org.springframework.web.servlet.mvc.method.annotation.AbstractMessageConverterMethodArgumentResolver.readWithMessageConverters(AbstractMessageConverterMethodArgumentResolver.java:138)
    at org.springframework.web.servlet.mvc.method.annotation.RequestResponseBodyMethodProcessor.readWithMessageConverters(RequestResponseBodyMethodProcessor.java:183)
    at org.springframework.web.servlet.mvc.method.annotation.RequestResponseBodyMethodProcessor.resolveArgument(RequestResponseBodyMethodProcessor.java:98)
    at org.springframework.web.method.support.HandlerMethodArgumentResolverComposite.resolveArgument(HandlerMethodArgumentResolverComposite.java:79)
    at org.springframework.web.method.support.InvocableHandlerMethod.getMethodArgumentValues(InvocableHandlerMethod.java:157)
    at org.springframework.web.method.support.InvocableHandlerMethod.invokeForRequest(InvocableHandlerMethod.java:124)
    at org.springframework.web.servlet.mvc.method.annotation.ServletInvocableHandlerMethod.invokeAndHandle(ServletInvocableHandlerMethod.java:104)
    at org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter.invokeHandleMethod(RequestMappingHandlerAdapter.java:749)
    at org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter.handleInternal(RequestMappingHandlerAdapter.java:689)
    at org.springframework.web.servlet.mvc.method.AbstractHandlerMethodAdapter.handle(AbstractHandlerMethodAdapter.java:83)
    at org.springframework.web.servlet.DispatcherServlet.doDispatch(DispatcherServlet.java:938)
    at org.springframework.web.servlet.DispatcherServlet.doService(DispatcherServlet.java:870)
    at org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:961)
    at org.springframework.web.servlet.FrameworkServlet.doPost(FrameworkServlet.java:863)
    at javax.servlet.http.HttpServlet.service(HttpServlet.java:647)
    at org.springframework.web.servlet.FrameworkServlet.service(FrameworkServlet.java:837)
    at javax.servlet.http.HttpServlet.service(HttpServlet.java:728)
    at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:305)
    at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:210)
    at org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:51)
    at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:243)
    at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:210)
    at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:222)
    at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:123)
    at org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:502)
    at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:171)
    at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:100)
    at org.apache.catalina.valves.AccessLogValve.invoke(AccessLogValve.java:953)
    at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:118)
    at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:408)
    at org.apache.coyote.http11.AbstractHttp11Processor.process(AbstractHttp11Processor.java:1041)
    at org.apache.coyote.AbstractProtocol$AbstractConnectionHandler.process(AbstractProtocol.java:603)
    at org.apache.tomcat.util.net.JIoEndpoint$SocketProcessor.run(JIoEndpoint.java:312)
    at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1145)
    at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:615)
    at java.lang.Thread.run(Thread.java:745)
Caused by: org.codehaus.jackson.map.JsonMappingException: No suitable constructor found for type [simple type, class com.resources.Processo]: can not instantiate from JSON object (need to add/enable type information?)
 at [Source: org.apache.catalina.connector.CoyoteInputStream@281c2495; line: 1, column: 2]
    at org.codehaus.jackson.map.JsonMappingException.from(JsonMappingException.java:163)
    at org.codehaus.jackson.map.deser.BeanDeserializer.deserializeFromObjectUsingNonDefault(BeanDeserializer.java:746)
    at org.codehaus.jackson.map.deser.BeanDeserializer.deserializeFromObject(BeanDeserializer.java:683)
    at org.codehaus.jackson.map.deser.BeanDeserializer.deserialize(BeanDeserializer.java:580)
    at org.codehaus.jackson.map.ObjectMapper._readMapAndClose(ObjectMapper.java:2732)
    at org.codehaus.jackson.map.ObjectMapper.readValue(ObjectMapper.java:1923)
    at org.springframework.http.converter.json.MappingJacksonHttpMessageConverter.readJavaType(MappingJacksonHttpMessageConverter.java:184)
    ... 37 more 

确保要转换的对象具有可访问的空构造函数。

public Processo() {}

You have to give a valid json in order for Spring to parse it. 你必须给一个有效的json,以便Spring解析它。

For example: 例如:

curl -H "Content-Type: application/json" -d '{"username":"xyz"}' http://localhost:8080/sequenziatore/proc

If you give an empty string as json, you'll get a bad request... 如果你给一个空字符串作为json,你会收到一个错误的请求......

Also, the json you provide should map to the Processo class 此外,您提供的json应映射到Processo类

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM