在MyEclipse中建立一个新的J2EE的Web Project。
编写IHelloWorld接口文件,代码如下:
package com.test.www;
/**
* Spring工程中要使用的接口文件
*/
public interface IHelloWorld {
public String getMessage(String name);
}
编写HelloWorldImpl实现文件,代码如下:
package com.test.www.impl;
import com.test.www.IHelloWorld;
/**
* Spring工程中要使用的接口文件
*/
public class HelloWorldImpl implements IHelloWorld{
private String helloStr; //Spring中需要注入的字符串
/**
* 实现接口中的方法,得到Say Hello to <somebody>的字符串
*/
public String getMessage(String name){
return helloStr+":"+name;
}
public String getHelloStr() {
return helloStr;
}
public void setHelloStr(String helloStr) {
this.helloStr = helloStr;
}
}
编写Web-INF下的Web工程文件Web.xml,具体配置如下:
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE web-app PUBLIC "-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN" "http://java.sun.com/dtd/web-app_2_3.dtd">
<web-app>
<!—Spring框架需要引入的配置文件及相关类 -->
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>/WEB-INF/applicationContext.xml</param-value>
</context-param>
<servlet>
<servlet-name>context</servlet-name>
<servlet-class>org.springframework.web.context.ContextLoaderServlet</servlet-class>
<load-on-startup>1</load-on-startup>
</servlet>
<!—axis需要引入的Servlet -->
<servlet>
<servlet-name>axis</servlet-name>
<servlet-class>org.apache.axis.transport.http.AxisServlet</servlet-class>
<load-on-startup>2</load-on-startup>
</servlet> <servlet-mapping>
<servlet-name>axis</servlet-name>
<url-pattern>/services/*</url-pattern>
</servlet-mapping>
<!—axis的Web Service的Web发布路径 -->
</web-app>简要说明:这个配置文件里,可能还会有许多,不要怕,只需要把关于axis的copy到你自己的web.xml就可以了,不会冲突的。
编写Web-INF下的Spring工程文件applicationContext.xml,具体配置如下:
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN//EN" "http://www.springframework.org/dtd/spring-beans.dtd">
<beans>
<bean id="helloWorld"
class="com.test.www.impl.HelloWorldImpl">
<property name="helloStr">
<value>Say Hello to :</value>
</property>
</bean>
</beans>原文在这里有一些小错,不过是可以看出来的,我把正确的贴上来了,上面这段要放到services的配置下,记得要把包名和类名写对。以上就完成了整个Spring的代码部分的编写,好像到了这个地方还一直没有介绍Web Service的部分(除了如何配置axis)。其实在Spring中对Web Service进行封装很简单,仅仅需要继承org.springframework.remoting.jaxrpc.ServletEndpointSupport类,实现里面的一些方法,包装一次,将其发布出来就可以勒。
编写包装成Web Service的JaxRpcHelloWorld类,代码如下:
package com.test.www.webservice;
import java.rmi.RemoteException;
import javax.xml.rpc.ServiceException;
import org.springframework.remoting.jaxrpc.ServletEndpointSupport;
import com.test.www.IHelloWorld;
public class JaxRpcHelloWorld extends ServletEndpointSupport implements IHelloWorld{
private IHelloWorld helloWorld;
protected void onInit() throws ServiceException {
//在Spring容器中获取Bean的实例
helloWorld = (IHelloWorld) getApplicationContext().getBean(
"helloWorld");
}
public String getMessage(String name) throws RemoteException {
//执行Bean中的相同的方法
return helloWorld.getMessage(name);
}
}
最后编写server-config.wsdd文件,发布Web Service,具体代码如下:
<deployment xmlns="http://xml.apache.org/axis/wsdd/"
xmlns:java="http://xml.apache.org/axis/wsdd/providers/java">
<handler name="URLMapper" type="java:org.apache.axis.handlers.http.URLMapper"/>
<service name="HelloWorld" provider="java:RPC">
<parameter name="className"
value="/com.test.www.webservice.JaxRpcHelloWorld"/>
<parameter name="allowedMethods" value="*"/>
</service>
<transport name="http">
<requestFlow>
<handler type="URLMapper"/>
</requestFlow>
</transport>
</deployment>
所有的工作全部完成,接下来只需要启动resin来验证你的Web Service是否已经发布成功,启动resin后,在你的浏览器中输入:http://localhost:8080/<YourProject>/services/HelloWorld?wsdl,如果能发现HelloWorld(wdsl)等信息
编写IHelloWorld接口文件,代码如下:
package com.test.www;
/**
* Spring工程中要使用的接口文件
*/
public interface IHelloWorld {
public String getMessage(String name);
}
编写HelloWorldImpl实现文件,代码如下:
package com.test.www.impl;
import com.test.www.IHelloWorld;
/**
* Spring工程中要使用的接口文件
*/
public class HelloWorldImpl implements IHelloWorld{
private String helloStr; //Spring中需要注入的字符串
/**
* 实现接口中的方法,得到Say Hello to <somebody>的字符串
*/
public String getMessage(String name){
return helloStr+":"+name;
}
public String getHelloStr() {
return helloStr;
}
public void setHelloStr(String helloStr) {
this.helloStr = helloStr;
}
}
编写Web-INF下的Web工程文件Web.xml,具体配置如下:
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE web-app PUBLIC "-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN" "http://java.sun.com/dtd/web-app_2_3.dtd">
<web-app>
<!—Spring框架需要引入的配置文件及相关类 -->
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>/WEB-INF/applicationContext.xml</param-value>
</context-param>
<servlet>
<servlet-name>context</servlet-name>
<servlet-class>org.springframework.web.context.ContextLoaderServlet</servlet-class>
<load-on-startup>1</load-on-startup>
</servlet>
<!—axis需要引入的Servlet -->
<servlet>
<servlet-name>axis</servlet-name>
<servlet-class>org.apache.axis.transport.http.AxisServlet</servlet-class>
<load-on-startup>2</load-on-startup>
</servlet> <servlet-mapping>
<servlet-name>axis</servlet-name>
<url-pattern>/services/*</url-pattern>
</servlet-mapping>
<!—axis的Web Service的Web发布路径 -->
</web-app>简要说明:这个配置文件里,可能还会有许多,不要怕,只需要把关于axis的copy到你自己的web.xml就可以了,不会冲突的。
编写Web-INF下的Spring工程文件applicationContext.xml,具体配置如下:
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN//EN" "http://www.springframework.org/dtd/spring-beans.dtd">
<beans>
<bean id="helloWorld"
class="com.test.www.impl.HelloWorldImpl">
<property name="helloStr">
<value>Say Hello to :</value>
</property>
</bean>
</beans>原文在这里有一些小错,不过是可以看出来的,我把正确的贴上来了,上面这段要放到services的配置下,记得要把包名和类名写对。以上就完成了整个Spring的代码部分的编写,好像到了这个地方还一直没有介绍Web Service的部分(除了如何配置axis)。其实在Spring中对Web Service进行封装很简单,仅仅需要继承org.springframework.remoting.jaxrpc.ServletEndpointSupport类,实现里面的一些方法,包装一次,将其发布出来就可以勒。
编写包装成Web Service的JaxRpcHelloWorld类,代码如下:
package com.test.www.webservice;
import java.rmi.RemoteException;
import javax.xml.rpc.ServiceException;
import org.springframework.remoting.jaxrpc.ServletEndpointSupport;
import com.test.www.IHelloWorld;
public class JaxRpcHelloWorld extends ServletEndpointSupport implements IHelloWorld{
private IHelloWorld helloWorld;
protected void onInit() throws ServiceException {
//在Spring容器中获取Bean的实例
helloWorld = (IHelloWorld) getApplicationContext().getBean(
"helloWorld");
}
public String getMessage(String name) throws RemoteException {
//执行Bean中的相同的方法
return helloWorld.getMessage(name);
}
}
最后编写server-config.wsdd文件,发布Web Service,具体代码如下:
<deployment xmlns="http://xml.apache.org/axis/wsdd/"
xmlns:java="http://xml.apache.org/axis/wsdd/providers/java">
<handler name="URLMapper" type="java:org.apache.axis.handlers.http.URLMapper"/>
<service name="HelloWorld" provider="java:RPC">
<parameter name="className"
value="/com.test.www.webservice.JaxRpcHelloWorld"/>
<parameter name="allowedMethods" value="*"/>
</service>
<transport name="http">
<requestFlow>
<handler type="URLMapper"/>
</requestFlow>
</transport>
</deployment>
所有的工作全部完成,接下来只需要启动resin来验证你的Web Service是否已经发布成功,启动resin后,在你的浏览器中输入:http://localhost:8080/<YourProject>/services/HelloWorld?wsdl,如果能发现HelloWorld(wdsl)等信息
解决方案 »
免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货