欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

JAX-WS:与web应用、spring的整合

程序员文章站 2022-06-17 11:35:38
...

前面几篇

接下来介绍下JAX-WS与其他容器的整合:servlet容器、spring等整合

1、与tomcat整合

JAX-WS与tomcat等servlet容器的整合,我能想到最简单的可能是直接用servlet将发布的代码写在起init方法内,如:

    public void init(ServletConfig servletConfig) throws ServletException {
        super.init(servletConfig);
        Endpoint.publish("http://localhost:8080/service/jaxWsService", new JaxWsServiceImpl());
    }

当然有更好的做法,在JAX-RT有提供与servlet集成的代理类,只需要在你项目中加上相应的依赖。下面是maven的依赖:

<dependency>
	<groupId>com.sun.xml.ws</groupId>
	<artifactId>jaxws-rt</artifactId>
	<version>2.1.4</version>
</dependency>

接下里按照之前的流程编写服务端代码:

@WebService
public class JaxWsWebServiceImpl implements JaxWsWebService {

    @WebMethod
    public @WebResult
    String getMessage() {
        return "Hello";
    }

}

由于默认是document-style需要生成返回的代理类:

F:\git\webservice\sws\sws-server\target\classes>wsgen -verbose -keep -cp . -wsdl org.ws.server.ws.chap8.impl.JaxWsWebServiceImpl

接下来是对web.xml的配置:需要加入一个监听器和servlet实例:

<listener>
	<listener-class>
		com.sun.xml.ws.transport.http.servlet.WSServletContextListener
	</listener-class>
</listener>

<servlet>
	<servlet-name>helloJaxWs</servlet-name>
	<servlet-class>
		com.sun.xml.ws.transport.http.servlet.WSServlet
	</servlet-class>
	<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
	<servlet-name>helloJaxWs</servlet-name>
	<url-pattern>/helloJaxWsWeb</url-pattern>
</servlet-mapping>

还需要一个指定名为sun-jaxws.xml位于/WEB-INF/下:

<?xml version="1.0" encoding="UTF-8"?>
<endpoints
	xmlns="http://java.sun.com/xml/ns/jax-ws/ri/runtime"
	version="2.0">
	<endpoint
		name="helloJaxWsWeb"
		implementation="org.ws.server.ws.chap8.impl.JaxWsWebServiceImpl"
		url-pattern="/helloJaxWsWeb"/>
</endpoints>

在tomcat下运行该工程,访问即可:

http://localhost/sws-server/helloJaxWsWeb?wsdl

这样就完成了与web容器的集成,但是这样会引进很多依赖的包,见jaxws-rt-2.1.3.jar的MANIFEST.MF:

Major-Version: 2.1.3
Class-Path: jaxws-api.jar jsr181-api.jar jsr250-api.jar saaj-api.jar s
 aaj-impl.jar jsr173_api.jar sjsxp.jar resolver.jar jaxb-api.jar jaxb-
 impl.jar activation.jar stax-ex.jar streambuffer.jar stax-utils.jar

2、与spring的整合

这里主要考虑spring原生提供的对JAX-WS的支持,位于spring-web-xxx.jar下,分别是SimpleJaxWsServiceExporter和 SimpleHttpServerJaxWsServiceExporter

1、SimpleJaxWsServiceExporter
继承自AbstractJaxWsServiceExporter,在bean的初始化方法发布
@Override
protected void publishEndpoint(Endpoint endpoint, WebService annotation) {
	endpoint.publish(calculateEndpointAddress(endpoint, annotation.serviceName()));
}
 与我们之前介绍的
Endpoint.publish("http://localhost:8080/service/helloJaxWs", new HelloJAXWSImpl());
 类似,下面是一个配置:
@WebService(serviceName = "jaxWsSpringService")
public class JaxWsSpringServiceImpl implements JaxWsSpringService {

    @WebMethod
    public String sayHello() {
        return "Hello JaxWs-Spring";
    }

}
 注意这里serviceName需要指定,见上面的annotation.serviceName(),作为访问的方法。下面是spring的配置文件:
<bean class= "org.springframework.remoting.jaxws.SimpleJaxWsServiceExporter">
	<property name="baseAddress" value="http://localhost:8888/service/"></property>
</bean>
<bean class="org.ws.server.ws.chap6.impl.JaxWsSpringServiceImpl"/>
 在web容器或其他方式运行,这里在main方法测试:
ClassPathXmlApplicationContext xmlApplication = new ClassPathXmlApplicationContext("jaxws-spring.xml");
 页面访问http://localhost:8888/service/jaxWsSpringService?wsdl即可

2、SimpleHttpServerJaxWsServiceExporter
同样继承自AbstractJaxWsServiceExporter,在bean的初始化方法发布于SimpleJaxWsServiceExporter类似,只是采用的自定义的HttpServer:
@Override
protected void publishEndpoint(Endpoint endpoint, WebService annotation) {
	endpoint.publish(buildHttpContext(endpoint, annotation.serviceName()));
}
 其中
protected HttpContext buildHttpContext(Endpoint endpoint, String serviceName) {
	String fullPath = calculateEndpointPath(endpoint, serviceName);
	HttpContext httpContext = this.server.createContext(fullPath);
	if (this.filters != null) {
		httpContext.getFilters().addAll(this.filters);
	}
	if (this.authenticator != null) {
		httpContext.setAuthenticator(this.authenticator);
	}
	return httpContext;
}
 因此采用这种配置需要制定server实例,当然也可以配置filters,authenticator等更多的扩展。如一个典型的制定ip访问
public class AuthHandler extends Authenticator {

    @Override
    public Result authenticate(HttpExchange httpexchange) {
        String ip = httpexchange.getRemoteAddress().getAddress().getHostAddress();
        if (!("127.0.0.1".equals(ip))) {
            return new Authenticator.Failure(403);
        }
        return new Authenticator.Success(null);
    }

}
 具体配置如下:
<bean class="org.springframework.remoting.jaxws.SimpleHttpServerJaxWsServiceExporter">
	<property name="server" ref="server" />
	<property name="basePath" value="/ws/" />
	<property name="authenticator" ref="authenticator" />
</bean>

<bean id="server" class="org.springframework.remoting.support.SimpleHttpServerFactoryBean">
	<property name="hostname" value="localhost" />
	<property name="port" value="8890" />
</bean>

<bean id="authenticator" class="org.ws.server.ws.chap6.impl.AuthHandler"/>
 页面访问http://localhost:8890/ws/jaxWsSpringService?wsdl如果host正确即可见到wsdl文档,否则HTTP 错误 403(Forbidden):服务器拒绝执行该请求