1。后台利用 cxf 构建一个web service服务。
- HelloWorld.java
- /**
- *
- */
- package com.alcor.jws.test;
- import javax.jws.WebMethod;
- import javax.jws.WebService;
- import org.apache.cxf.feature.Features;
- /**
- * @author 徐泽宇(roamer)
- *
- * 2010-7-10
- */
- @WebService
- @Features(features = "org.apache.cxf.feature.LoggingFeature")
- public interface HelloWorld {
- @WebMethod
- String sayHi(String text);
- @WebMethod
- boolean userLogon(String username,String userpasswd);
- }
- HelloWorldImpl.java
- /**
- *
- */
- package com.alcor.jws.test;
- import org.apache.cxf.feature.Features;
- import org.apache.log4j.Logger;
- import javax.jws.WebMethod;
- import javax.jws.WebService;
- /**
- * @author 徐泽宇(roamer)
- *
- * 2010-7-10
- */
- @WebService
- @Features(features = "org.apache.cxf.feature.LoggingFeature")
- public class HelloWorldImpl implements HelloWorld {
- /**
- * Logger for this class
- */
- private static final Logger logger = Logger.getLogger(HelloWorldImpl.class);
- @WebMethod
- public String sayHi(String text) {
- if (logger.isDebugEnabled()) {
- logger.debug("sayHi(String) - start"); //$NON-NLS-1$
- }
- String returnString = "Hello,你好: " + text;
- if (logger.isDebugEnabled()) {
- logger.debug("返回内容:"+returnString);
- logger.debug("sayHi(String) - end"); //$NON-NLS-1$
- }
- return returnString;
- }
- @WebMethod
- public boolean userLogon(String username ,String userpasswd)
- {
- logger.debug("用户名是:"+username+"口令是:"+userpasswd);
- if (username.equalsIgnoreCase("admin"))
- {
- return true;
- }else{
- return false;
- }
- }
- }
- java 的web service 访问客户端
- /**
- *
- */
- package com.alcor.jws.test;
- import org.apache.cxf.interceptor.LoggingInInterceptor;
- import org.apache.cxf.jaxws.JaxWsProxyFactoryBean;
- /**
- * @author 徐泽宇(roamer)
- *
- * 2010-7-10
- */
- public class Client {
- private Client() {
- }
- public static void main(String args[]) throws Exception {
- /*第一种方法,通过配置文件来实现 begin
- ApplicationContext ctx = new ClassPathXmlApplicationContext( "META-INF/WebServiceClient.xml");
- HelloWorld client = (HelloWorld) ctx.getBean("client");
- String result = client.sayHi("Roamer");
- System.out.println(result);
- boolean logonResult = client.userLogon("roamer", "passwd");
- System.out.println(logonResult);