报这个错有很多种原因,我这只是其中的一种,可能您报的跟我的不一样,当然本篇文章如果能帮到您,那再好不过了。
问题描述
Resolved [org.springframework.web.HttpMediaTypeNotAcceptableException: Could not find acceptable representation]
控制台如下:
406:HTTP协议状态码的一种(4xx表示客户端的问题),表示客户端无法解析服务端返回的内容。说白了就是后台的返回结果前台无法解析就报406错误。
接口代码如下:
import com.gzl.cn.demo.entity.Params;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
@RestController
public class RequestParamController {
@GetMapping("/requestParm1")
public void requestParm1(@RequestParam Integer id) {
System.out.println("get带@RequestParam:" + id);
}
@GetMapping("/requestParm7")
public Params requestParm7(Params params) {
System.out.println(params);
return params;
}
}
一旦代码添加上这个属性绑定之后就会报错: 以下是mvc当中接受参数的时候,会进行访问如下,属于自定义的参数解析器。
import cn.hutool.core.date.DateUtil;
import org.springframework.beans.propertyeditors.StringTrimmerEditor;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.bind.WebDataBinder;
import org.springframework.web.bind.support.WebBindingInitializer;
import org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter;
import java.beans.PropertyEditorSupport;
import java.util.Date;
@Configuration
public class Config {
@Bean
public RequestMappingHandlerAdapter webBindingInitializer() {
RequestMappingHandlerAdapter adapter = new RequestMappingHandlerAdapter();
adapter.setWebBindingInitializer(new WebBindingInitializer() {
@Override
public void initBinder(WebDataBinder binder) {
binder.registerCustomEditor(Date.class, new PropertyEditorSupport() {
@Override
public void setAsText(String text) {
// DateUtil.parse是hutool当中的方法
setValue(DateUtil.parse(text));
}
});
// 如果是字符串类型,就去除字符串的前后空格
binder.registerCustomEditor(String.class,
new StringTrimmerEditor(true));
}
});
return adapter;
}
}
解决过程
首先去掉参数解析之后就正常了,那问题应该就是出现在解析绑定这块。然后我进行打断点调试,发现这块打断点,然后访问接口照样是会进来的。说明这块代码并没有失效,那为什么406呢?
调整为如下即可解决,原因是RequestMappingHandlerAdapter
本身就在容器当中,我们属于是给他添加属性,所以一定要从容器里面来取,然后再放回容器当中。
import cn.hutool.core.date.DateUtil;
import org.springframework.beans.propertyeditors.StringTrimmerEditor;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.bind.WebDataBinder;
import org.springframework.web.bind.support.WebBindingInitializer;
import org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter;
import java.beans.PropertyEditorSupport;
import java.util.Date;
@Configuration
public class Config {
@Bean
public RequestMappingHandlerAdapter webBindingInitializer(RequestMappingHandlerAdapter requestMappingHandlerAdapter) {
requestMappingHandlerAdapter.setWebBindingInitializer(new WebBindingInitializer() {
@Override
public void initBinder(WebDataBinder binder) {
binder.registerCustomEditor(Date.class, new PropertyEditorSupport() {
@Override
public void setAsText(String text) {
// DateUtil.parse是hutool当中的方法
setValue(DateUtil.parse(text));
}
});
// 如果是字符串类型,就去除字符串的前后空格
binder.registerCustomEditor(String.class,
new StringTrimmerEditor(true));
}
});
return requestMappingHandlerAdapter;
}
}