HTTP Status 415 – Unsupported Media Type解决方案之一

在json数据传输的时候,往后端传输json的时候爆了错误如下:

HTTP Status 415 – Unsupported Media Type

HTTP Status 415 – Unsupported Media Type


Type Status Report

Description The origin server is refusing to service the request because the payload is in a format not supported by this method on the target resource.


Apache Tomcat/8.5.23

主要错误:HTTP Status 415 – Unsupported Media Type
网上搜索415错误,大都是追加
httpURLConnection.setRequestProperty(“content-type”, “application/json;charset=UTF-8”);

,未有什么作用。

controller层代码:

   @RequestMapping(value = "/testAcceptJson", method = RequestMethod.POST)
    @ResponseBody
    public String getTest(HttpServletRequest request, @RequestBody Map<String,Object> frontData){
        System.out.println(frontData);
        return  "asd";
    }

用Map接受json格式,会报错,换成String格式则不会。@RequestBody 这个注解多用于json格式的交互,但是默认可以用bean和String来接受json。不支持Map接收,但在大多说开发的环境,希望用Map来接受

解决方案

追加json全家桶jar包,之前我只引了一个基础包。所以绑定报错。

<dependency>
      <groupId>org.codehaus.jacksongroupId>
      <artifactId>jackson-mapper-aslartifactId>
      <version>1.9.13version>
    dependency>

    <dependency>
      <groupId>com.alibabagroupId>
      <artifactId>fastjsonartifactId>
      <version>1.2.3version>
    dependency>

   <dependency>
      <groupId>com.fasterxml.jackson.coregroupId>
      <artifactId>jackson-annotationsartifactId>
      <version>${jackson.version}version>
    dependency>
        <dependency>
          <groupId>com.fasterxml.jackson.coregroupId>
          <artifactId>jackson-coreartifactId>
          <version>${jackson.version}version>
        dependency>
        <dependency>
          <groupId>com.fasterxml.jackson.coregroupId>
          <artifactId>jackson-databindartifactId>
          <version>${jackson.version}version>
        dependency>

大功告成~~

你可能感兴趣的:(技术)