spring+cxf+maven+rest风格+json/xml

spring+cxf+maven+rest风格+json/xml

Apache CXF = Celtix + XFire,开始叫 Apache CeltiXfire,后来更名为 Apache CXF 了,以下简称为 CXF。CXF 继承了 Celtix 和 XFire 两大开源项目的精华,提供了对 JAX-WS 全面的支持,并且提供了多种 Binding 、DataBinding、Transport 以及各种 Format 的支持,并且可以根据实际项目的需要,采用代码优先(Code First)或者 WSDL 优先(WSDL First)来轻松地实现 Web Services 的发布和使用。Apache CXF已经是一个正式的Apache顶级项目。

1.导入依赖
pom.xml

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0modelVersion>
    <groupId>com.ssm.rest.wsgroupId>
    <artifactId>ssm-rest-wsartifactId>
    <version>0.0.1-SNAPSHOTversion>
    <packaging>warpackaging>

    <properties>
        <spring.version>4.0.2.RELEASEspring.version>
        <jackson.version>1.9.2jackson.version>
        <cxf.version>3.0.3cxf.version>
    properties>

    <dependencies>
        <dependency>
            <groupId>junitgroupId>
            <artifactId>junitartifactId>
            <version>4.11version>
            <scope>testscope>
        dependency>
        
        <dependency>
            <groupId>org.springframeworkgroupId>
            <artifactId>spring-coreartifactId>
            <version>${spring.version}version>
        dependency>
        <dependency>
            <groupId>org.springframeworkgroupId>
            <artifactId>spring-beansartifactId>
            <version>${spring.version}version>
        dependency>
        <dependency>
            <groupId>org.springframeworkgroupId>
            <artifactId>spring-webartifactId>
            <version>${spring.version}version>
        dependency>
        <dependency>
            <groupId>org.springframeworkgroupId>
            <artifactId>spring-contextartifactId>
            <version>${spring.version}version>
        dependency>
        <dependency>
            <groupId>log4jgroupId>
            <artifactId>log4jartifactId>
            <version>1.2.17version>
        dependency>

        
        <dependency>
            <groupId>javax.servletgroupId>
            <artifactId>jstlartifactId>
            <version>1.2version>
        dependency>
        <dependency>
            <groupId>javax.servletgroupId>
            <artifactId>servlet-apiartifactId>
            <version>2.5version>
        dependency>
        <dependency>
            <groupId>org.apache.commonsgroupId>
            <artifactId>commons-lang3artifactId>
            <version>3.3.2version>
        dependency>

        
        <dependency>
            <groupId>org.apache.cxfgroupId>
            <artifactId>cxf-rt-frontend-jaxwsartifactId>
            <version>${cxf.version}version>
        dependency>
        <dependency>
            <groupId>org.apache.cxfgroupId>
            <artifactId>cxf-rt-transports-httpartifactId>
            <version>${cxf.version}version>
        dependency>
        <dependency>
            <groupId>org.apache.cxfgroupId>
            <artifactId>cxf-rt-frontend-jaxrsartifactId>
            <version>${cxf.version}version>
        dependency>

        
        <dependency>  
            <groupId>org.codehaus.jacksongroupId>  
            <artifactId>jackson-core-aslartifactId>  
            <version>${jackson.version}version>  
        dependency>  
        <dependency>  
            <groupId>org.codehaus.jacksongroupId>  
            <artifactId>jackson-mapper-aslartifactId>  
            <version>${jackson.version}version>  
        dependency> 
        <dependency>  
            <groupId>org.codehaus.jacksongroupId>  
            <artifactId>jackson-jaxrsartifactId>  
            <version>${jackson.version}version>  
        dependency>  
        <dependency>  
            <groupId>org.codehaus.jacksongroupId>  
            <artifactId>jackson-xcartifactId>  
            <version>${jackson.version}version>  
        dependency>
    dependencies>

    <build>
        <finalName>ssm-rest-wsfinalName>
        <plugins>
            
            <plugin>
                <groupId>org.apache.maven.pluginsgroupId>
                <artifactId>maven-resources-pluginartifactId>
                <version>2.7version>
                <configuration>
                    <encoding>UTF-8encoding>
                configuration>
            plugin>
            
            <plugin>
                <groupId>org.apache.maven.pluginsgroupId>
                <artifactId>maven-compiler-pluginartifactId>
                <version>3.2version>
                <configuration>
                    <source>1.7source>
                    <target>1.7target>
                    <encoding>UTF-8encoding>
                configuration>
            plugin>
        plugins>
    build>
