Spring Web Service 学习之Hello World篇2
2008-11-03 15:32
435 查看
6, 修改配置文件spring-ws-servlet.xml.
Java代码
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.0.xsd">
<bean id="payloadMapping" class="org.springframework.ws.server.endpoint.mapping.PayloadRootQNameEndpointMapping">
<property name="endpointMap">
<map>
<entry key=”{http://www.fuxueliang.com/ws/hello}HelloRequest” />
<ref bean="helloEndpoint" />
</entry>
</map>
</property>
</bean>
<bean id="hello" class="org.springframework.ws.wsdl.wsdl11.SimpleWsdl11Definition">
<property name="wsdl" value="/WEB-INF/hello.wsdl"/>
</bean>
<bean id="helloEndpoint" class="org.rondy.ws.HelloEndPoint">
<property name="helloService" ref="helloService" />
</bean>
<bean id="helloService" class="org.rondy.service.HelloServiceImpl" />
</beans>
注: 其中最主要的bean就是payloadMapping, 它定义了接收到的message与endpoint之间的mapping关系:将SOAP Body中包含的xml的根节点的QName为{http://www.fuxueliang.com/ws/hello}HelloRequest交给helloEndpoint处理.
SimpleWsdl11Definition这个bean则是定义了这个服务的wsdl, 访问地址是:http://localhost:8080/springws/hello.wsdl.
7, 客户端(saaj实现)的代码如下:
Java代码
/**
*
* @author Rondy.F
*
*/
public class HelloWebServiceClient {
public static final String NAMESPACE_URI = "http://www.fuxueliang.com/ws/hello";
public static final String PREFIX = "tns";
private SOAPConnectionFactory connectionFactory;
private MessageFactory messageFactory;
private URL url;
public HelloWebServiceClient(String url) throws SOAPException, MalformedURLException {
connectionFactory = SOAPConnectionFactory.newInstance();
messageFactory = MessageFactory.newInstance();
this.url = new URL(url);
}
private SOAPMessage createHelloRequest() throws SOAPException {
SOAPMessage message = messageFactory.createMessage();
SOAPEnvelope envelope = message.getSOAPPart().getEnvelope();
Name helloRequestName = envelope.createName("HelloRequest", PREFIX, NAMESPACE_URI);
SOAPBodyElement helloRequestElement = message.getSOAPBody().addBodyElement(helloRequestName);
helloRequestElement.setValue("Rondy.F");
return message;
}
public void callWebService() throws SOAPException, IOException {
SOAPMessage request = createHelloRequest();
SOAPConnection connection = connectionFactory.createConnection();
SOAPMessage response = connection.call(request, url);
if (!response.getSOAPBody().hasFault()) {
writeHelloResponse(response);
} else {
SOAPFault fault = response.getSOAPBody().getFault();
System.err.println("Received SOAP Fault");
System.err.println("SOAP Fault Code :" + fault.getFaultCode());
System.err.println("SOAP Fault String :" + fault.getFaultString());
}
}
@SuppressWarnings("unchecked")
private void writeHelloResponse(SOAPMessage message) throws SOAPException {
SOAPEnvelope envelope = message.getSOAPPart().getEnvelope();
Name helloResponseName = envelope.createName("HelloResponse", PREFIX, NAMESPACE_URI);
Iterator childElements = message.getSOAPBody().getChildElements(helloResponseName);
SOAPBodyElement helloResponseElement = (SOAPBodyElement) childElements.next();
String value = helloResponseElement.getTextContent();
System.out.println("Hello Response [" + value + "]");
}
public static void main(String[] args) throws Exception {
String url = "http://localhost:8080/springws";
HelloWebServiceClient helloClient = new HelloWebServiceClient(url);
helloClient.callWebService();
}
}
几点看法:
1, 从上面代码可以看出, 比较麻烦的部分就是客户端和服务端对xml处理
Java代码
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.0.xsd">
<bean id="payloadMapping" class="org.springframework.ws.server.endpoint.mapping.PayloadRootQNameEndpointMapping">
<property name="endpointMap">
<map>
<entry key=”{http://www.fuxueliang.com/ws/hello}HelloRequest” />
<ref bean="helloEndpoint" />
</entry>
</map>
</property>
</bean>
<bean id="hello" class="org.springframework.ws.wsdl.wsdl11.SimpleWsdl11Definition">
<property name="wsdl" value="/WEB-INF/hello.wsdl"/>
</bean>
<bean id="helloEndpoint" class="org.rondy.ws.HelloEndPoint">
<property name="helloService" ref="helloService" />
</bean>
<bean id="helloService" class="org.rondy.service.HelloServiceImpl" />
</beans>
<?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.0.xsd"> <bean id="payloadMapping" class="org.springframework.ws.server.endpoint.mapping.PayloadRootQNameEndpointMapping"> <property name="endpointMap"> <map> <entry key=”{http://www.fuxueliang.com/ws/hello}HelloRequest” /> <ref bean="helloEndpoint" /> </entry> </map> </property> </bean> <bean id="hello" class="org.springframework.ws.wsdl.wsdl11.SimpleWsdl11Definition"> <property name="wsdl" value="/WEB-INF/hello.wsdl"/> </bean> <bean id="helloEndpoint" class="org.rondy.ws.HelloEndPoint"> <property name="helloService" ref="helloService" /> </bean> <bean id="helloService" class="org.rondy.service.HelloServiceImpl" /> </beans>
注: 其中最主要的bean就是payloadMapping, 它定义了接收到的message与endpoint之间的mapping关系:将SOAP Body中包含的xml的根节点的QName为{http://www.fuxueliang.com/ws/hello}HelloRequest交给helloEndpoint处理.
SimpleWsdl11Definition这个bean则是定义了这个服务的wsdl, 访问地址是:http://localhost:8080/springws/hello.wsdl.
7, 客户端(saaj实现)的代码如下:
Java代码
/**
*
* @author Rondy.F
*
*/
public class HelloWebServiceClient {
public static final String NAMESPACE_URI = "http://www.fuxueliang.com/ws/hello";
public static final String PREFIX = "tns";
private SOAPConnectionFactory connectionFactory;
private MessageFactory messageFactory;
private URL url;
public HelloWebServiceClient(String url) throws SOAPException, MalformedURLException {
connectionFactory = SOAPConnectionFactory.newInstance();
messageFactory = MessageFactory.newInstance();
this.url = new URL(url);
}
private SOAPMessage createHelloRequest() throws SOAPException {
SOAPMessage message = messageFactory.createMessage();
SOAPEnvelope envelope = message.getSOAPPart().getEnvelope();
Name helloRequestName = envelope.createName("HelloRequest", PREFIX, NAMESPACE_URI);
SOAPBodyElement helloRequestElement = message.getSOAPBody().addBodyElement(helloRequestName);
helloRequestElement.setValue("Rondy.F");
return message;
}
public void callWebService() throws SOAPException, IOException {
SOAPMessage request = createHelloRequest();
SOAPConnection connection = connectionFactory.createConnection();
SOAPMessage response = connection.call(request, url);
if (!response.getSOAPBody().hasFault()) {
writeHelloResponse(response);
} else {
SOAPFault fault = response.getSOAPBody().getFault();
System.err.println("Received SOAP Fault");
System.err.println("SOAP Fault Code :" + fault.getFaultCode());
System.err.println("SOAP Fault String :" + fault.getFaultString());
}
}
@SuppressWarnings("unchecked")
private void writeHelloResponse(SOAPMessage message) throws SOAPException {
SOAPEnvelope envelope = message.getSOAPPart().getEnvelope();
Name helloResponseName = envelope.createName("HelloResponse", PREFIX, NAMESPACE_URI);
Iterator childElements = message.getSOAPBody().getChildElements(helloResponseName);
SOAPBodyElement helloResponseElement = (SOAPBodyElement) childElements.next();
String value = helloResponseElement.getTextContent();
System.out.println("Hello Response [" + value + "]");
}
public static void main(String[] args) throws Exception {
String url = "http://localhost:8080/springws";
HelloWebServiceClient helloClient = new HelloWebServiceClient(url);
helloClient.callWebService();
}
}
/** * * @author Rondy.F * */ public class HelloWebServiceClient { public static final String NAMESPACE_URI = "http://www.fuxueliang.com/ws/hello"; public static final String PREFIX = "tns"; private SOAPConnectionFactory connectionFactory; private MessageFactory messageFactory; private URL url; public HelloWebServiceClient(String url) throws SOAPException, MalformedURLException { connectionFactory = SOAPConnectionFactory.newInstance(); messageFactory = MessageFactory.newInstance(); this.url = new URL(url); } private SOAPMessage createHelloRequest() throws SOAPException { SOAPMessage message = messageFactory.createMessage(); SOAPEnvelope envelope = message.getSOAPPart().getEnvelope(); Name helloRequestName = envelope.createName("HelloRequest", PREFIX, NAMESPACE_URI); SOAPBodyElement helloRequestElement = message.getSOAPBody().addBodyElement(helloRequestName); helloRequestElement.setValue("Rondy.F"); return message; } public void callWebService() throws SOAPException, IOException { SOAPMessage request = createHelloRequest(); SOAPConnection connection = connectionFactory.createConnection(); SOAPMessage response = connection.call(request, url); if (!response.getSOAPBody().hasFault()) { writeHelloResponse(response); } else { SOAPFault fault = response.getSOAPBody().getFault(); System.err.println("Received SOAP Fault"); System.err.println("SOAP Fault Code :" + fault.getFaultCode()); System.err.println("SOAP Fault String :" + fault.getFaultString()); } } @SuppressWarnings("unchecked") private void writeHelloResponse(SOAPMessage message) throws SOAPException { SOAPEnvelope envelope = message.getSOAPPart().getEnvelope(); Name helloResponseName = envelope.createName("HelloResponse", PREFIX, NAMESPACE_URI); Iterator childElements = message.getSOAPBody().getChildElements(helloResponseName); SOAPBodyElement helloResponseElement = (SOAPBodyElement) childElements.next(); String value = helloResponseElement.getTextContent(); System.out.println("Hello Response [" + value + "]"); } public static void main(String[] args) throws Exception { String url = "http://localhost:8080/springws"; HelloWebServiceClient helloClient = new HelloWebServiceClient(url); helloClient.callWebService(); } }
几点看法:
1, 从上面代码可以看出, 比较麻烦的部分就是客户端和服务端对xml处理
相关文章推荐
- Spring Web Service 学习之Hello World篇
- Web Service学习-CXF与Spring整合为JavaEE应用发布WebService(三)
- Web Service学习-CXF与Spring整合为JavaEE应用发布WebService(三)
- SpringMVC学习笔记(三)使用IntelliJ IDEA开发Spring MVC HelloWorld(注解配置SpringMVC)
- CXF学习-与Spring整合(暴露web Service接口给其他程序使用)
- SpringBoot学习之hello world
- Spring Web Service 学习之Hello World篇
- Hello, Spring_Data_JPA! 学习笔记(一)
- Spring学习(二):Hello Spring你的第一个Spring应用
- Spring学习(七)——开发Web Service的几种方式
- 学习怎么用Spring写一个RESTful Web Service
- CXF学习-与Spring结合:使用其他程序提供的web service 接口
- Spring.NET学习笔记21——整合Web Service(应用篇) Level 200
- Web Service学习-CXF与Spring整合为JavaEE应用发布WebService(三)
- Apache CXF学习-创建基于spring的web service 推荐
- SpringMVC学习笔记(二)使用IntelliJ IDEA开发Spring MVC HelloWorld(基于Maven)
- Spring学习笔记-1:SpringMVC hello world
- spring boot学习总结(一)-- 基础入门 Hello,spring boot!
- (二).SpringMVC 学习-SpringMVC Hello World!
- (二).SpringMVC 学习-SpringMVC Hello World!