Spring MVC Tomcat - 415服务器拒绝请求,因为请求实体的格式不受所请求方法所请求资源的支持

时间:2017-08-10 23:15:42

标签: java spring-mvc tomcat

我想做什么: 我正在编写Java Spring MVC REST API。我正在尝试发送一个JSON,并根据该数据获取图像。

我的问题是什么
当我发送JSON(如下所示)作为POST主体的一部分,其内容类型为application/json时,我得到下面列出的415错误

我尝试了什么

  • @RestController切换为@Controller

  • @RequestBody切换为@ModelAttribute

  • 我正在使用PostMan发送数据。我将标头Content-Type包含到application/json,因此它应该将其作为JSON数据发送。 Fiddler似乎证实了这一点。

错误代码:
The server refused this request because the request entity is in a format not supported by the requested resource for the requested method.

控制器代码:

@RestController
public class Controller {

    @RequestMapping(value = "/GrabImage", method = RequestMethod.POST, consumes = "application/json", produces = MediaType.IMAGE_PNG_VALUE)
    public byte[] GrabImage(@RequestBody Count count){
        //code will go here
    }
}

JSON数据:

{
  "wordCounts": [
    {
      "word": "Sierra",
      "count": 50
    },
    {
      "word": "Love",
      "count": 25
    },
    {
      "word": "Dog",
      "count": 10
    }
  ]
}

统计班级:

public class Count
{
    private WordCounts[] wordCounts;

    public WordCounts[] getWordCounts ()
    {
        return wordCounts;
    }

    public void setWordCounts (WordCounts[] wordCounts)
    {
        this.wordCounts = wordCounts;
    }

    @Override
    public String toString()
    {
        return "ClassPojo [wordCounts = "+wordCounts+"]";
    }
}

WordCount类:

public class WordCounts
{
    private int count;

    private String word;

    public int getCount ()
    {
        return count;
    }

    public void setCount (int count)
    {
        this.count = count;
    }

    public String getWord ()
    {
        return word;
    }

    public void setWord (String word)
    {
        this.word = word;
    }

    @Override
    public String toString()
    {
        return "ClassPojo [count = "+count+", word = "+word+"]";
    }
}

0 个答案:

没有答案