<dependencies> <!-- Amdatu Remote RSA --> <dependency> <groupId>org.amdatu.remote</groupId> <artifactId>org.amdatu.remote.admin.http</artifactId> <version>1.0.0</version> </dependency> </dependencies> <rsadmin:config name="http-rsa"> <connection endpoint="http://localhost:8080/rsadmin"/> </rsadmin:config> public interface Calculator { int add(int a, int b); } public class CalculatorImpl implements Calculator { public int add(int a, int b) { return a + b; } } import org.osgi.framework.BundleActivator; import org.osgi.framework.BundleContext; import org.osgi.framework.ServiceRegistration; import org.amdatu.remote.admin.http.HttpAdminConstants; public class Activator implements BundleActivator { private ServiceRegistration registration; public void start(BundleContext context) { Dictionary<String, Object> props = new Hashtable<>(); props.put(HttpAdminConstants.ENDPOINT_ID, "http-rsa"); Calculator calculator = new CalculatorImpl(); registration = context.registerService(Calculator.class.getName(), calculator, props); } public void stop(BundleContext context) { registration.unregister(); } } import org.apache.http.client.HttpClient; import org.apache.http.client.methods.HttpPost; import org.apache.http.entity.StringEntity; import org.apache.http.impl.client.HttpClients; public class RemoteServiceClient { public static void main(String[] args) throws Exception { HttpClient client = HttpClients.createDefault(); HttpPost post = new HttpPost("http://localhost:8080/rsadmin/services/Calculator"); StringEntity input = new StringEntity("{\"a\": 10, \"b\": 5}"); input.setContentType("application/json"); post.setEntity(input); HttpResponse response = client.execute(post); if (response.getStatusLine().getStatusCode() == 200) { BufferedReader reader = new BufferedReader(new InputStreamReader(response.getEntity().getContent())); String line; while ((line = reader.readLine()) != null) { System.out.println(line); } } else { } } }


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