415 Unsupported Media Type

本文详细记录了解决Spring MVC框架中使用JSON请求时遇到的问题,包括配置*-servlet.xml文件,设置Content-Type为application/json,并调整curl命令的正确用法。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

蛋疼这个错误搞了一上午,终于解决:


1、*-servlet.xml文件加:

<mvc:annotation-driven /> 


2、请求报文头加:

Content-Type: application/json

用火狐的restful插件调试成功,但是用mac自带的curl工具始终失败郁闷

curl -H "Content-Type: application/json" -X POST  -data '{"usname":"pageqiu"}'  http://127.0.0.1:8080/ffan_train/testJson.do

errinfo:

curl: (6) Could not resolve host: "usname"


先把正确的贴上来:

1、

*-servlet.xml文件

<?xmlversion="1.0"encoding="UTF-8"?>

<beansxmlns="http://www.springframework.org/schema/beans"

xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"

xmlns:p="http://www.springframework.org/schema/p"

xmlns:context="http://www.springframework.org/schema/context"

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">

<mvc:annotation-driven/>  

<context:component-scanbase-package="com.wanda.ffan.travel.train"/>

<beanid="jacksonMessageConverter"class="org.springframework.http.converter.json.MappingJacksonHttpMessageConverter">

<propertyname="supportedMediaTypes">

  <list>

<value>text/plain;charset=UTF-8</value>

<!--  <value>application/json;charset=UTF-8</value>-->

  </list>

</property>

</bean>

<beanclass="org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter"> 

<propertyname="messageConverters"> 

  <list> 

    <refbean="jacksonMessageConverter"/> 

  </list> 

</property> 

</bean>

<beanid="defaultHandlerMapping"class="org.springframework.web.servlet.mvc.annotation.DefaultAnnotationHandlerMapping">

<propertyname="defaultHandler">

<beanclass="org.springframework.web.servlet.mvc.UrlFilenameViewController"></bean>

</property>    

</bean>

<beanid="viewResolver"class="org.springframework.web.servlet.view.InternalResourceViewResolver">

<propertyname="viewClass"value="org.springframework.web.servlet.view.InternalResourceView"/>

<propertyname="prefix"value="/WEB-INF/jsp/"/>

<propertyname="suffix"value=".jsp"/>

</bean>

</beans>



2、java


@RequestMapping(method=RequestMethod.POST,value="/testJson.do",consumes ="application/json")

public @ResponseBody String testJSON(@RequestBody DemoVovo){

 

   

    log.error("***testJson begin****");

    //DemoVo vo = (DemoVo) JSON.parse(body);

    log.error(vo.getUsname());

    log.error("*****testJson end****");

       

        return "success";

    }




用curl请求报错的原因也找到了:

curl -H "Content-Type: application/json" -X POST  --data '{"usname":"pageqiu"}'  http://127.0.0.1:8080/ffan_train/testJson.do

data前是两个-,开发容易吗?
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值