project>

2.核心配置文件
spring-ws.xml


<beans xmlns="http://www.springframework.org/schema/beans"   
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"  
    xmlns:jaxws="http://cxf.apache.org/jaxws"
    xmlns:jaxrs="http://cxf.apache.org/jaxrs"
    xsi:schemaLocation="http://www.springframework.org/schema/beans   
    http://www.springframework.org/schema/beans/spring-beans-4.0.xsd   
    http://cxf.apache.org/jaxws http://cxf.apache.org/schemas/jaxws.xsd
    http://cxf.apache.org/jaxrs http://cxf.apache.org/schemas/jaxrs.xsd">  


    

    <jaxrs:server id="webService" address="/">
        
        <jaxrs:inInterceptors>

        jaxrs:inInterceptors>

        
        <jaxrs:outInterceptors>

        jaxrs:outInterceptors>

        
        <jaxrs:serviceBeans>
            <ref bean="personService"/>
        jaxrs:serviceBeans>

        
        <jaxrs:extensionMappings>
            <entry key="json" value="application/json"/>
            <entry key="xml" value="application/xml"/>
        jaxrs:extensionMappings>
        
        <jaxrs:languageMappings>

        jaxrs:languageMappings>
        
        <jaxrs:providers>
            <bean class="org.codehaus.jackson.jaxrs.JacksonJaxbJsonProvider"/>
        jaxrs:providers>

    jaxrs:server>
beans>

spring.xml


<beans xmlns="http://www.springframework.org/schema/beans"   
    xmlns:context="http://www.springframework.org/schema/context"  
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"  
    xsi:schemaLocation="http://www.springframework.org/schema/beans   
    http://www.springframework.org/schema/beans/spring-beans-4.0.xsd   
    http://www.springframework.org/schema/context   
    http://www.springframework.org/schema/context/spring-context-4.0.xsd ">  

    
    <context:component-scan base-package="com.ssm.rest.ws"/>

    
    <import resource="spring-ws.xml"/>
beans>

3.web.xml

  
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"  
    xmlns="http://java.sun.com/xml/ns/javaee"  
    xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd"  
    version="3.0">  
    <display-name>ssm-rest-wsdisplay-name>  

    
    <context-param>
        <param-name>contextConfigLocationparam-name>
        <param-value>classpath*:spring.xmlparam-value>
    context-param>
    

    
    <listener>
        <listener-class>org.springframework.web.context.ContextLoaderListenerlistener-class>
    listener>

    
    <servlet>
        <servlet-name>CXFServletservlet-name>
        <servlet-class>org.apache.cxf.transport.servlet.CXFServletservlet-class>
    servlet>
    <servlet-mapping>
        <servlet-name>CXFServletservlet-name>
        <url-pattern>/rest/ws/*url-pattern>
    servlet-mapping>

web-app>

4.编写代码案例
1.定义实体

package com.ssm.rest.ws.entity;

import java.io.Serializable;

import javax.xml.bind.annotation.XmlRootElement;

/**
 * @autho 董杨炀
 * @time 2017年5月17日 上午10:28:09
*/
@XmlRootElement
public class PersonEntity implements Serializable{

    private static final long serialVersionUID = 1L;

    private Integer id;
    private String name;
    private Integer age;
    private String desc;
    private Boolean status;

    public PersonEntity() {

    }

    public PersonEntity(Integer id, String name, Integer age, String desc,Boolean status) {
        this.id = id;
        this.name = name;
        this.age = age;
        this.desc = desc;
        this.status = status;
    }

    public Integer getId() {
        return id;
    }

