在一次对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注解即可解决问题