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>
[img]http://dl.iteye.com/upload/attachment/577435/567d7f70-0110-3994-b14d-ba684a382891.jpg[/img]
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值