WebService CXF+struts+spring 示例

 

最近学校WebService的CXF..做了一个示例程序是 CXF+Struts+spring的...

有用的就看看.没用的就飘过!

 

1:类的编写(bean):

@XmlRootElement(name="customer")
@XmlAccessorType(XmlAccessType.PROPERTY)
@XmlType(propOrder={"name","age"})
public class Customer {
	
	private int age;
	private String name;
	
	
	public int getAge() {
		return age;
	}
	public void setAge(int age) {
		this.age = age;
	}
	public String getName() {
		return name;
	}
	public void setName(String name) {
		this.name = name;
	}
	

 2:Service的编写:

@WebService
public interface HelloService {
	
	public void save(Customer c1,Customer c2);
	public void test(String args);
	public Customer get(int id);
}

HelloServiceImpl:

@WebService
public class HelloServiceImpl implements HelloService {

	public Customer get(int id) {
		Customer cus=new Customer();
		cus.setAge(100);
		cus.setName("LLLL");
		return cus;
	}

	public void save(Customer c1, Customer c2) {
		System.out.println(c1.getAge()+"-------"+c2.getAge());
		System.out.println(c1.getName()+"-------"+c2.getName());

	}

	public void test(String args) {
		System.out.println(args);

	}

3:spring配置..发布节点:

       <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 id="Customer" class="org.web.HelloServiceImpl"></bean>
	<jaxws:endpoint 
	  id="custom" 
	  implementor="#Customer" 
	  address="/web" />

PS:  web.xml配置:

 

 

	 <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>/web/*</url-pattern>
	</servlet-mapping>

		<filter>
		<filter-name>struts2</filter-name>
		<filter-class>
			org.web.StrutsFilter
		</filter-class>
	</filter>
	<filter-mapping>
		<filter-name>struts2</filter-name>
		<url-pattern>/*</url-pattern>
	</filter-mapping>	

自己写的struts过滤器:

public class StrutsFilter extends FilterDispatcher {
public void init(FilterConfig filterConfig) throws ServletException {
		 super.init(filterConfig);
	 }
public void doFilter(ServletRequest req, ServletResponse res,   
		   FilterChain chain) throws IOException, ServletException {   
		 
		      String url = ((HttpServletRequest)req).getRequestURI();   
		      if (url.indexOf("fckeditor") < 0 && url.indexOf("web") < 0) {   
		             super.doFilter(req, res, chain);    
		       } else {   
		             chain.doFilter(req, res);   
		       }     
		    }   
}
 

 

 PS:struts会过滤掉cxf..即cxf会被认为是一个action..所以无法查看发布:

所以我自己写了一个Filter如上

这样就可以避免不能查看了!!!呵呵...

 

 

服务端算是完成:可以测试下:http://127.0.0.1:88/Hello/web

可以看见如下内容:

CXF

  4:客户端:

①:类的编写

action类的编写

@SuppressWarnings("serial")
public class SoapAction extends ActionSupport {
	private HelloService hello;
	
	public HelloService getHello() {
		return hello;
	}

	public void setHello(HelloService hello) {
		this.hello = hello;
	}

	public String test(){
		Customer c1=new Customer();
		c1.setAge(1);
		c1.setName("one");
		
		Customer c2=new Customer();
		c2.setAge(2);
		c2.setName("two");
		
		hello.save(c1, c2);
		hello.test("11111111111");
		
		return "success";
	}
}

struts的配置:

<action name="Test" method="test" class="test">
		 	<result name="success">/index.jsp</result>
</action>
 

spring的配置(重点!!)

<bean id="webTest" class="org.web.HelloService" factory-bean="client" factory-method="create"/>	
 <bean id="client" class="org.apache.cxf.jaxws.JaxWsProxyFactoryBean">
	 		<property name="serviceClass" value="org.web.HelloService"></property>
	 		<property name="address" value="http://127.0.0.1:88/Hello/web/web"></property>
	 </bean>
	 <bean id="test" class="org.web.SoapAction" >
	 		<property name="hello" ref="webTest"></property>
	 </bean>
 

好了发布测试:

http://127.0.0.1:88/Hello/Test.action

成功!!!

 

鼓掌!!!

 

 

 

 

 

代码已经上传!jar包由于过大的原因没有上传!

给了一个jar包的截图!!

WebService CXF+struts+spring 示例

 

 

你可能感兴趣的:(spring,bean,webservice,struts,servlet)