    public void setId(Integer id) {
        this.id = id;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public Integer getAge() {
        return age;
    }

    public void setAge(Integer age) {
        this.age = age;
    }

    public String getDesc() {
        return desc;
    }

    public void setDesc(String desc) {
        this.desc = desc;
    }

    public Boolean getStatus() {
        return status;
    }

    public void setStatus(Boolean status) {
        this.status = status;
    }

    @Override
    public String toString() {
        return "PersonEntity [id=" + id + ", name=" + name + ", age=" + age + ", desc=" + desc + ", status=" + status
                + "]";
    }



}
package com.ssm.rest.ws.entity;

import java.io.Serializable;
import java.util.List;

import javax.xml.bind.annotation.XmlRootElement;

/**
 * @autho 董杨炀
 * @time 2017年5月17日 上午10:30:19
*/
@XmlRootElement
public class PersonEntityList implements Serializable{

    private static final long serialVersionUID = 1L;

    private List personList;

    public PersonEntityList(List personList) {
        super();
        this.personList = personList;
    }

    public PersonEntityList() {
        super();
    }

    public List getPersonList() {
        return personList;
    }


    public void setPersonList(List personList) {
        this.personList = personList;
    }

    @Override
    public String toString() {
        return "PersonEntityList [personList=" + personList + "]";
    }

}

2.编写接口和实现

package com.ssm.rest.ws.service;

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;
import com.ssm.rest.ws.entity.PersonEntity;
import com.ssm.rest.ws.entity.PersonEntityList;

/**
 * @autho 董杨炀
 * @time 2017年5月17日 上午10:33:18
*/
@Path("/person")
@Produces(MediaType.APPLICATION_JSON)
public interface PersonService {

    @GET
    @Path("/getStatus")
    public String getStatus();

    @GET
    @Path("/getPerson/{index}")
    public PersonEntity getPersonEntityById(@PathParam("index") Integer id);

    @POST
    @Path("/getPersonList")
    public PersonEntityList getPersonList();
}
package com.ssm.rest.ws.service.impl;

import java.util.List;
import org.springframework.stereotype.Component;
import com.ssm.rest.ws.DataSource;
import com.ssm.rest.ws.entity.PersonEntity;
import com.ssm.rest.ws.entity.PersonEntityList;
import com.ssm.rest.ws.service.PersonService;

/**
 * @autho 董杨炀
 * @time 2017年5月17日 上午10:36:56
*/
@Component("personService")
public class PersonServiceImpl implements PersonService{

    /**
     * http://localhost:8080/ssm-rest-ws/rest/ws/person/getStatus
     * @autho 董杨炀
     * @time 2017年5月17日 上午10:37:08
     * @return
     */
    @Override
    public String getStatus() {
        return "getStatus";
    }

    /**
     * http://localhost:8080/ssm-rest-ws/rest/ws/person/getPerson/1
     * @autho 董杨炀
     * @time 2017年5月17日 上午10:37:08
     * @param id
     * @return
     */
    @Override
    public PersonEntity getPersonEntityById(Integer id) {
        List list = DataSource.getList();
        PersonEntity vo = null;
        if (list.size() > id) {
            vo = list.get(id - 1);
        }
        return vo;
    }

    /**
     * http://localhost:8080/ssm-rest-ws/rest/ws/person/getPersonList
     * @autho 董杨炀
     * @time 2017年5月17日 上午10:37:08
     * @return
     */
    @Override
    public PersonEntityList getPersonList() {
        List list = DataSource.getList();
        PersonEntityList listVo = new PersonEntityList(list);
        return listVo;
    }

}

3.这里不连数据库,需要模拟一个数据源

package com.ssm.rest.ws;

import java.util.ArrayList;
import java.util.List;

import com.ssm.rest.ws.entity.PersonEntity;

/**
 * 模拟数据源
 * 方法静态,不需要new对象调用
 * @autho 董杨炀
 * @time 2017年5月17日 上午10:39:05
*/
public class DataSource {

    private static List list;

    static {
        DataSource.list = new ArrayList();

        for (int i = 0; i < 10; i++) {
            PersonEntity vo = new PersonEntity(i, "name" + i, Math.round(100.0f),
                    "desc" + i, (i & 1) == 0);
            DataSource.list.add(vo);
        }
    }

    public static List getList() {
        return DataSource.list;
    }

}

5.启动项目进行测试
spring+cxf+maven+rest风格+json/xml_第1张图片
spring+cxf+maven+rest风格+json/xml_第2张图片
spring+cxf+maven+rest风格+json/xml_第3张图片

项目地址:https://github.com/15000814726/ssm-rest-ws.git

你可能感兴趣的:(javaEE)