我想做什么: 我正在编写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+"]";
}
}