首页 > CXF如何自动运行 REST API

CXF如何自动运行 REST API

java
public final class Server { private static Logger LOGGER = Logger.getLogger(Server.class); private final String SERVICE = "/travelman/rest/" ; private final String PORT = "9002" ; public void publish(Class<?> serviceClass, Object impl) { JAXRSServerFactoryBean sf = new JAXRSServerFactoryBean(); sf.setResourceClasses(serviceClass); sf.setResourceProvider(serviceClass, new SingletonResourceProvider(impl)); // String addr = "http://192.168.100.146:"+PORT+SERVICE+serviceClass.getSimpleName() ; String addr = "http://192.168.1.111:"+PORT+SERVICE+serviceClass.getSimpleName() ; LOGGER.info("Rest Service:"+addr); sf.setAddress(addr); System.out.println("Service:"+addr); sf.create(); } public void startLoginUserRestApi(){ LoginUserRestApiImpl loginUserRestApiImpl=new LoginUserRestApiImpl(); this.publish(cn.com.travelman.service.rest.userinfo.LoginUserRestApi.class, loginUserRestApiImpl); } public static void main(String args[]) throws Exception { Server server = new Server(); server.startLoginUserRestApi(); System.out.println("Server ready..."); Thread.sleep(200 * 60 * 1000); System.out.println("Server exiting"); System.exit(0); }

现在运行方式是这样的。运行server.java文件就发布一个服务。但是现在要让他自动运行,该怎么办呢?


那要看你想让的rest服务以何种方式存在了。
你现在这种是stand-alone的方式运行,在入口类里利用JAXRSServerFactoryBean发布rest,你可以打成jar后在manifest里指定入口类,然后双击jar即可自动运行。
也可以把你的jar包装成系统服务,然后利用系统服务生命周期自动运行,这个可以利用Java Service Wrappe。
如果你想部署在某个web容器里,tomcat,weblogic等的话就要转换成j2ee项目,然后利用监听器在容器启动时自动运行,或集成到spring里。
总之,路子很多,你最好先弄明白你想让你的服务以哪种方式存在。

【热门文章】
【热门文章】