springmvc @responsebody 406/415問題解決


提供幾個解決思路

1.如果項目中用的spring jar包是4.x版本, 需要jackson-annotations-2.x/jackson-core-2.x/jackson-databind-2.x這幾個jar包
spring版本是3.x的需要這兩個jar包jackson-core-asl1.9/jackson-mapper-asl1.9
2.dispatch-servlet.xml配置文件中缺少<mvc:annotation-driven />這段
  如果加上<mvc:之后, 報錯 可能是因為<beans里面沒有引入這段
    xmlns:mvc="http://www.springframework.org/schema/mvc"
    xsi:schemaLocation里面加上這段
    http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-3.2.xsd
3.可能原因是你配置的后綴是.html返回時, 默認把contentType改成"text/html"
    需要修改web.xml配置, 加入*.json配置

    <servlet>
        <servlet-name>dispatch</servlet-name>
        <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
        <load-on-startup>1</load-on-startup>
    </servlet>
    <servlet-mapping>
        <servlet-name>dispatch</servlet-name>
        <url-pattern>*.html</url-pattern>
        <url-pattern>*.json</url-pattern>
    </servlet-mapping>
/**
 * 用戶積分列表
 */
@ResponseBody
@RequestMapping(value = "/list.json",method = {RequestMethod.POST,RequestMethod.GET})
public List<User> userList(@RequestBody PhoneUserQueryParameter p,Model model,HttpServletRequest request,HttpServletResponse response) {
		return phoneUserService.query(p);
}

另外又遇到一個問題, 在用google chrome的DHC插件調試接口時, 后台接收不到POST請求的參數
用GET請求可以接收到參數
在參數前加上@RequestBody, 同時, DHC插件HEADERS中必須設置Content-type為application/json, 后台才能順利接收到參數

 

友情贊助

如果您喜歡此文,感覺對您工作有幫助,預期領導會給您漲工資,不妨小額贊助一下,讓我有動力繼續努力。

贊助方式:打開支付寶App,使用“掃一掃”付款,付款碼見下圖,別忘了付款留言哦!

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM