菜鸟求Java大神解决一个SpringMVC的一个问题。

红框里面的方法转化成JSON格式数据是出现异常17:29:57.737 [http-bio-8080-exec-23] DEBUG o.s.w.s.m.a.AnnotationMethodHandlerExceptionResolver - Resolving exception from handler [com.mvc.action.StudentAction@2aa89e44]: org.springframework.web.HttpMediaTypeNotAcceptableException: Could not find acceptable representation17:29:57.737 [http-bio-8080-exec-23] DEBUG o.s.w.s.m.a.ResponseStatusExceptionResolver - Resolving exception from handler [com.mvc.action.StudentAction@2aa89e44]: org.springframework.web.HttpMediaTypeNotAcceptableException: Could not find acceptable representation17:29:57.737 [http-bio-8080-exec-23] DEBUG o.s.w.s.m.s.DefaultHandlerExceptionResolver - Resolving exception from handler [com.mvc.action.StudentAction@2aa89e44]: org.springframework.web.HttpMediaTypeNotAcceptableException: Could not find acceptable representation17:29:57.737 [http-bio-8080-exec-23] DEBUG o.s.web.servlet.DispatcherServlet - Null ModelAndView returned to DispatcherServlet with name 'spring': assuming HandlerAdapter completed request handling17:29:57.737 [http-bio-8080-exec-23] DEBUG o.s.web.servlet.DispatcherServlet - Successfully completed request
在网上也找了但是没有解决。

包也有,xml文件也配置了

<bean id="mappingJacksonHttpMessageConverter"
class="org.springframework.http.converter.json.MappingJacksonHttpMessageConverter">
<property name="supportedMediaTypes">
<list>
<value>text/json;charset=UTF-8</value>
<value>text/html;charset=UTF-8</value>
<value>application/json;charset=UTF-8</value>
</list>
</property>
</bean>
<!-- 启动Spring MVC的注解功能,完成请求和注解POJO的映射 -->
<bean
class="org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter">
<property name="cacheSeconds" value="0" />
<property name="messageConverters">
<list>
<ref bean="mappingJacksonHttpMessageConverter" /><!-- json转换器 -->
</list>
</property>
</bean>

温馨提示:答案为网友推荐,仅供参考
第1个回答  2018-04-17

springMVC提供的异常处理主要有以下几种方式。

几种处理方式为:

1、是直接实现自己的HandlerExceptionResolver。HandlerExceptionResolver是一个接口,springMVC本身已经对其有了一个自身的实现——DefaultExceptionResolver,该解析器只是对其中的一些比较典型的异常进行了拦截处理;

2、是使用注解的方式实现一个专门用于处理异常的Controller——ExceptionHandler。

3、是使用邻接矩阵存放图,邻接矩阵中位于行和列上面点的顺序可以自己定义;

4、是把图中点的名称按照点在邻接矩阵中行或者列上的顺序存放一个数组中;

5、是使用一个数组来标记各个点是否被访问过,数组的大小为图中点的数量;

本回答被网友采纳
第2个回答  2014-06-04
jackson-mapper-asl-xx.xx.jar好像也要的追问

这个包加上也不行

第3个回答  2014-06-04
需要一个json帮助类来将showAll()返回的数据转换成json数据后再return。追问

应该不需要帮助类吧,要不然这个@ResponseBody注解就没有作用了

追答

@RequestMapping(value = "/admin/rolesList", method = RequestMethod.GET)
public @ResponseBody
Object rolesList(HttpServletRequest request, HttpServletResponse response, ModelMap model,
RedirectAttributes redirectAttributes, @RequestParam(required = false) String rolesName,
@RequestParam(required = false) String targetType, @RequestParam(required = false) String identityType) {
List recordList = roleManager.findRoles(rolesName, targetType, identityType);
String jsonString = JsonUtil.beanListToJson(recordList); //使用JsonUtil帮助类转换,可以在网上找到很帮助类
return JsonUtil.ajaxJson(response, jsonString);
}

本回答被提问者采纳
第4个回答  2014-06-04

 提供下思路给你吧,仔细看报错的log

一条一条看,别说全英文看不懂,一开始大家都这样,耐下心来,不一定全部理解,只要明白是啥意思,比方说这个:

org.springframework.web.HttpMediaTypeNotAcceptableException: Could not find acceptable representation

找不到可以匹配(接受)的数据,你仔细看你写的代码吧,哪个地方配置漏掉什么了吧

追问

Could not find acceptable representation就这一处错误,关键是其他地方没问题。。。。

追答

最笨的方法debug,一点点的改,一点点的尝试,这个痛苦的过程就是你成长的过程

本回答被网友采纳
相似回答