Java Web project (Spring Project) integrates WebService for open interface

Source: Internet
Author: User
Tags soap wsdl

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
    1. Package com.service;
    2. Import Javax.jws.WebParam;
    3. Import Javax.jws.WebService;
    4. @WebService
    5. Public interface Ihelloworld {
    6. Public string SayHello (@WebParam (name = "arg0") string text);
    7. }

2) is the interface implementation class

[Java]View PlainCopy
  1. Package Com.service.impl;
  2. Import Javax.jws.WebService;
  3. Import Com.service.IHelloWorld;
  4. @WebService (Endpointinterface = "Com.service.IHelloWorld")
  5. Public class Helloworldimpl implements Ihelloworld {
  6. Public string SayHello (string text) {
  7. return "Hello:" + text;
  8. }
  9. }


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

Webservice.xml

[HTML]View PlainCopy
  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. <import resource="Classpath*:meta-inf/cxf/cxf.xml" />
  12. <import resource="Classpath*:meta-inf/cxf/cxf-extension-soap.xml" />
  13. <import resource="Classpath*:meta-inf/cxf/cxf-servlet.xml" />
  14. <!--The following class property value must be exactly the same as the package path for the service implementation class in your project-
  15. <Bean id="Hello" class="Com.service.impl.HelloWorldImpl"/>
  16. <jaxws:endpoint id="HelloWorld" implementor="#hello" address= "/ HelloWorld " />
  17. </Beans>

Add a Webservice.xml profile to Web. xml

[HTML]View PlainCopy
  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

[HTML]View PlainCopy
  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. <servlet-mapping>
  8. <servlet-name>cxfservlet</servlet-name>
  9. <url-pattern>/webservice/*</url-pattern>
  10. </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
    1. Package com.service;
    2. Import Javax.jws.WebParam;
    3. Import Javax.jws.WebService;
    4. @WebService
    5. Public interface Ihelloworld {
    6. Public string SayHello (@WebParam (name = "arg0") string text);
    7. }

2) Create Spring-client.xml,

[HTML]View PlainCopy
  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. <Bean id="Client" class="Com.service.IHelloWorld" factory-bean="Clientfactory" factory-method="create" />
  10. <Bean id= "clientfactory" class="Org.apache.cxf.jaxws.JaxWsProxyFactoryBean" >
  11. <property name= "serviceclass" value="Com.service.IHelloWorld" />
  12. <property name="Address" value="Http://localhost:8080/test/HelloWorld" />
  13. </Bean>
  14. </Beans>

3) Test class

[Java]View PlainCopy
  1. Package com.test;
  2. Import Org.springframework.context.ApplicationContext;
  3. Import Org.springframework.context.support.ClassPathXmlApplicationContext;
  4. Import Com.service.IHelloWorld;
  5. Public class Test {
  6. public static void Main (string[] args) {
  7. ApplicationContext CTX = new Classpathxmlapplicationcontext ("Spring-client.xml");
  8. Ihelloworld client = (Ihelloworld) Ctx.getbean ("client");
  9. String result = Client.sayhello ("Hello!");
  10. SYSTEM.OUT.PRINTLN (result);
  11. }
  12. }

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.