Java web service without a web application server

*爱你&永不变心* 提交于 2019-11-27 09:17:30

You don't need a third party library to use annotations. J2SE ships with , so all the annotations are still available to you. You can achieve lightweight results with the following solution, but for anything optimized/multi-threaded, it's on your own head to implement:

  1. Design a SEI, service endpoint interface, which is basically a java interface with web-service annotations. This is not mandatory, it's just a point of good design from basic OOP.

    import javax.jws.WebService;
    import javax.jws.WebMethod;
    import javax.jws.WebParam;
    import javax.jws.soap.SOAPBinding;
    import javax.jws.soap.SOAPBinding.Style;
    
    @WebService
    @SOAPBinding(style = Style.RPC) //this annotation stipulates the style of your ws, document or rpc based. rpc is more straightforward and simpler. And old.
    public interface MyService{
    @WebMethod String getString();
    
    }
    
  2. Implement the SEI in a java class called a SIB service implementation bean.

    @WebService(endpointInterface = "com.yours.wsinterface") //this binds the SEI to the SIB
    public class MyServiceImpl implements MyService {
    public String getResult() { return "result"; }
     }
    
  3. Expose the service using an Endpoint import javax.xml.ws.Endpoint;

    public class MyServiceEndpoint{
    
    public static void main(String[] params){
      Endpoint endPoint =  EndPoint.create(new MyServiceImpl());
      endPoint.publish("http://localhost:9001/myService"); //supply your desired url to the publish method to actually expose the service.
       }
    }
    

The snippets above, like I said, are pretty basic, and will perform poorly in production. You'll need to work out a threading model for requests. The endpoint API accepts an instance of Executor to support concurrent requests. Threading's not really my thing, so I'm unable to give you pointers.

For using nice j2ee annotations see Apache CXF http://cxf.apache.org/.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!