【发布时间】:2018-05-15 12:58:31
【问题描述】:
我在 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 的两个插件)发送请求时,我收到以下错误:
{ “时间戳”:1512129442019, “状态”:415, "error": "不支持的媒体类型", "message": "不支持内容类型 'text/plain;charset=UTF-8'", “路径”:“/费用/餐厅” }
这个错误表明端点不支持Json内容,但我做到了 放
使用 =MediaType.APPLICATION_JSON_VALUE
@RequestMapping 注解内
我错过了什么?
【问题讨论】:
-
否,错误并未说明端点不支持 JSON。它声明它不支持文本/纯文本。 json 的内容类型是 application/json。在你的 mozilla 插件中指定内容类型为 application/json 应该没问题
-
@YannicKlem 我无法更改客户端如何支持此 Mediatype 服务器端。
-
我知道,但您说错误表明端点不支持 json。那是错的。错误说它确实 nkt 支持 text/plain。因此,您发送客户端的请求没有正确的内容类型标头。
-
请尝试这样请求: curl -X PUT -H 'Content-Type: application/json' -i localhost:8080/spring-rest/api/employees/500 --data '{ "name": "abc", "email ": "abc.a@gmail.com", "薪水": 10000 }'
标签: spring spring-boot utf-8 spring-restcontroller spring-web