blog.relativt.net

Eirik's public note to self

1 February 2013

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:

org.apache.cxf cxf-rt-frontend-jaxrs 2.6.1 org.apache.cxf cxf-rt-transports-http-jetty 2.6.1

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(); }
tags: REST - Apache CXF - Maven