springboot接口返回数据类型解析问题

问题:今天在使用postman调试springboot项目的接口的时候一直报错提示:

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

但是我在controller里已经写了@ResponseBody标签,controller里使用的是@Controller标签

结果在使用postman测试的时候一直是{ status:500; msg:"could not find acceptable representation"};

在网上看了很多种方法有的是在pom.xml里添加其他json包的依赖等等。。。无果,,,,

解决:

最终success解决了;

在全局拦截器里继承WebMvcConfigurerAdapter然后重写configureContentNegotiation方法,在方法里重写下:

super.configureContentNegotiation(configurer);
configurer.defaultContentType(MediaType.APPLICATION_JSON_UTF8);
configurer.favorPathExtension(false);

这里千万注意一定要先调用super父类的方法,然后再写默认的返回数据格式(今天就是因为在这里顺序后调用父类的方法,导致一直是失败的)!!!

扫描二维码关注公众号,回复: 4450971 查看本文章

同时你也可以在启动类里继承WebMvcConfigurerAdapter去重写;

问题解决,希望这次踩过的坑可以帮助大家!

猜你喜欢

转载自blog.csdn.net/qq_37461349/article/details/83625202