Simple standalone REST service with Apache CXF
by Eirik Tenold
I wanted to create a simple REST service with Apache CXF. The simple steps:
In pom.xml:
The service implementation:
@Path(“/a/”) public class MyServiceImpl {
@GET
@Path(“/b”)
@Produces(“application/json”)
public String test() {
return “42”;
} }
To start the service as a standalone app:
public static void main(String[] args) { MyServiceImpl myService = new MyServiceImpl();
JAXRSServerFactoryBean factoryBean = new JAXRSServerFactoryBean();
factoryBean.setResourceClasses(MyServiceImpl.class);
factoryBean.setResourceProvider(MyServiceImpl.class, new SingletonResourceProvider(myService));
factoryBean.setAddress(“http://localhost:8080/info”);
Server server = factoryBean.create(); }