简体   繁体   English

Cloud Foundry Service Broker-实现REST端点

[英]Cloud foundry service broker - implementing REST endpoints

I have a general question on a topic I am starting to learn, but having difficulty imagining the specific implementations for. 对于要开始学习的主题,我有一个一般性问题,但是难以想象其具体实现。

I want to implement a service broker for Cloud Foundry. 我想为Cloud Foundry实施服务代理。 The service broker API is as follows: 服务代理API如下:

http://docs.cloudfoundry.org/services/api.html http://docs.cloudfoundry.org/services/api.html

I'm new to web programming. 我是Web编程的新手。 I have worked with web applications where I publish html files which reference servlets. 我使用过Web应用程序,在其中发布了引用servlet的html文件。 But I'm not sure how one goes about implementing, for example: 但是我不确定如何实施,例如:

Route 路线

GET /v2/catalog GET / v2 /目录

I was wondering if someone could give a high level rundown of what is involved in doing this. 我想知道是否有人可以概括性地介绍这样做的内容。 How do I implement a "path" like this? 我如何实现这样的“路径”? Let's say I wrote a servlet which hangs around at site.com/Servlet. 假设我写了一个servlet,它挂在site.com/Servlet上。 The service broker will call site.com/Servlet/v2/catalog. 服务代理将致电site.com/Servlet/v2/catalog。 How would my Servlet understand this? 我的Servlet如何理解这一点? Would this URI even direct to my Servlet as written? 这个URI是否会像编写的那样直接定向到我的Servlet? I'm using Liberty (Websphere), but any answer would be useful. 我正在使用Liberty(Websphere),但任何答案都将很有用。

I suggest using the Spring framework - website at https://spring.io/ . 我建议使用Spring框架-网站https://spring.io/ It might take a bit of learning to understand what Spring is (it has many components that do different things), but Spring provides tools to make writing REST APIs very easy. 可能需要学习一些知识才能了解Spring是什么(它具有许多功能不同的组件),但是Spring提供了使编写REST API非常容易的工具。 Spring is well documented, has tons of users, and is quite modern. Spring有据可查,有大量用户,并且非常现代。

For a REST API in Spring, you'd define a "Controller" class that controls incoming HTTP calls to the port you program is listening on. 对于Spring中的REST API,您将定义一个“ Controller”类,该类控制对程序正在侦听的端口的传入HTTP调用。

For your concerns about how your programs understands a GET call to a particular endpoint - Spring provides the @RequestMapping annotation to do exactly this task. 对于您的程序如何理解对特定端点的GET调用的担忧-Spring提供了@RequestMapping批注以准确地完成此任务。 Within a class you've annotated with @Controller, you will have the @RequestMapping annotation over a method like this: 在用@Controller注释的类中,您将在类似这样的方法上使用@RequestMapping注释:

@Controller
public class CloudFoundryController {
...

   @RequestMapping(value = {"/servlet/v2/catalog"}, method = RequestMethod.GET
   public HttpResponse getV2Catalogue() {
      ...
   }
}

When this application detects an HTTP GET request with "/servlet/v2/catalog" as the URL endpoint, then Spring will ensure that the getV2Catalogue() method is called. 当此应用程序检测到以“ / servlet / v2 / catalog”作为URL端点的HTTP GET请求时,Spring将确保调用getV2Catalogue()方法。 When the method returns, Spring sends back, over the network, an object of whatever type you've defined in the method header as an http response. 当方法返回时,Spring通过网络将您在方法标头中定义的任何类型的对象作为HTTP响应发送回网络。

Building a REST service with Spring: https://spring.io/guides/gs/rest-service/ 使用Spring构建REST服务: https//spring.io/guides/gs/rest-service/

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM