webservice如今开发运用已经很容易了,这都得益于巨头们的努力.众多框架:cxf……
这个示例也是用的cxf,这是我的第一个webservice 例子,jar包这里没有上传,可以到官网下载。
HelloService.java代码:
HelloServcieImpl.java代码:
applicationContext.xml:
web.xml:
配置的说明:[quote]
那如何解决呢?
请看:web.xml:
[quote]
HelloServiceImpl.java:
[/quote]
到此服务端的内容就结束了。
下面是[size=x-large]客户端[/size]了,
客户端的:
applicationContext.xml:
配置的说明:[quote]在client.xml:
服务端的applicationContext.xml 细则说明:[quote]
jaxws:endpoint 的属性implementor说明 helloService这个接口由hello这个bean实现的。
官方的解释:[url]http://cxf.apache.org/docs/jax-ws-configuration.html[/url],上面写得很清楚,英文好的话建议看一下。
[/quote]
客户端的applicationContext.xml细则说明:
[quote]
上面两行。
两个bean共同组成了一个webserive的接口客户端的核心,其中
指出了,服务端的地址。(文字不好组织)
[/quote]
这个示例也是用的cxf,这是我的第一个webservice 例子,jar包这里没有上传,可以到官网下载。
HelloService.java代码:
package org.iteye.bbjava.ws.service;
import javax.jws.WebService;
@WebService
public interface HelloService {
String sayHi(String text);
}
HelloServcieImpl.java代码:
package org.iteye.bbjava.ws.service.impl;
import javax.jws.WebService;
import org.iteye.bbjava.ws.service.HelloService;
@WebService(endpointInterface = "org.iteye.bbjava.ws.service.HelloService")
public class HelloServiceImpl implements HelloService {
public String sayHi(String text) {
System.out.println("sayHi called");
return "Hello " + text;
}
}
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-servlet.xml" />
<jaxws:endpoint id="helloService" implementor="#hello"
address="/HelloServiceImpl" />
<bean id="hello" class="org.iteye.bbjava.ws.service.impl.HelloServiceImpl" />
</beans>
web.xml:
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" id="WebApp_ID" version="2.5">
<display-name>ws-spring</display-name>
<welcome-file-list>
<welcome-file>index.html</welcome-file>
<welcome-file>index.htm</welcome-file>
<welcome-file>index.jsp</welcome-file>
<welcome-file>default.html</welcome-file>
<welcome-file>default.htm</welcome-file>
<welcome-file>default.jsp</welcome-file>
</welcome-file-list>
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>WEB-INF/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>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>CXFServlet</servlet-name>
<url-pattern>/*</url-pattern>
</servlet-mapping>
</web-app>
配置的说明:[quote]
<servlet-name>CXFServlet</servlet-name>
<url-pattern>/*</url-pattern>
上面的url-pattern的作用比较重要,因为CXFServlet会“过虑”关相请求。我们就以上面的代码为例,在浏览器中输入[url]http://localhost:8080/[/url] 会进入webservice相关页面,也就是说在正常的main.jsp它进不去了。
那如何解决呢?
请看:web.xml:
[quote]
<servlet-name>CXFServlet</servlet-name>
<url-pattern>/service/ *</url-pattern>
HelloServiceImpl.java:
@WebService(endpointInterface = "org.iteye.bbjava.ws.service.HelloService",targetNamespace = "http://localhost:8080/service")
[/quote]
到此服务端的内容就结束了。
下面是[size=x-large]客户端[/size]了,
package org.iteye.bbjava.ws;
import org.iteye.bbjava.ws.service.HelloService;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class ClientTest {
private ClientTest() {
}
public static void main(String args[]) throws Exception {
ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext(
new String[] { "applicationContext.xml" });
HelloService client = (HelloService) context.getBean("helloService");
String response = client.sayHi("zhang");
System.out.println("Response: " + response);
}
}
客户端的:
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-2.0.xsd
http://cxf.apache.org/jaxws http://cxf.apache.org/schema/jaxws.xsd">
<bean id="helloService" class="org.iteye.bbjava.ws.service.HelloService"
factory-bean="clientFactory" factory-method="create" />
<bean id="clientFactory" class="org.apache.cxf.jaxws.JaxWsProxyFactoryBean">
<property name="serviceClass"
value="org.iteye.bbjava.ws.service.HelloService" />
<property name="address" value="http://localhost:8080/ws-spring/HelloServiceImpl" />
</bean>
</beans>
配置的说明:[quote]在client.xml:
<property name="address" value="http://localhost:8080/ws-spring/HelloServiceImpl" />
改为
<property name="address" value="http://localhost:8080/ws-spring/service/HelloServiceImpl" />
[/quote]
服务端的applicationContext.xml 细则说明:[quote]
<jaxws:endpoint id="helloService" implementor="#hello"
address="/HelloServiceImpl" />
jaxws:endpoint 的属性implementor说明 helloService这个接口由hello这个bean实现的。
官方的解释:[url]http://cxf.apache.org/docs/jax-ws-configuration.html[/url],上面写得很清楚,英文好的话建议看一下。
[/quote]
客户端的applicationContext.xml细则说明:
[quote]
<bean id="helloService" class="org.iteye.bbjava.ws.service.HelloService"
factory-bean="clientFactory" factory-method="create" />
<bean id="clientFactory" class="org.apache.cxf.jaxws.JaxWsProxyFactoryBean">
<property name="serviceClass"
value="org.iteye.bbjava.ws.service.HelloService" />
<property name="address" value="http://localhost:8080/ws-spring/HelloServiceImpl" />
</bean>
上面两行。
两个bean共同组成了一个webserive的接口客户端的核心,其中
<property name="address" value="http://localhost:8080/ws-spring/HelloServiceImpl" />
指出了,服务端的地址。(文字不好组织)
[/quote]