`
lw4135
  • 浏览: 45023 次
  • 性别: Icon_minigender_1
  • 来自: 长沙
社区版块
存档分类
最新评论

WebService版集成Spring的HelloWorld

阅读更多
HelloWorld接口:

package com;

import javax.jws.WebService;

@WebService
public interface HelloWorld {
  public String sayHi();
  public String sayHiToUser(User user);
  
}

HelloWorld实现类:


package com;

import javax.jws.WebService;

@WebService(endpointInterface="com.HelloWorld",serviceName="HelloWorld")
public class HelloWorldImpl  implements HelloWorld{

	public String sayHi() {
		return "Hello world33!";
	}

	public String sayHiToUser(User user) {
		String name=user.getName();
		
		return "Hello33"+name+"!";
	}

}

用户对象模型:


package com;

public class User {
	private String name;
	private String password;
	private int age;
	public String getName() {
		return name;
	}
	public void setName(String name) {
		this.name = name;
	}
	public String getPassword() {
		return password;
	}
	public void setPassword(String password) {
		this.password = password;
	}
	public int getAge() {
		return age;
	}
	public void setAge(int age) {
		this.age = age;
	}

}

服务器程序:

package com;

import javax.xml.ws.Endpoint;

public class WebServiceApp {
	public static void main(String[] args) {
		System.out.println("Webservice启动中……");
		HelloWorldImpl impl = new HelloWorldImpl();
		String address = "http://localhost:8080/helloWorld";
		Endpoint.publish(address, impl);
		System.out.println("Webservice已启动……");
	}

}


客户端程序:


package com;

import org.apache.cxf.jaxws.JaxWsProxyFactoryBean;

public class WebserviceClient {
	/**
	 * @param java api webservice工厂模式
	 *          
	 */
	public static void main(String[] args) {

		JaxWsProxyFactoryBean factory = new JaxWsProxyFactoryBean();
		factory.setServiceClass(HelloWorld.class);
		factory.setAddress("http://10.2.104.33:8080/helloWorld");
		HelloWorld helloWorld = (HelloWorld) factory.create();
		System.out.println("第一次调用webservice……");
		System.out.println(helloWorld.sayHi());
		System.out.println("第二次调用webservice……");
        User user=new User();
        user.setName("liuwang");
        user.setAge(20);
        user.setPassword("haha");
        System.out.println(helloWorld.sayHiToUser(user));
	}
}


集成spring的客户端:
package com;

import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

public class WebServieSpring {
	public static void main(String[] args) {
		ApplicationContext context = new ClassPathXmlApplicationContext(
				"applicationContext.xml");
		HelloWorld client = (HelloWorld) context.getBean("client");
		System.out.println(client.sayHi());
		User user=new User();
		user.setName("刘望");
		System.out.println(client.sayHiToUser(user));
	}
}


applicationContext.xml配置文件:


<?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"/>
    
    <jaxws:endpoint id="helloWorld"
    	implementor="com.HelloWorldImpl"
    	address="/helloWorld"/>
    
    <bean id="client" class="com.HelloWorld"
    	factory-bean="clientFactory" factory-method="create"/>
    	
    <bean id="clientFactory" class="org.apache.cxf.jaxws.JaxWsProxyFactoryBean">
    	<property name="serviceClass" value="com.HelloWorld"></property>
    	<property name="address" value="http://10.2.104.33:8080/webservice/webservice/helloWorld"></property>
    </bean>
</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">
	<display-name></display-name>
	<welcome-file-list>
		<welcome-file>index.jsp</welcome-file>
	</welcome-file-list>

	<context-param>
		<param-name>contextConfigLocation</param-name>
		<param-value>
				/WEB-INF/classes/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>/webservice/*</url-pattern>
		</servlet-mapping>
</web-app>
  • 大小: 226.8 KB
5
1
分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics