Restful cxf spring integration 1 (release of the WSDL Interface)

Source: Internet
Author: User

I have been writing this article for a long time. I will write it if I have nothing to do today!

There were not many contacts with cxf before, so I didn't understand it very well. During this time, the project needs to be studied.

The first step is to understand the restful interface. You can also publish the WSDL interface. Previously, the rest interface only published a service, and its interface service method is described in the usage documentation, however, the rest interface released later is a wsdl interface.

The project requires support from spring core, Spring AOP, cxf, and other related packages.

The demo project target is used as a user add (post) and query (get) function.

There are three files in total:

  1. Users. Java
  2. Beans. xml
  3. Web. xml

----------------- Source code split line -----------------------------------

Java users class:

package com.ly.cxf.demo;import java.util.ArrayList;import java.util.List;import javax.ws.rs.GET;import javax.ws.rs.POST;import javax.ws.rs.Path;import javax.ws.rs.PathParam;import javax.ws.rs.Produces;import javax.ws.rs.core.MediaType;// @WebService(targetNamespace="http://com.ly.cxf.demo")@Path("/getInfo")public class Users {static List<String> users = null;{if (users == null) {users=new ArrayList<String>();users.add("yinlei");}}@GET@Produces( { MediaType.APPLICATION_XML })// http://localhost:8080/sprcxf/api/getInfopublic String getUserInfo() {String ret = "<?xml version=\"1.0\" encoding=\"UTF-8\"?><users>";for (String u : users) {ret = ret + "<user><name>" + u + "</name></user>";}ret =ret+"</users>";return ret;}@POST@Produces( { MediaType.APPLICATION_XML })@Path("/{user}")//http://localhost:8080/sprcxf/api/getInfo/testAnyUserNamepublic String postUserInfo(@PathParam("user") String user) {users.add(user);return "<?xml version=\"1.0\" encoding=\"UTF-8\"?><retMsg>OK</retMsg>";}}

Beans. xml file:

<?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/beanshttp://www.springframework.org/schema/beans/spring-beans.xsdhttp://cxf.apache.org/jaxwshttp://cxf.apache.org/schemas/jaxws.xsdhttp://cxf.apache.org/jaxrs    http://cxf.apache.org/schemas/jaxrs.xsd"xmlns:jaxrs="http://cxf.apache.org/jaxrs">  <import resource="classpath:META-INF/cxf/cxf.xml"/>  <!--jetty <import resource="classpath:META-INF/cxf/cxf-extension-http-binding.xml"/> -->  <import resource="classpath:META-INF/cxf/cxf-servlet.xml"/>    <bean id="restSample" class="com.ly.cxf.demo.Users"/>   <jaxrs:server id="restServiceContainer" address="/api">    <jaxrs:serviceBeans>        <ref bean="restSample" />    </jaxrs:serviceBeans></jaxrs:server>  </beans>

Web. xml file:

<?xml version="1.0" encoding="UTF-8"?><web-app version="2.5" xmlns="http://java.sun.com/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"><context-param><param-name>contextConfigLocation</param-name><param-value>WEB-INF/beans.xml</param-value></context-param><listener><listener-class>org.springframework.web.context.ContextLoaderListener</listener-class></listener><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>/*</url-pattern></servlet-mapping>  <welcome-file-list>    <welcome-file>index.jsp</welcome-file>  </welcome-file-list></web-app>

Dependent package:

  • Com.springsource.net. SF. cglib-2.2.0.jar
  • Com.springsource.org. aopalliance-1.0.0.jar
  • Com.springsource.org. aspectj. weaver-1.6.8.RELEASE.jar
  • Commons-logging-1.1.1.jar
  • Cxf-2.6.0.jar
  • Geronimo-activation_1.1_spec-1.1.jar
  • Geronimo-annotation_1.0_spec-1.1.1.jar
  • Geronimo-servlet_3.0_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.6.jar
  • Jaxb-impl-2.2.5.jar
  • Jsr311-api-1.1.1.jar
  • Neethi-3.0.2.jar
  • Org. springframework. aop-3.0.1.RELEASE-A.jar
  • Org. springframework. ASM. Jar
  • Org. springframework. aspects-3.0.1.RELEASE-A.jar
  • Org. springframework. Beans. Jar
  • Org. springframework. Context. Jar
  • Org. springframework. Core. Jar
  • Org. springframework. expression. Jar
  • Org. springframework. instrument-3.0.1.RELEASE-A.jar
  • Org. springframework. instrument. tomcat-3.0.1.RELEASE-A.jar
  • Org. springframework. Web. Jar
  • Org. springframework. webmvc. Jar
  • Stax2-api-3.1.1.jar
  • Woodstox-core-asl-4.1.2.jar
  • Wsdl4j-1.6.2.jar
  • Xmlschema-core-2.0.2.jar

------------------------ Source code end split line ----------------------------

Enter http: // localhost: 8080/sprcxf/

Click the link shown in the figure to go To the WSDL interface description page:

Usage:

Get http: /localhost: 8080/sprcxf/API/getinfo

Post http: // localhost: 8080/sprcxf/API/getinfo/anyusername

Conclusion: the benefit of using this rest interface is that it automatically provides the WSDL interface description file. It is easy to understand.

Click to download source code

Download source code:

Http://yunpan.cn/lk/5643d5xzvu

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.