Skip to content

How to deploy a Java web service using xpresso

aburkov edited this page Jun 29, 2015 · 14 revisions

With xpresso you can deploy a RESTful web service in Java in no time.

Let's suppose we have an object of a class SomeMath which has two methods we would like to publish on the network as web services, getSum and getProduct:

public class SomeMath() {
	public Double getSum(Double[] values) { //we want to publish this one
		return x.sum(values);
	}
	public Double getProduct(Double x, Double y) {//and this one
		return x * y;
	}
	public Double anotherMethod(Double somethingElse) {//but not this one
		return somethingElse;
	}
}

In order to convert our SomeMath class into a web service, we simply need to first annotate our two methods with the @ExposeAs annotation and then start our web service:

public class SomeMath() {
	public Double getSum(@ExposeAs("values") Double[] values) {
		return x.sum(values);
	}
	public Double getProduct(@ExposeAs("x") Double x, @ExposeAs("y") Double y) {
		return x * y;
	}
	public Double anotherMethod(Double somethingElse) {
		return somethingElse;
	}
}

WebService ws = x.WebService(new SomeMath(), 8080).start();

That's all! Our web service is up and running. Let's test it. Open the following url in your browser:

http://localhost:8080/SomeMath/getSum?values=5&values=6&values=7

The output:

18.0

Now open the following url:

http://localhost:8080/SomeMath/getProduct?x=5&y=10

The output:

50.0

If a method returns an output type of more complex classes such as Java's standard Map and List, or xpresso's own list and dict, the output will be a corresponding JSON string.

You can set the number of concurrent threads for your ws web service by calling ws.setMaximumConcurrentThreads(intValue). There's no limit on the number of concurrent threads by default.