且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

没有Web应用程序服务器的Java Web服务

更新时间:2022-06-25 03:39:39

您不需要第三方库来使用 jax-ws 注释。 J2SE附带 jax-ws ,所以所有的注释仍然可供您使用。您可以使用以下解决方案实现轻量级结果,但对于任何优化/多线程,您可以自行实现:

You don't need a third party library to use jax-ws annotations. J2SE ships with jax-ws, 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. 设计一个SEI服务端点接口,它基本上是一个带有Web服务注释的java接口。这不是强制性的,它只是基本OOP的良好设计点。

  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();

}


  • 在名为a的java类中实现SEI SIB服务实现bean。

  • 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"; }
     }
    


  • 使用端点公开服务
    import javax.xml.ws.Endpoint;

  • 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.
       }
    }
    


  • 就像我说的那样,上面的片段非常基本,并且在制作中表现不佳。您需要为请求设计线程模型。端点API接受的执行人实例支持并发请求。线程不是我的事,所以我无法给你指点。

    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.