What is webservice?webservice small sample point this understanding
Below to get to the chase:
Java Web project (Spring Project) integrates WebService to open interface steps:
Get ready:
Use CXF with good spring compatibility to achieve
CXF's jar:http://cxf.apache.org/download.html
Select the zip format to download, unzip the jar under the Lib directory
The minimum required jars 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 WebService server
1) Create a service interface
[Java]View PlainCopy
- Package com.service;
- Import Javax.jws.WebParam;
- Import Javax.jws.WebService;
- @WebService
- Public interface Ihelloworld {
- Public string SayHello (@WebParam (name = "arg0") string text);
- }
2) is the interface implementation class
[Java]View PlainCopy
- Package Com.service.impl;
- Import Javax.jws.WebService;
- Import Com.service.IHelloWorld;
- @WebService (Endpointinterface = "Com.service.IHelloWorld")
- Public class Helloworldimpl implements Ihelloworld {
- Public string SayHello (string text) {
- return "Hello:" + text;
- }
- }
3) Create a spring configuration file and add the service class to the container
Webservice.xml
[HTML]View PlainCopy
- <? 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:p="http://www.springframework.org/schema/p"
- xmlns:jaxws="Http://cxf.apache.org/jaxws"
- xmlns:cxf="Http://cxf.apache.org/core"
- xsi:schemalocation= "Http://www.springframework.org/schema/beans
- Http://www.springframework.org/schema/beans/spring-beans-2.5.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" />
- <!--The following class property value must be exactly the same as the package path for the service implementation class in your project-
- <Bean id="Hello" class="Com.service.impl.HelloWorldImpl"/>
- <jaxws:endpoint id="HelloWorld" implementor="#hello" address= "/ HelloWorld " />
- </Beans>
Add a Webservice.xml profile to Web. xml
[HTML]View PlainCopy
- <context-param>
- <param-name>contextconfiglocation</param-name>
- <param-value>
- /web-inf/webservice.xml
- </param-value>
- </context-param>
4) Add CXF servlet to Web. xml
[HTML]View PlainCopy
- <servlet>
- <display-name>cxf Servlet</display-name>
- <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>/webservice/*</url-pattern>
- </servlet-mapping>
At this point, the WebService server is created,
Access in the browser: HTTP://LOCALHOST:8080/TEST/WEBSERVICE/HELLOWORLD?WSDL, (test is the project name). If there is a similar
<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/"> ....
The configuration was successful.
Next paste several runtime error resolution methods
1:webservice.xml hint Cxf.xml,cxf-servlet.xml not found, I wrote on the path is classpath*:meta-inf/cxf/ Cxf.xml, here Classpath followed by a "*" symbol, no symbolic means to find only the classpath under the Cxf.xml file, the addition of a number means not only in the Classpath to find the XML file, but also in the jar package to find the XML file. So when we do not have a configuration file such as Cxf.xml in the project classpath, we must add * after classpath so that the spring container will go to the jar package that is added.
2: If CXF servlet, the mapping path should not be written as/*, otherwise you will not be able to access the project home page, can be written/webservice/* or other projects are not used in the path as a CXF servlet request path.
Two: Create WebService client
The client can be used for testing in the same project as the server, or a new Java project can be created for testing.
When you create a new Java project test, if the corresponding jar package, like the server, use spring also if the Spring jar package.
I'm building a new project here, still using spring to test
1) First to create a server-side service interface, (if the client and server side in the same project can omit this step)
[Java]View PlainCopy
- Package com.service;
- Import Javax.jws.WebParam;
- Import Javax.jws.WebService;
- @WebService
- Public interface Ihelloworld {
- Public string SayHello (@WebParam (name = "arg0") string text);
- }
2) Create Spring-client.xml,
[HTML]View PlainCopy
- <? 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:p="http// www.springframework.org/schema/p "
- xmlns:jaxws= "http://cxf.apache.org/jaxws" xmlns:cxf="Http://cxf.apache.org/core "
- xsi:schemalocation= "Http://www.springframework.org/schema/beans
- Http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
- Http://cxf.apache.org/jaxws
- Http://cxf.apache.org/schema/jaxws.xsd ">
- <Bean id="Client" class="Com.service.IHelloWorld" factory-bean="Clientfactory" factory-method="create" />
- <Bean id= "clientfactory" class="Org.apache.cxf.jaxws.JaxWsProxyFactoryBean" >
- <property name= "serviceclass" value="Com.service.IHelloWorld" />
- <property name="Address" value="Http://localhost:8080/test/HelloWorld" />
- </Bean>
- </Beans>
3) Test class
[Java]View PlainCopy
- Package com.test;
- Import Org.springframework.context.ApplicationContext;
- Import Org.springframework.context.support.ClassPathXmlApplicationContext;
- Import Com.service.IHelloWorld;
- Public class Test {
- public static void Main (string[] args) {
- ApplicationContext CTX = new Classpathxmlapplicationcontext ("Spring-client.xml");
- Ihelloworld client = (Ihelloworld) Ctx.getbean ("client");
- String result = Client.sayhello ("Hello!");
- SYSTEM.OUT.PRINTLN (result);
- }
- }
Display after successful run
Hello: Hello!
Article reference: http://www.open-open.com/lib/view/open1405929509210.html
Java Web project (Spring Project) integrates WebService for open interface