Integrated web service in java web project (spring project) to implement open interface

Keywords: xml Spring Apache Java

What is a WebService? A small example of a webService Learn more about this

Enter the topic below:

Java  web project ( spring Integrated webservice in project To open the interface to the outside world:

Get ready:

Using cxf with good compatibility with spring

The jar download address for cxf: http://cxf.apache.org/download.html

Choose zip format to download, jar in lib directory after decompression

The minimum jar s required are as follows:

cxf-2.3.3.jar
geronimo-annotation_1.0_spec-1.1.1.jar
geronimo-jaxws_2.2_spec-1.0.jar
geronimo-stax-api_1.0_spec-1.0.1.jar
geronimo-ws-metadata_2.0_spec-1.1.3.jar
jaxb-api-2.2.1.jar
jaxb-impl-2.2.1.1.jar
neethi-2.0.4.jar
wsdl4j-1.6.2.jar
XmlSchema-1.4.7.jar
wstx-asl-3.2.9.jar

One: Create a web service server

1) Create a service interface

  1. package com.service;  
  2.   
  3. import javax.jws.WebParam;  
  4. import javax.jws.WebService;  
  5.   
  6. @WebService  
  7. public interface IHelloWorld {  
  8.     public String sayHello(@WebParam(name = "arg0") String text);  
  9. }  

2) Is an interface implementation class

  1. package com.service.impl;  
  2.   
  3. import javax.jws.WebService;  
  4.   
  5. import com.service.IHelloWorld;  
  6.   
  7. @WebService(endpointInterface = "com.service.IHelloWorld")  
  8. public class HelloWorldImpl implements IHelloWorld {  
  9.     public String sayHello(String text) {  
  10.         return "Hello : " + text;  
  11.     }  
  12. }  

3) Create a spring configuration file to add service classes to the container

webservice.xml

  1. <?xml version="1.0" encoding="UTF-8"?>  
  2. <beans xmlns="http://www.springframework.org/schema/beans"  
  3.     xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"   
  4.     xmlns:p="http://www.springframework.org/schema/p"  
  5.     xmlns:jaxws="http://cxf.apache.org/jaxws"  
  6.     xmlns:cxf="http://cxf.apache.org/core"  
  7.     xsi:schemaLocation="http://www.springframework.org/schema/beans  
  8.     http://www.springframework.org/schema/beans/spring-beans-2.5.xsd  
  9.     http://cxf.apache.org/jaxws  
  10.     http://cxf.apache.org/schemas/jaxws.xsd">  
  11.   
  12.     <import resource="classpath*:META-INF/cxf/cxf.xml" />  
  13.     <import resource="classpath*:META-INF/cxf/cxf-extension-soap.xml" />  
  14.     <import resource="classpath*:META-INF/cxf/cxf-servlet.xml" />  
  15.   
  16.     <!--Below class Property values must exactly match the package path of the service implementation class in your project-->  
  17.     <bean id="hello" class="com.service.impl.HelloWorldImpl"/>  
  18.     <jaxws:endpoint id="helloWorld" implementor="#hello" address="/HelloWorld" />  
  19. </beans>  

Add a webservice.xml configuration file to the web.xml

  1. <context-param>  
  2.     <param-name>contextConfigLocation</param-name>  
  3.     <param-value>  
  4.         /WEB-INF/webservice.xml       
  5.     </param-value>  
  6. </context-param>  

