首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
社区首页 >问答首页 >有没有办法将JSON对象作为get类型Spring REST Service的输入?

有没有办法将JSON对象作为get类型Spring REST Service的输入?
EN

Stack Overflow用户
提问于 2020-01-23 17:32:41
回答 1查看 39关注 0票数 0

我不想将它用作POST方法,因为它是一个列表服务,而JSON对象帮助获取过滤器参数。下面是我用POST编写的代码。有没有办法使用GET (遵循Spring-Boot中的REST标准?

TimeEntriesRequest是我的POJO请求类,TimeEntry是我的bean

代码语言:javascript
代码运行次数:0
运行
AI代码解释
复制
 @RequestMapping(
        method=RequestMethod.POST,
        value="/TimeEntries",
        //consumes = "application/json",
        produces = "application/json"
    )
@ResponseBody
public List<TimeEntry> getTimeEntries(@RequestBody TimeEntriesRequest timeEntriesRequest) throws RestClientException, IllegalArgumentException, IllegalAccessException {

    System.out.println("In controller");
    return timeEntriesService.getAllTimeEntries(timeEntriesRequest);

}
EN

回答 1

Stack Overflow用户

发布于 2020-01-23 18:02:30

Spring Boot不支持带有GET方法的@RequestBody,但您可以尝试使用另一个注释- @RequestParam。

代码语言:javascript
代码运行次数:0
运行
AI代码解释
复制
@RequestMapping(
    method=RequestMethod.GET,
    value="/TimeEntries",
    produces = "application/json"
)
@ResponseBody
public List<TimeEntry> getTimeEntries(
   @RequestParam(value = "personDTO") String timeEntriesRequestDTO) {
   TimeEntriesRequest timeEntriesRequest = new ObjectMapper().readValue(timeEntriesRequestDTO, TimeEntriesRequest.class);
   //       ...
}

您可以遵循一个tutorial来实现它

附注:我不认为这是一个推荐的方法,因为json会在请求url中,所以在使用这个方法之前,你需要检查是否有其他的方法。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/59884230

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档
查看详情【社区公告】 技术创作特训营有奖征文