org.springframework.http.converter.HttpMessageNotReadableException: Could not read JSON: No content to map to Object due to end of input; 异常处理后的结果示例如下:
对于在SprngMVC 控制器层使用 @RequestBody 注解作为参数使用 MockHttpServletRequest 进行测试示例:
//运行类
@RunWith(SpringJUnit4ClassRunner.class)
//bean的配置文件路径
@ContextConfiguration(locations = {"/com/wangmeng/dpap/module/springmvctest/spring.xml", "/com/wangmeng/dpap/module/springmvctest/springmvc-servlet.xml"})
public class EmpControllerTest {
//注入 controll 控制器
@Autowired
private EmpController empController;
@Autowired
private RequestMappingHandlerAdapter handlerAdapter;
//定义request 对象
MockHttpServletRequest request = new MockHttpServletRequest();
//定义 response 对象
MockHttpServletResponse response = new MockHttpServletResponse();
@Test
public void insertEmpTest(){
String emp="{\"name\":\"emptest\",\"age\":10}";
//请求路径
request.setRequestURI("springmvctest/empAdd");
//请求方式
request.setMethod(HttpMethod.POST.name());
//请求方式为JSON (如果在Control 层中参数接收方式@RequestBody 必须要设置请求方式为 JSON)
request.setContentType("application/json;charset=UTF-8");
//设置请求数据 对于注解使用的 @RequestBody 使用setContent() 传递参数,对于使用的注解是@RequestParam就可以使用addParameter() 传递参数
request.setContent(emp.getBytes()); //在springMVC 通过 MappingJackson2HttpMessageConverter 将json 转化为 JavaBean 对象设置的请求信息,如果不使用该方法就会 抛出 org.springframework.http.converter.HttpMessageNotReadableException: Could not read JSON: No content to map to Object due to end of input;
try {
handlerAdapter.handle(request, response, new HandlerMethod(empController,"insertEmp",EmpVo.class));
//获取@ResponseBody 的返回信息
String bodyStr = response.getContentAsString();
Assert.assertNotNull(bodyStr);
} catch (NoSuchMethodException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
//@Test
public void deleteEmpTest(){
request.addParameter("id","1000");
request.setRequestURI("springmvctest/deleteEmp");
request.setMethod(HttpMethod.POST.name());
request.setContentType("application/json;charset=UTF-8");
// 在控制层如果返回的是ModelAndView 即可使用该对象
ModelAndView view = new ModelAndView();
try {
view = handlerAdapter.handle(request, response, new HandlerMethod(empController,"deleteEmp",String.class));
Assert.assertEquals("springmvctest/index", view.getViewName());
} catch (NoSuchMethodException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
Assert.assertNull(view);
}
// @Test
public void empListTest(){
request.addParameter("seachKey","");
request.addParameter("start","0");
request.addParameter("limit","20");
request.setRequestURI("springmvctest/empList");
request.setMethod(HttpMethod.POST.name());
request.setContentType("application/json;charset=UTF-8");
ModelAndView view = new ModelAndView();
try {
view = handlerAdapter.handle(request, response, new HandlerMethod(empController,"empList",String.class,int.class,int.class));
Assert.assertNotNull(view);
} catch (NoSuchMethodException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}