MAVEN+SPRINGMVC+CXF implementation Simple WebService small example

Source: Internet
Author: User

1. First you need to create a MAVEN project "Of course a Web project"

2.pom.xml Add the following

<properties>
<cxf.version>2.2.3</cxf.version>
</properties>

<dependency>
<groupId>org.apache.cxf</groupId>
<artifactId>cxf-rt-frontend-jaxws</artifactId>
<version>${cxf.version}</version>
</dependency>
<dependency>
<groupId>org.apache.cxf</groupId>
<artifactId>cxf-rt-transports-http</artifactId>
<version>${cxf.version}</version>
</dependency>

Yes, you just need to introduce these two, and then the other jars that cxf need are automatically added to the project.

3. Add in the project's Web. xml

<servlet>
<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>/cxf/*</url-pattern>
</servlet-mapping>

4. The specific code is implemented as follows:

4.1 Write an interface Ihelloservice.java

Package com.niuniu.service;

Import Javax.jws.WebService;

@WebService
Public interface Ihelloservice {
public string Wolaile (string userName);
}

4.2 Implementation of the Write interface Helloserviceimpl.java

Package Com.niuniu.service.impl;

Import Javax.jws.WebService;

Import org.springframework.stereotype.Component;
Import Com.niuniu.service.IHelloService;

@WebService (endpointinterface= "Com.niuniu.service.IHelloService", Servicename= "HelloService")
@Component
public class Helloserviceimpl implements Ihelloservice {

@Override
public string Wolaile (string userName) {

Return "Hello, Wolaile," + userName;
}

}

The 4.3 configuration files are as follows:

<?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:jaxws= "Http://cxf.apache.org/jaxws"
Xsi:schemalocation= "Http://www.springframework.org/schema/beans
Http://www.springframework.org/schema/beans/spring-beans-3.2.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-servlet.xml"/>

<jaxws:endpoint id= "HelloService" implementor= "Com.niuniu.service.impl.HelloServiceImpl" address= "/cxfservice" />
</beans>

The relevant configuration of spring is not described here.

"If you don't have a spring Web project, you can start by searching for a Web project that can run and then put the configuration in a running project."

Publish the project to Tomcat and then visit http://localhost:8080/ownweb/cxf/cxfService?wsdl to see if you can access it successfully.

"Ownweb is your project name, CXF is configured in Web. XML <url-pattern>,cxfservice is the value of address in 4.3, according to their own needs can be modified under the"

MAVEN+SPRINGMVC+CXF implementation Simple WebService small example

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.