WebServiceのCXFは簡単に使えます
5303 ワード
サービス:
実装クラス
スプリング構成
web.xml
クライアント
テストクラス
public class Test {
スプリング構成
public interface ServerInter {
void sayWord(String word);
}
実装クラス
@WebService
public class ServerInterImpl implements ServerInter {
public void sayWord(String word) {
System.out.println(word);
}
}
スプリング構成
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:jaxws="http://cxf.apache.org/jaxws"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd
http://cxf.apache.org/jaxws http://cxf.apache.org/schemas/jaxws.xsd">
<import resource="classpath:META-INF/cxf/cxf.xml"/>
<import resource="classpath:META-INF/cxf/cxf-extension-soap.xml"/>
<import resource="classpath:META-INF/cxf/cxf-servlet.xml"/>
<!-- bean -->
<bean id="serverService" class="com.server.inter.impl.ServerInterImpl"/>
<!-- serviceClass address webservice http://localhost:8080/WebServiceServer/serverWebService?wsdl-->
<jaxws:server id="serverWebService" serviceClass="com.server.inter.ServerInter" address="/serverWebService">
<jaxws:serviceBean>
<ref bean = "serverService"/><!-- bean -->
</jaxws:serviceBean>
</jaxws:server>
</beans>
web.xml
<?xml version="1.0" encoding="UTF-8"?>
<web-app version="2.5" xmlns="http://java.sun.com/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee
http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd">
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath*:/applicationContext.xml</param-value>
</context-param>
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<servlet>
<servlet-name>cxfServlet</servlet-name>
<servlet-class>org.apache.cxf.transport.servlet.CXFServlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>cxfServlet</servlet-name>
<url-pattern>/*</url-pattern>
</servlet-mapping>
<welcome-file-list>
<welcome-file>index.jsp</welcome-file>
</welcome-file-list>
</web-app>
クライアント
@WebService
public interface ServerInter {
void sayWord(String word);
}
テストクラス
public class Test {
//1. spring bean
public void app1(){
ClassPathXmlApplicationContext resource = new ClassPathXmlApplicationContext("applicationContext.xml");
ServerInter serverInter = (ServerInter)resource.getBean("clientWebService");
serverInter.sayWord("aaaaa");
}
//2. JaxWsProxyFactoryBean bean, spring
public void app2(){
String address = "http://localhost:8080/WebServiceServer/serverWebService";
JaxWsProxyFactoryBean jwpf = new JaxWsProxyFactoryBean();
jwpf.setAddress(address);
jwpf.setServiceClass(ServerInter.class);
ServerInter si = (ServerInter) jwpf.create();
si.sayWord("bbbbb");
}
public static void main(String[] args) {
Test test = new Test();
test.app1();
test.app2();
}
}
スプリング構成
<?xml version="1.0" encoding="UTF-8"?><beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:jaxws="http://cxf.apache.org/jaxws"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd
http://cxf.apache.org/jaxws http://cxf.apache.org/schemas/jaxws.xsd">
<import resource="classpath:META-INF/cxf/cxf.xml"/>
<import resource="classpath:META-INF/cxf/cxf-extension-soap.xml"/>
<import resource="classpath:META-INF/cxf/cxf-servlet.xml"/>
<!-- -->
<!-- address , serverWebService -->
<jaxws:client id="clientWebService" serviceClass="com.server.inter.ServerInter"
address="http://localhost:8080/WebServiceServer/serverWebService">
</jaxws:client>
</beans>