今天在做接口对接的时候,出现了415这个错误,这个坑改了好久,记录一下
我首先用swagger测试了一下数据,是可以成功的
这样可以猜测错误应该出现在前端axios请求过程中了
但是前端也确定发送的请求没问题,又检查了一下,才发现类型没匹配上 ,前端传过来一个string的id,后端用long类型接收了,因此一直报415,
@PostMapping("getInfo")public Map<String, String> getInfo(@RequestBody String id) {System.out.println("==========");System.out.println(id);System.out.println("++++++++++");Student student = studentServiceImpl.findStudent(Long.parseLong(id));System.out.println("getInfo:"+student);Map<String, String> map = new HashMap<>(10);map.put("id", student.getId()+"");map.put("name", student.getName());map.put("gender", student.getGender());map.put("nation", student.getNation());map.put("department", "");map.put("idCardNumber", student.getMajor());map.put("phoneNumber", student.getPhoneNumber());map.put("password", student.getPassword());return map;}
这个错误改掉之后,又出现了新的错误,我后台接收的id末尾莫名其妙的多了一个“=”号,这又是什么原因呢?
排查之后,前端发送请求加了一个请求头,问题成功解决。