【问题标题】:Expose a Springboot rest endpoint to read json object公开 Spring Boot 休息端点以读取 json 对象
【发布时间】:2016-05-01 16:53:18
【问题描述】:

我是 Springboot 和 Webservices 的新手。使用 springboot 我需要公开一个休息端点。一些数据提供者会调用 rest 端点来发布数据。然后我需要开始处理发布的 json 并将其转换为 java 对象。所以使用spring boot我需要公开一个rest webservice来接受和处理发布到创建的webservice的json。我该怎么做。任何例子都会有所帮助

【问题讨论】:

  • 好吧,您当然必须构建一个 RESTful Web 服务。您是否需要消费者取决于“应由某人调用其余端点”中的“某人”是否也将成为 Spring 项目的一部分。如果是这样,那么“某人”就是你的消费者。

标签: json web-services rest spring-boot jackson


【解决方案1】:

就这么简单。 我们需要有一个 Springboot restcontroller 来暴露 rest 端点。然后使用@Requestbody,我们需要直接从传递的json消息中获取对象。

@RestController
public class JsonObjectRestController  {

@RequestMapping(value="/rest/pushjson",method = RequestMethod.POST)
public ResponseEntity<PushedJsonObject> getJsonObject(@RequestBody PushedJsonObject jsonObject)
{
    if(jsonObject != null)
    {
     //process the json object              
    }
    return new ResponseEntity<PushedJsonObject >(jsonObject, HttpStatus.OK);
}

}

说明: 我们需要一个模型类来表示发送的 json(这里是 PushedJsonObject)。让你的控制器使用新的 Spring4 @RestController 进行注释。 @RequestBody 方法参数注解应该使用 HttpMessageConverter 将 HTTP 请求正文中的 json 值绑定到 java 对象。确保 Jackson 在类路径中,以便 Spring Boot 自动将其配置为使用 MappingJackson2MessageConverter。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-03-01
    • 2015-01-31
    • 2016-05-06
    • 2020-08-29
    • 2019-05-07
    • 1970-01-01
    相关资源
    最近更新 更多