javax.servlet.ServletException: Could not resolve view with name‘ XXXX’in servlet with name 'spring'的解决方案-----SKY
2016-11-10 13:41
811 查看
出现的异常如下:
javax.servlet.ServletException: Could not resolve view with name '{"msg":"成功","result":"更新成功","resultStatus":1,"success":true}' in servlet with name 'spring'
org.springframework.web.servlet.DispatcherServlet.render(DispatcherServlet.java:1190)
org.springframework.web.servlet.DispatcherServlet.processDispatchResult(DispatcherServlet.java:992)
org.springframework.web.servlet.DispatcherServlet.doDispatch(DispatcherServlet.java:939)
org.springframework.web.servlet.DispatcherServlet.doService(DispatcherServlet.java:856)
org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:953)
org.springframework.web.servlet.FrameworkServlet.doPost(FrameworkServlet.java:855)
javax.servlet.http.HttpServlet.service(HttpServlet.java:650)
org.springframework.web.servlet.FrameworkServlet.service(FrameworkServlet.java:829)
javax.servlet.http.HttpServlet.service(HttpServlet.java:731)
org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:52)
org.springframework.web.filter.CharacterEncodingFilter.doFilterInternal(CharacterEncodingFilter.java:88)
org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:107)
引起该异常的原因:在mvc的配置文件中配置了:<property name="defaultContentType" value="text/html" />,
而url(http://localhost:8080/SpringRestful/services/User/users?_method=delete)请求没有带扩展名,且请求的方法没有@ResponseBody签名,spring会认为你请求的结果是一个html的视图,所以才抛出了上面的异常。
解决方法如下任意一种:
1.url请求加上扩展名:http://localhost:8080/SpringRestful/services/User/users.json?_method=delete
2.请求的方法上加上@ResponseBody签名
3.<property name="defaultContentType" value="text/html" />改为<property name="defaultContentType" value="application/json" />
在这里我选这的是在对应的方法上加上@ResponseBody签名:
下面我将详细介绍该标签的用法。
i) 该注解用于读取Request请求的body部分数据,使用系统默认配置的HttpMessageConverter进行解析,然后把相应的数据绑定到要返回的对象上;
ii) 再把HttpMessageConverter返回的对象数据绑定到 controller中方法的参数上。
使用时机:
A) GET、POST方式提时, 根据request header Content-Type的值来判断:
application/x-www-form-urlencoded, 可选(即非必须,因为这种情况的数据@RequestParam, @ModelAttribute也可以处理,当然@RequestBody也能处理);
multipart/form-data, 不能处理(即使用@RequestBody不能处理这种格式的数据);
其他格式, 必须(其他格式包括application/json, application/xml等。这些格式的数据,必须使用@RequestBody来处理);
B) PUT方式提交时, 根据request header Content-Type的值来判断:
application/x-www-form-urlencoded, 必须;
multipart/form-data, 不能处理;
其他格式, 必须;
说明:request的body部分的数据编码格式由header部分的Content-Type指定;
使用时机: 返回的数据不是html标签的页面,而是其他某种格式的数据时(如json、xml等)使用;
@RequestBody注解时: 根据Request对象header部分的Content-Type类型,逐一匹配合适的HttpMessageConverter来读取数据;
javax.servlet.ServletException: Could not resolve view with name '{"msg":"成功","result":"更新成功","resultStatus":1,"success":true}' in servlet with name 'spring'
org.springframework.web.servlet.DispatcherServlet.render(DispatcherServlet.java:1190)
org.springframework.web.servlet.DispatcherServlet.processDispatchResult(DispatcherServlet.java:992)
org.springframework.web.servlet.DispatcherServlet.doDispatch(DispatcherServlet.java:939)
org.springframework.web.servlet.DispatcherServlet.doService(DispatcherServlet.java:856)
org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:953)
org.springframework.web.servlet.FrameworkServlet.doPost(FrameworkServlet.java:855)
javax.servlet.http.HttpServlet.service(HttpServlet.java:650)
org.springframework.web.servlet.FrameworkServlet.service(FrameworkServlet.java:829)
javax.servlet.http.HttpServlet.service(HttpServlet.java:731)
org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:52)
org.springframework.web.filter.CharacterEncodingFilter.doFilterInternal(CharacterEncodingFilter.java:88)
org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:107)
引起该异常的原因:在mvc的配置文件中配置了:<property name="defaultContentType" value="text/html" />,
而url(http://localhost:8080/SpringRestful/services/User/users?_method=delete)请求没有带扩展名,且请求的方法没有@ResponseBody签名,spring会认为你请求的结果是一个html的视图,所以才抛出了上面的异常。
解决方法如下任意一种:
1.url请求加上扩展名:http://localhost:8080/SpringRestful/services/User/users.json?_method=delete
2.请求的方法上加上@ResponseBody签名
3.<property name="defaultContentType" value="text/html" />改为<property name="defaultContentType" value="application/json" />
在这里我选这的是在对应的方法上加上@ResponseBody签名:
下面我将详细介绍该标签的用法。
i) 该注解用于读取Request请求的body部分数据,使用系统默认配置的HttpMessageConverter进行解析,然后把相应的数据绑定到要返回的对象上;
ii) 再把HttpMessageConverter返回的对象数据绑定到 controller中方法的参数上。
使用时机:
A) GET、POST方式提时, 根据request header Content-Type的值来判断:
application/x-www-form-urlencoded, 可选(即非必须,因为这种情况的数据@RequestParam, @ModelAttribute也可以处理,当然@RequestBody也能处理);
multipart/form-data, 不能处理(即使用@RequestBody不能处理这种格式的数据);
其他格式, 必须(其他格式包括application/json, application/xml等。这些格式的数据,必须使用@RequestBody来处理);
B) PUT方式提交时, 根据request header Content-Type的值来判断:
application/x-www-form-urlencoded, 必须;
multipart/form-data, 不能处理;
其他格式, 必须;
说明:request的body部分的数据编码格式由header部分的Content-Type指定;
@ResponseBody
作用: 该注解用于将Controller的方法返回的对象,通过适当的HttpMessageConverter转换为指定格式后,写入到Response对象的body数据区。使用时机: 返回的数据不是html标签的页面,而是其他某种格式的数据时(如json、xml等)使用;
@RequestBody注解时: 根据Request对象header部分的Content-Type类型,逐一匹配合适的HttpMessageConverter来读取数据;
补充:
MappingJacksonHttpMessageConverter 调用了 objectMapper.writeValue(OutputStream stream, Object)方法,使用@ResponseBody注解返回的对象就传入Object参数内。若返回的对象为已经格式化好的json串时,不使用@RequestBody注解,而应该这样处理:
1、response.setContentType("application/json; charset=UTF-8");
2、response.getWriter().print(jsonStr);
直接输出到body区,然后的视图为void。
相关文章推荐
- javax.servlet.ServletException: Could not resolve view with name 'login' in servlet with name 'XXX'
- javax.servlet.ServletException: Could not resolve view with name 'xxxxx'in servlet with name 'xxx'
- javax.servlet.ServletException: Could not resolve view with name 'destination/isOtherExist' in servl
- javax.servlet.ServletException: Could not resolve view with name 'xxxxx'in servlet with name 'xxx'
- javax.servlet.ServletException: Could not resolve view with name 'xxx' in servlet with name
- javax.servlet.ServletException: Could not resolve view with name 'ok' in servlet with name 'spring'
- javax.servlet.ServletException: Could not resolve view with name 'qq' in servlet with name 'WeiXin'
- 16 Could not resolve view with name 'XXX.ftl' in servlet with name 'SpringMVC'
- Could not resolve view with name '***' in servlet with name 'dispatcher'
- Could not resolve view with name 'pcir/del' in servlet with name 'forum'
- Could not resolve view with name 'ok' in servlet with name 'spring'
- Could not resolve view with name 'pcir/del' in servlet with name 'forum'
- Could not resolve view with name 'sys/menu/user' in servlet with name 'dispatcher'
- Could not resolve view with name 'bok.html' in servlet with name 'spring'
- springMVC+freemarker 中 Could not resolve view with name 'XXX.ftl' in servlet with name 'SpringMVC'
- Spring 4.0.2 + Apache Tiles 3.0.3 [Could not resolve view with name "xxx"]
- Solving “javax.naming.NameNotFoundException: Unable to resolve ‘jdbc.SOAAppUserDataSource’ “ with Oracle SOA Suite 11g Adapter S
- Could not resolve view with name
- WARNING: The host '$hostname' could not be looked up with resolveip.
- Hadoop:could not resolve hostname[xxxx]