【发布时间】:2018-05-27 10:20:09
【问题描述】:
我正在使用一个简单的表单,用户可以在其中输入一些 JSON。 我将该输入添加到请求的正文中。 当我从正文中检索值时,它没有被格式化/编码为 JSON。 相反,我得到类似 json=%7B%22vrt%22%3A%7B ... 如何/在哪里指定正文中的值必须是 JSON,以便我的控制器可以使用 GSON 解析它?
提前致谢。 问候
控制器
@PostMapping(value = "/api/sendMessage")
public ModelAndView sendIoTMessage(@RequestBody String json) {
VehicleMessage vehicleMessage = new Gson().fromJson(json, VehicleMessage.class);
MessageProcessor.postVehicleMessage(vehicleMessage);
ModelAndView mav = new ModelAndView();
mav.setViewName("iot");
return mav;
}
表格
<form id="sendMessage" th:action="@{/api/sendMessage}" method="post">
<div class="form-group">
<input class="form-control" th:value="*{json}" id="json" name="json">
</div>
<button type="submit" class="btn btn-primary">Submit</button>
</form>
【问题讨论】:
标签: json spring spring-mvc spring-boot