MockMVC出现415错误的解决办法

在一次对Controller层的单测过程中,我遇到了以下错误:

警告: Resolved [org.springframework.web.HttpMediaTypeNotSupportedException: Content type 'application/json;charset=utf-8' not supported]

以下是测试类的返回信息:

MockHttpServletRequest:
      HTTP Method = PUT
      Request URI = /user/2018110124
       Parameters = {}
          Headers = [Content-Type:"application/json;charset=utf-8"]
             Body = {"id":null,"jobNumber":"2018110124","userName":"兰源","passWord":"123456789","telephoneNumber":"18208171899","sex":1,"cardType":"中华人共和国身份证","cardNumber":"510522200006016911","authority":null,"nation":"汉","postType":"行政","status":null,"department":"教务处","politicalStatus":"党员","jobTitle":"教授","email":"18208171899@163.com","lastLogin":null}
    Session Attrs = {}

Handler:
             Type = controller.UserController
           Method = public java.lang.String controller.UserController.modify(java.lang.String,entity.User)

Async:
    Async started = false
     Async result = null

Resolved Exception:
             Type = org.springframework.web.HttpMediaTypeNotSupportedException

ModelAndView:
        View name = null
             View = null
            Model = null

FlashMap:
       Attributes = null

MockHttpServletResponse:
           Status = 415
    Error message = null
          Headers = [Accept:"application/octet-stream, text/plain, application/xml, text/xml, application/x-www-form-urlencoded, application/*+xml, multipart/form-data, */*"]
     Content type = null
             Body = 
    Forwarded URL = null
   Redirected URL = null
          Cookies = []

在这次测试中出现了415错误,但是问题是我已经设置了contentType和accept的媒体类型为application/json,所以我很疑惑怎么解决这个问题,经过查询资料之后发现,我们只需要在Controller层上加上@EnableWebMvc注解即可解决问题

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值