2014-03-31 31 views
0

我的应用程序存在一个问题:我将给出一个嵌套参数给后台,它显示参数放在HTTP请求的TextView部分。我想用@RequestBody来获取参数,但是一旦我在参数前面键入@RequestBody注释,我将得到一个错误〜。使用嵌套参数的Ajax请求,在函数中使用@requestBody得到415错误

JS

$.ajax({ 
    url:"maintenance/clientSystem/updatePriceHierarchy.html", 
    data: {"post":"515", "person":{"personId":"162"}}, 
    dataType:"json", 
    type:"POST", 
    contentType: "application/json" 
}).done(function(data){ 
    console.log("finish"); 
}); 

控制器

@RequestMapping("client/updatePerson") 
    public final void updatePerson(HttpServletResponse response, Person bean) throws Exception { 
    System.out.println(bean.getPersonId()); 
    } 

弹簧MVC配置

<!-- for local resources --> 
<mvc:resources mapping="/css/**" location="/css/"/> 
<mvc:resources mapping="/js/**" location="/js/"/> 
<mvc:resources mapping="/images/**" location="/images/"/> 
<mvc:resources mapping="/images/deskTopIcon/**" location="/images/deskTopIcon/"/> 
<mvc:resources mapping="/images/deskTopImg/**" location="/images/deskTopImg/"/> 
<!-- scan package --> 
<context:component-scan base-package="com.jesse.controller" /> 

<!-- add annotation driver --> 
<mvc:annotation-driven /> 
<!-- define prefix and suffix for view --> 
<bean id="viewResolver" 
    class="org.springframework.web.servlet.view.InternalResourceViewResolver"> 
    <property name="viewClass" value="org.springframework.web.servlet.view.JstlView"></property> 
    <property name="prefix" value="/pages/" /> <property name="suffix" 
    value=".jsp" /> 
</bean> 

<bean class ="org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter" > 
    <property name= "messageConverters" > 
     <list> 
       <ref bean= "stringHttpMessageConverter" /> 
       <ref bean="jacksonMessageConverter" /> 
       <ref bean="jsonHttpMessageConverter" /> 
      </list> 
    </property> 
</bean> 

<bean id="jacksonMessageConverter" class="org.springframework.http.converter.json.MappingJacksonHttpMessageConverter" /> 
<bean id = "stringHttpMessageConverter" class = "org.springframework.http.converter.StringHttpMessageConverter" /> 

<bean id="multipartResolver" class="org.springframework.web.multipart.commons.CommonsMultipartResolver"> 
    <property name="maxUploadSize" value="1000000"/> 
</bean> 

<bean id="jsonHttpMessageConverter" class="org.springframework.http.converter.json.MappingJacksonHttpMessageConverter"> 
    <property name="supportedMediaTypes"> 
     <list> 
      <value>application/json;charset=UTF-8</value> 
     </list> 
    </property> 
</bean> 

任何人都可以帮助我吗?

+0

您已经添加了'MappingJacksonHttpMessageConverter'两次,你没有因为消息来指定'supportedMediaTypes'转换器会照顾它。简而言之,从上下文中移除'jsonHttpMessageConverter' bean。您也不必显式声明'AnnotationMethodHandlerAdapter'。它将被默认注册。如果你在类路径上有Jackson,它会自动为它注册一个转换器。 – Bart

+0

@Bart好的,我会修改我的spring-servlet.xml。但我不认为这是关键。 – Jesse

回答

1

有一些错误:

  • 控制器updatePerson()映射网址是client/updatePerson和你做AJAX请求maintenance/clientSystem/updatePriceHierarchy.html

  • 您的AJAX请求的类型为POST和你没有提什么类型的方法在控制器中。

  • 您提到的问题使用@RequestBody,但我无法在您的控制器方法中看到。

正确所有这些,然后来:

在功能上得到一个415错误与@RequestBody

HTTP 415错误意味着不支持的媒体类型:服务器拒绝为请求提供服务,因为请求的实体的格式不是所请求方法的请求资源所支持的格式。

如何摆脱415错误是: 指定正确的Content-TypeAccept请求头。像:

$.ajax({ 
    type: "POST", 
    url: "client/updatePerson", 
    data: JSON.stringify(jsonStr), 
    async: false, 
    cache: false, 
    processData:false, 
    beforeSend: function(xhr) { 
     xhr.setRequestHeader("Accept", "application/json"); //Accept request header specified 
     xhr.setRequestHeader("Content-Type", "application/json"); //Content-Type request header specified 
    }, 
    success: function(response){ 
     alert('Success: '+response.name); 
    }, 
    error: function(jqXHR, textStatus, errorThrown) { 
     alert(textStatus+' : '+ errorThrown); 
    } 
}); 

注:jsonStr什么都在AJAX数据说明,该字符串应该是一个类你在控制器方法有JSON表示格式,那么只有春天将其转换回。

例如,您Person类将是这样的:

class Person { 
    private Long pid; 
    private String name; 
    private Person person; 

    public Person(){} //Default constructor is needed 

    //getters and setters 
} 

然后,jsonStr看起来像:

var jsonStr = {"pid": 515, "name": "Jeese"}; 

嵌套人:

var jsonStr = {"pid": 515, "name": "Jeese", "person" : {"pid": 516, "name": "Jeese sub"}}; 

然后,控制器,方法将如下所示:

@Controller 
@RequestMapping("/client/updatePerson") 
public class ClientController { 

    private final Logger logger = LoggerFactory.getLogger(ClientController.class); 

    @RequestMapping(method = RequestMethod.POST, 
      produces={MediaType.APPLICATION_JSON_VALUE}, 
      consumes={MediaType.APPLICATION_JSON_VALUE}) 
     public @ResponseBody Person updatePerson(@RequestBody Person bean) throws Exception { 
     logger.debug("updatePerson() invoked.."); 
      //do your works here with person.. 
      logger.debug(bean.toString()); 
      logger.debug("Sub person: "+bean.getPerson().toString()); 
      return bean; 
     } 
} 

jackson-mapper-asl jar应该在CLASSPATH中可用。


参见:

HTTP Error codes

Spring 3.1.X RequestMapping new features where no need to register RequestMappingHandlerAdapter

RequestMapping

+0

解决方法:感谢您的回答。 URL问题是由我公司的默认隐藏一些细节信息引起的,我找到了关键点,那就是你不能提交目标表单bean没有的参数,比如** JS **:'' {person {name:“jesse”,value:“21”},additionalParameter:0}。如果你的formBean没有附加参数,你会得到一个415错误。 和你的答案真的帮助我很多,再次感谢。 – Jesse

相关问题