public interface HttpService { void start(); void stop(); String get(String url); void post(String url, String data); } public class HttpServiceImpl implements HttpService { private Server server; public void start() { server = new Server(8080); // ... server.start(); } public void stop() { server.stop(); } public String get(String url) { // ... return response; } public void post(String url, String data) { // ... } } public class Main { public static void main(String[] args) { BundleContext context = FrameworkUtil.getBundle(Main.class).getBundleContext(); ServiceReference<HttpService> serviceRef = context.getServiceReference(HttpService.class); HttpService httpService = context.getService(serviceRef); httpService.start(); String response = httpService.get("http://example.com/api"); System.out.println(response); httpService.stop(); context.ungetService(serviceRef); } }


上一篇:
下一篇:
切换中文