使用java创建webService可以使用axis 也可以使用 cxf,axis需要打包成 arr cxf可以直接发布
首先引用相应的jar包 以及 spring相关的jar包
编写相应的接口类
实现类
在 spring 配置文件中添加:
在web.xml中添加配置
至此服务端配置完成
首先引用相应的jar包 以及 spring相关的jar包
在spring 配置文件的命名空间中引入cxf相关的包路径
xmlns:jaxws="http://cxf.apache.org/jaxws"
xsi:schemaLocation="
http://cxf.apache.org/jaxws http://cxf.apache.org/schemas/jaxws.xsd"
编写相应的接口类
@WebService
public interface GreetingService {
public String greeting(String userName);
}
实现类
import java.util.Calendar;
import javax.jws.WebService;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import com.company.cxf.service.GreetingService;
import com.company.cxf.dao.*;
@WebService(endpointInterface = "com.company.cxf.service.GreetingService")
public class GreetingServiceImpl implements GreetingService {
public String greeting(String userName){
//servletConfig.getServletContext().getRealPath("/");
ApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml");
UserMapper us=(UserMapper)context.getBean("UserMapper");
return "Hello " + us.getUserName(userName) + ", currentTime is " + Calendar.getInstance().getTime();
}
}
在 spring 配置文件中添加:
<jaxws:endpoint id="greetingService"
implementor="com.company.cxf.service.impl.GreetingServiceImpl"
address="/GreetingService" />
在web.xml中添加配置
<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>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>CXFServlet</servlet-name>
<url-pattern>/*</url-pattern>
</servlet-mapping>
至此服务端配置完成
调用类
package com.company.cxf.client;
import org.apache.cxf.jaxws.JaxWsProxyFactoryBean;
import com.company.cxf.service.GreetingService;
public class TestGreetingService {
public static void main(String[] args) {
// 创建WebService客户端代理工厂
JaxWsProxyFactoryBean factory = new JaxWsProxyFactoryBean();
// 注册WebService接口
factory.setServiceClass(GreetingService.class);
// 设置WebService地址
factory.setAddress("http://localhost:8080/cxf_webservice/GreetingService");
GreetingService greetingService = (GreetingService) factory.create();
System.out.println("invoke webservice...");
System.out.println("message context is:" + greetingService.greeting("SEX"));
}
}