我有一个Spring Boot应用程序中的以下@RestController:
@Data
@RestController
public class Hello {
@Autowired
private ResturantExpensesRepo repo;
@RequestMapping(value = "/expenses/restaurants",method = RequestMethod.POST,consumes =MediaType.APPLICATION_JSON_VALUE ,
headers = MediaType.APPLICATION_JSON_VALUE)
@ResponseBody
public void hello(@RequestBody ResturantExpenseDto dto)
{
Logger logger = LoggerFactory.getLogger("a");
logger.info("got a request");
ResturantExpenseEntity resturantExpenseEntity = new ResturantExpenseEntity();
resturantExpenseEntity.setDate(new Date(System.currentTimeMillis()));
resturantExpenseEntity.setName(dto.getName());
resturantExpenseEntity.setExpense(dto.getExpense());
repo.save(resturantExpenseEntity);
}
}
当我尝试从restClient/RestedClient(都是mozila的插件)发送请求时,我遇到了以下错误:
{ "timestamp": 1512129442019, "status": 415, "error": "Unsupported Media Type", "message": "Content type 'text/plain;charset=UTF-8' not supported", "path": "/expenses/restaurants" }
这个错误表示终点不支持Json内容,但是我确实在 @RequestMapping 注释中加入了
consumes =MediaType.APPLICATION_JSON_VALUE
我错过了什么?