Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to realize Spring and CXF Integrated release WebService

2025-02-24 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >

Share

Shulou(Shulou.com)06/02 Report--

This article focuses on "how to achieve the integration of Spring and CXF release WebService", interested friends may wish to take a look. The method introduced in this paper is simple, fast and practical. Now let the editor take you to learn "how to achieve the integration of Spring and CXF release WebService"!

Here I have figured out two ways, one is to introduce dependencies, the other is to download the binary files of apache-cxf and configure them in eclipse, so as not to introduce dependencies. All the jar packages about CXF and jar packages related to Spring in the apache-cxf/lib directory can be downloaded and seen by yourself. If you need to introduce other jar packages, add dependencies in pom, and remember to configure environment variables after download. Let's start with the first one.

First, the server

Establish a Maven project and introduce dependencies

1. Dependence

1.1 the first way

Add dependencies directly to the pom file.

Org.apache.cxf

Cxf-core

3.1.4

Org.apache.cxf

Cxf-rt-transports-http

3.1.4

Org.apache.cxf

Cxf-rt-frontend-jaxws

3.1.4

CXF only needs to add the dependencies of these three jar packages, and Maven will be automatically introduced to help us introduce other jar packages. Save after adding in the pom file, it will trigger Maven to automatically download the dependent jar package you added (if you have one in the local warehouse directly from the local warehouse), and then there will be Maven Dependencies in the project directory, as shown in the following figure, after the addition is completed.

I only added three dependent jar packages to the pom file, and Maven automatically added so many other jar packages for me. Integrate with spring to release webservice and add spring's dependent jar package. I'll add this by myself, and I'll finish the first one.

1.2 the second way

Extract the downloaded binaries to a directory, and then configure the environment variables:

1. Variable name: CXF_HOME value: the decompression path of apache-cxf, for example: e:\ Install software\ apache-cxf-3.2.5

2. Add% CXF_HOME%/bin after path

Enter wsdl2java in the cmd command, and if you are prompted for usage, the configuration is successful.

Download address for apache-cxf binaries: http://cxf.apache.org/download.html

Download the one pointed to by the arrow:

Then set up the project, but you don't need to introduce dependencies, just configure it in eclipse, as shown in the figure:

Right-click on the project, Build Path / Configure Build Path / Add library / CXF Runtime, then select apache-cxf, click Finish, there will be an extra Apache CXF Library in your project directory, and the rest will be the same as below.

However, this method may report an error from time to time when it is running, but the service can still run normally. Here is the error message:

DefaultValidationEventHandler: [ERROR]: prefix wsdp is not bound to a namespace

Location: node: [wsd:Types: null] javax.xml.bind.UnmarshalException: prefix wsdp is not bound to a namespace- with linked exception:

[java.lang.IllegalArgumentException: prefix wsdp is not bound to a namespace]

At

The reason for searching for errors on the Internet is that there are extra jar packages in the lib directory. The solution is to delete the redundant jar packages.

Cxf-services-ws-discovery-api-3.1.4.jar

Services-ws-discovery-service-3.1.4.jar

Services-wsn-api-3.1.4.jar

Services-wsn-core-3.1.4.jar

Manifest.jar

Where the MANIFEST.MF file is in this directory.\ apache-cxf-3.2.5\ samples\ jax_rs\ minimal_osgi\ src\ main\ resources\ META-INF.

2. Test the code

Create an interface and remember to add the @ WebService annotation to indicate that you want to "expose" the interface (service class).

@ WebService

Public interface HelloService {

Public String sayHello (String name)

}

Implementation class:

/ / No @ Webservice annotations can be added to the implementation class

Public class HelloServiceImp implements HelloService {

@ Override

Public String sayHello (String name) {

Return "Hello, I am" + name

}

}

3. Configuration file

There are also two ways to configure it. Let's take a look at the first one.

3.1 the first configuration mode

Spring.xml

Spring-cxf.xml

Xsi:schemaLocation= "http://www.springframework.org/schema/beans

Http://www.springframework.org/schema/beans/spring-beans-3.0.xsd

Http://www.springframework.org/schema/context

Http://www.springframework.org/schema/context/spring-context-3.0.xsd

Http://cxf.apache.org/jaxws

Http://cxf.apache.org/schemas/jaxws.xsd"

3.2 the second configuration mode

Spring.xml

Spring-cxf.xml

Xsi:schemaLocation= "http://www.springframework.org/schema/beans

Http://www.springframework.org/schema/beans/spring-beans-3.0.xsd

Http://www.springframework.org/schema/context

Http://www.springframework.org/schema/context/spring-context-3.0.xsd

Http://cxf.apache.org/jaxws

Http://cxf.apache.org/schemas/jaxws.xsd"

Add:

1. As for the cxf.xml and cxf-servlet.xml that need to be introduced before, it is said on the Internet that cxf3.0 will not be needed in the future. As for why, the reason is here.

2, about another configuration mode in spring-cxf.xml (I did not try this way, interested partners can try it themselves).

The above configuration is finished, it is time to configure it in the web.xml file.

ContextConfigLocation

Classpath:spring.xml

Org.springframework.web.context.ContextLoaderListener

Cxf

Org.apache.cxf.transport.servlet.CXFServlet

one

Cxf

/ service/*

4. Run

After the configuration is completed, deploy to Tomcat and run it. Right-click run as/run on server/Finish,Eclipse to pop up the interface shown in the following figure to indicate that the service has been successfully published. If 404 pops up or an error is reported, the service has failed. Of course, you can also go to the browser to see the running effect, the address is: http://localhost:8080/ project name / service. Click on the place indicated by the arrow to see the wsdl file. (don't worry about my project name here.)

Second, generate the client

The first way is wsdl2java

The command provided by CXF to generate client-side code from wsdl.

Enter: wsdl2java-d in the cmd command to specify the code generation directory-the access address of the client webservice url or the local wsdl file directory address

Example: wsdl2java-d E:\\ AllWorkSpace\\ MyWork\\ TheClient\\ src-client http://localhost:8080/Dom4j_AxisDemo/service/hello?wsdl

Pay attention to the space in the middle!

The specific usage is Baidu. Here is only the explanation for the above usage:

-d specify the directory where the code is to be generated

-client generates code for client-side test web service

The code is generated as shown in the figure:

The second way is wsimport

The command to generate the client provided by JDK.

Enter: wsimport-s in the cmd command to specify the code generation directory-p package name-keep webservice access address url

Example: wsimport-s E:\\ AllWorkSpace\\ MyWork\\ TheClient\\ src-p com.eastcom.ws.client-keep http://localhost:8080/Dom4j_AxisDemo/service/hello?wsdl

Also pay attention to the space in the middle!

Directory address can not contain spaces, publish address do not forget? wsdl

III. Testing

Code, if you wsdl the file address directory you use when generating the client, you can't new it directly. In the constructor, you need to transfer the address of the service. You can check it in the server class generated by yourself.

Public class TestService {

Public static void main (String [] args) {

HelloService service=new HelloServiceImpService () .getHelloServiceImpPort ()

System.out.println (service.sayHello ("CXF"))

}

}

Result

At this point, I believe you have a deeper understanding of "how to achieve the integration and release of Spring and CXF WebService". You might as well do it in practice. Here is the website, more related content can enter the relevant channels to inquire, follow us, continue to learn!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Internet Technology

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report