4) Add cxf servlet to web.xml

  1. <servlet>  
  2.     <display-name>CXF Servlet</display-name>  
  3.     <servlet-name>CXFServlet</servlet-name>  
  4.     <servlet-class>org.apache.cxf.transport.servlet.CXFServlet</servlet-class>  
  5.     <load-on-startup>1</load-on-startup>  
  6. </servlet>  
  7.   
  8. <servlet-mapping>  
  9.     <servlet-name>CXFServlet</servlet-name>  
  10.     <url-pattern>/webservice/*</url-pattern>  
  11. </servlet-mapping>  

The webservice server is now created.

Access: http://localhost:8080/test/webservice/HelloWorld?wsdl in a browser (test is the project name).If something similar to

<wsdl:definitions xmlns:ns1="http://service.com/" xmlns:soap="http://schemas.xmlsoap.org/wsdl/soap/" xmlns:tns="http://impl.service.com/" xmlns:wsdl="http://schemas.xmlsoap.org/wsdl/" xmlns:xsd="http://www.w3.org/2001/XMLSchema" name="HelloWorldImplService" targetNamespace="http://impl.service.com/">
. . . . 

Configuration was successful.

Following are a few runtime error resolution tips

1:webservice.xml prompts cxf.xml, cxf-servlet.xml is not found, the path I wrote above is classpath*:META-INF/cxf/cxf.xml, where classpath is followed by a'*'symbol, unsigned means that only the cxf.xml file is found under the class path, plus sign means that not only the XML file is found under the class path, but also under the class pathFind the XML file in the jar package.So if we don't have a configuration file such as cxf.xml under the project class path, we have to append * to the classpath so that the spring container will find it in the jar package we've added.

2: If you do not write a mapping path as /* when using a cxf servlet, otherwise you will not be able to access the project home page, you can write as a request path for a cxf servlet as / webservice/* or a path that has not been used in other projects.

2: Create a web service client

Clients can be used in the same project as servers test Or create a new one Java Project for testing.

When creating a new Java project test, assume that the corresponding jar package, like the server, uses spring and assumes the spring jar package.

I'm creating a new project here and still using spring to test

1) First, create a service interface that is the same as the server side. (You can omit this step if the client and server sides are in the same project.)

  1. package com.service;  
  2.   
  3. import javax.jws.WebParam;  
  4. import javax.jws.WebService;  
  5.   
  6. @WebService  
  7. public interface IHelloWorld {  
  8.     public String sayHello(@WebParam(name = "arg0") String text);  
  9. }  

2) Create spring-client.xml,

  1. <?xml version="1.0" encoding="UTF-8"?>  
  2. <beans xmlns="http://www.springframework.org/schema/beans"  
  3.     xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:p="http://www.springframework.org/schema/p"  
  4.     xmlns:jaxws="http://cxf.apache.org/jaxws" xmlns:cxf="http://cxf.apache.org/core"  
  5.     xsi:schemaLocation="http://www.springframework.org/schema/beans   
  6.     http://www.springframework.org/schema/beans/spring-beans-2.5.xsd  
  7.     http://cxf.apache.org/jaxws   
  8.     http://cxf.apache.org/schema/jaxws.xsd">  
  9.   
  10.     <bean id="client" class="com.service.IHelloWorld" factory-bean="clientFactory" factory-method="create" />  
  11.     <bean id="clientFactory" class="org.apache.cxf.jaxws.JaxWsProxyFactoryBean">  
  12.         <property name="serviceClass" value="com.service.IHelloWorld" />  
  13.         <property name="address" value="http://localhost:8080/test/HelloWorld" />  
  14.     </bean>  
  15. </beans>  

3) Test Class

[java] view plain copy
  1. package com.test;  
  2.   
  3. import org.springframework.context.ApplicationContext;  
  4. import org.springframework.context.support.ClassPathXmlApplicationContext;  
  5.   
  6. import com.service.IHelloWorld;  
  7.   
  8. public class Test {  
  9.     public static void main(String[] args) {  
  10.         ApplicationContext ctx = new ClassPathXmlApplicationContext("spring-client.xml");  
  11.         IHelloWorld client = (IHelloWorld) ctx.getBean("client");  
  12. String result = client.sayHello; (Hello!)
  13.         System.out.println(result);  
  14.     }  
  15. }  

Show after successful run

Hello: Hello!
Other and webservice Related Links
5 Minute use Spring4 Build one REST WebService
	http://www.cnblogs.com/tzyy/p/4837701.html
	
Java WebService Simple small example
	http://blog.csdn.net/mixika99/article/details/51647972


MyEclipse10.7.1+JDK 1.6.0_22 Create [ Web Service Project]
	http://www.iteye.com/topic/1135747
	
Webservice Working principle and examples
	http://blog.csdn.net/yangwenxue_admin/article/details/51059125
	
java Realization WebService And different ways of calling
	http://www.cnblogs.com/siqi/archive/2013/12/15/3475222.html
	
Java call WebService The interface implements the function of sending SMS verification codes. java Mobile phone verification code, WebService Interface Call	
	http://blog.csdn.net/sxdtzhaoxinguo/article/details/34437591
	
W3School Web Services Course
	http://www.w3school.com.cn/webservices/
From: http://blog.csdn.net/mixika99/article/details/54407667

Posted by legomez2000 on Sun, 30 Jun 2019 10:10:56 -0700