spring+cxf以jax-rs规范实现webservice(restful风格)

一.服务端

新建项目:spring-jax-rs-server

spring+cxf以jax-rs规范实现webservice(restful风格)_第1张图片

1.pom文件:




    
        web-service-demo
        com.meboth.bonc.ljf
        1.0-SNAPSHOT
    
    4.0.0

    com.meboth.bonc.ljf.spring.jax.rs.server
    spring-jax-rs-server
    1.0-SNAPSHOT
    war

    spring-jax-rs-server Maven Webapp
    
    http://www.example.com

    
        UTF-8
        1.7
        1.7
    

    
        
        
            org.apache.cxf
            cxf-rt-frontend-jaxrs
            3.0.1
        
        
        
            org.slf4j
            slf4j-log4j12
            1.7.12
        

        
        
            org.apache.cxf
            cxf-rt-rs-client
            3.0.1
        

        
        
            org.apache.cxf
            cxf-rt-rs-extension-providers
            3.0.1
        

        
        
            org.codehaus.jettison
            jettison
            1.3.7
        

        
        
            org.springframework
            spring-context
            4.2.4.RELEASE
        
        
        
            org.springframework
            spring-web
            4.2.4.RELEASE
        
        
        
            org.springframework
            spring-test
            4.2.4.RELEASE
        
        
        
            junit
            junit
            4.12
        
    

    
        spring-jax-rs-server
        
            
                
                
                    org.apache.maven.plugins
                    maven-compiler-plugin
                    3.2
                    
                        1.8
                        1.8
                        UTF-8
                        true
                    
                
                
                
                    org.apache.tomcat.maven
                    tomcat7-maven-plugin
                    2.2
                    
                        
                        8080
                        
                        /
                    
                
            
        
    

2.实体类

package com.ljf.spring.jax.rs.server.model;

import javax.xml.bind.annotation.XmlRootElement;
import java.util.ArrayList;
import java.util.List;

@XmlRootElement(name = "myBeiJingUser")
public class BeiJingUser {
    private int id;
    private String name;
    private  String address;
    private List mobileList=new ArrayList();

    public BeiJingUser() {
    }

    public BeiJingUser(int id, String name, String address, List mobileList) {
        this.id = id;
        this.name = name;
        this.address = address;
        this.mobileList = mobileList;
    }

    public BeiJingUser(int id, String name, String address) {
        this.id = id;
        this.name = name;
        this.address = address;
    }

    public int getId() {
        return id;
    }

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

    public String getName() {
        return name;
    }

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

    public String getAddress() {
        return address;
    }

    public void setAddress(String address) {
        this.address = address;
    }

    public List getMobileList() {
        return mobileList;
    }

    public void setMobileList(List mobileList) {
        this.mobileList = mobileList;
    }
}




package com.ljf.spring.jax.rs.server.model;

import javax.xml.bind.annotation.XmlRootElement;

@XmlRootElement(name = "myCellPhone")
public class Mobile {
    private int id;
    private String name;

    public Mobile() {
    }

    public Mobile(int id, String name) {
        this.id = id;
        this.name = name;
    }

    public int getId() {
        return id;
    }

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

    public String getName() {
        return name;
    }

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



 

3.接口和实现类:

 

package com.ljf.spring.jax.rs.server.service;

import com.ljf.spring.jax.rs.server.model.BeiJingUser;

import javax.ws.rs.*;
import java.util.List;
@Path("/bjService")  // 路径;访问当前服务接口时候的路径。
@Produces("*/*")
public interface UserService {
    @POST
    @Path("/bjUser")	  // 路径; 访问当前服务接口的方法路径
    // @Consumes 服务端支持的请求的数据格式(xml、json)
    @Consumes({ "application/xml", "application/json" })
    public void saveUser(BeiJingUser user);

    @PUT
    @Path("/bjUser")
    @Consumes({ "application/xml", "application/json" })
    public void updateUser(BeiJingUser user);

    @GET
    @Path("/bjUser")
    // @Produces 服务端支持的响应的数据格式
    @Produces({ "application/xml", "application/json" })
    public List findAllUsers();

    @GET
    @Path("/bjUser/{id}")
    @Consumes("application/xml")
    @Produces({ "application/xml", "application/json" })
    public BeiJingUser finUserById(@PathParam("id") Integer id);

    @DELETE
    @Path("/bjUser/{id}")
    @Consumes({"application/xml", "application/json"})
    public void deleteUser(@PathParam("id") Integer id);
}







package com.ljf.spring.jax.rs.server.service;

import com.ljf.spring.jax.rs.server.model.Mobile;
import com.ljf.spring.jax.rs.server.model.BeiJingUser;

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

public class UserServiceImpl implements UserService{
    public void saveUser(BeiJingUser user) {
        System.out.println("save user:" + user.getName());
    }

    public void updateUser(BeiJingUser user) {
        System.out.println("update user:" + user);
    }

    public List findAllUsers() {
        List users = new ArrayList();

        // 汽车
        List carList1 = new ArrayList();
        Mobile car1 = new Mobile(101,"apple");
        Mobile car2 = new Mobile(102,"huawei");
        carList1.add(car1);
        carList1.add(car2);

        // 用户
        BeiJingUser user1 = new BeiJingUser(1,"小明","海淀",carList1);
        BeiJingUser user2 = new BeiJingUser(2,"小丽","朝阳",carList1);

        // 用户集合
        users.add(user1);
        users.add(user2);

        return users;
    }

    public BeiJingUser finUserById(Integer id) {
        if (id == 1) {
            Mobile bike=   new Mobile(101,"xiaomi");
            List bList=   new ArrayList();
            bList.add(bike);
            return new BeiJingUser(1,"小明","大兴",bList);
        }
        return null;
    }

    public void deleteUser(Integer id) {
        System.out.println("delete user id :" + id);
    }
}

4.资源类:

applicationContext类:




    
    
        
            
        
        
            
        
        
            
        
    

log4j类:

log4j.rootLogger=INFO,console,FILE
log4j.appender.console=org.apache.log4j.ConsoleAppender
log4j.appender.console.threshold=INFO
log4j.appender.console.layout=org.apache.log4j.PatternLayout
log4j.appender.console.layout.ConversionPattern=%d{yyyy-MM-dd HH:mm:ss}[%5p]-%c(%L)-%m%n
log4j.appender.FILE=org.apache.log4j.RollingFileAppender
log4j.appender.FILE.Append=true
log4j.appender.FILE.File=./web-service.log
log4j.appender.FILE.Threshold=INFO
log4j.appender.FILE.layout=org.apache.log4j.PatternLayout
log4j.appender.FILE.layout.ConversionPattern=%d{yyyy-MM-ddHH:mm:ss}[%5p]-%c(%L)-%m%n
log4j.appender.FILE.MaxFileSize=30MB

5.web.xml:




  Archetype Created Web Application
  
    cxfservlet
    org.apache.cxf.transport.servlet.CXFServlet
    1
  
  
    cxfservlet
    /ws/*
  

  
  
    contextConfigLocation
    classpath:applicationContext.xml
  
  
    org.springframework.web.context.ContextLoaderListener
  

6.发布服务:

spring+cxf以jax-rs规范实现webservice(restful风格)_第2张图片

 

 

spring+cxf以jax-rs规范实现webservice(restful风格)_第3张图片

 

配置后,启动就ok了!

spring+cxf以jax-rs规范实现webservice(restful风格)_第4张图片

二.客户端

新建项目:spring-jax-rs-client

spring+cxf以jax-rs规范实现webservice(restful风格)_第5张图片

1.pom文件:




    
        web-service-demo
        com.meboth.bonc.ljf
        1.0-SNAPSHOT
    
    4.0.0

    com.meboth.bonc.ljf.spring.jax.rs.client
    spring-jax-rs-client
    1.0-SNAPSHOT
    war

    spring-jax-rs-client Maven Webapp
    
    http://www.example.com

    
        UTF-8
        1.7
        1.7
    

    
        
        
            org.apache.cxf
            cxf-rt-frontend-jaxrs
            3.0.1
        
        
        
            org.slf4j
            slf4j-log4j12
            1.7.12
        

        
        
            org.apache.cxf
            cxf-rt-rs-client
            3.0.1
        

        
        
            org.apache.cxf
            cxf-rt-rs-extension-providers
            3.0.1
        

        
        
            org.codehaus.jettison
            jettison
            1.3.7
        

        
        
            org.springframework
            spring-context
            4.2.4.RELEASE
        
        
        
            org.springframework
            spring-web
            4.2.4.RELEASE
        
        
        
            org.springframework
            spring-test
            4.2.4.RELEASE
        
        
        
            junit
            junit
            4.12
        
    

    
        spring-jax-rs-client
        
            
            
            
                org.apache.maven.plugins
                maven-compiler-plugin
                3.2
                
                    1.8
                    1.8
                    UTF-8
                    true
                
            
            
            
                org.apache.tomcat.maven
                tomcat7-maven-plugin
                2.2
                
                    
                    8080
                    
                    /
                
            
        
        
    

2.实体类:

package com.ljf.spring.jax.rs.client.model;

import javax.xml.bind.annotation.XmlRootElement;
import java.util.ArrayList;
import java.util.List;

@XmlRootElement(name = "myBeiJingUser")
public class BeiJingUser {
    private int id;
    private String name;
    private  String address;
    private List mobileList=new ArrayList();

    public BeiJingUser() {
    }

    public BeiJingUser(int id, String name, String address, List mobileList) {
        this.id = id;
        this.name = name;
        this.address = address;
        this.mobileList = mobileList;
    }

    public BeiJingUser(int id, String name, String address) {
        this.id = id;
        this.name = name;
        this.address = address;
    }

    public int getId() {
        return id;
    }

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

    public String getName() {
        return name;
    }

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

    public String getAddress() {
        return address;
    }

    public void setAddress(String address) {
        this.address = address;
    }

    public List getMobileList() {
        return mobileList;
    }

    public void setMobileList(List mobileList) {
        this.mobileList = mobileList;
    }
}





package com.ljf.spring.jax.rs.client.model;

import javax.xml.bind.annotation.XmlRootElement;

@XmlRootElement(name = "myCellPhone")
public class Mobile {
    private int id;
    private String name;

    public Mobile() {
    }

    public Mobile(int id, String name) {
        this.id = id;
        this.name = name;
    }

    public int getId() {
        return id;
    }

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

    public String getName() {
        return name;
    }

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

3.资源类:

log4j.rootLogger=INFO,console,FILE
log4j.appender.console=org.apache.log4j.ConsoleAppender
log4j.appender.console.threshold=INFO
log4j.appender.console.layout=org.apache.log4j.PatternLayout
log4j.appender.console.layout.ConversionPattern=%d{yyyy-MM-dd HH:mm:ss}[%5p]-%c(%L)-%m%n
log4j.appender.FILE=org.apache.log4j.RollingFileAppender
log4j.appender.FILE.Append=true
log4j.appender.FILE.File=./web-service.log
log4j.appender.FILE.Threshold=INFO
log4j.appender.FILE.layout=org.apache.log4j.PatternLayout
log4j.appender.FILE.layout.ConversionPattern=%d{yyyy-MM-ddHH:mm:ss}[%5p]-%c(%L)-%m%n
log4j.appender.FILE.MaxFileSize=30MB

4.调用类:

http://localhost:8080/ws/mlgbUser/bjService/bjUser  注意路径又多了一层
package com.ljf.spring.jax.rs.client;

import com.ljf.spring.jax.rs.client.model.BeiJingUser;
import org.apache.cxf.jaxrs.client.WebClient;

import javax.ws.rs.core.MediaType;
import java.util.Collection;

public class JaxRsClient {
    public static void main(String args[])throws Exception{
        findAll();
        save();
    }
    public  static void  save() {
        // 测试1: 保存
        WebClient
                .create("http://localhost:8080/ws/mlgbUser/bjService/bjUser")
                .post(new BeiJingUser(100,"tongzhou","tongzhou"));

    }
    public static  void delete(){
        // 测试2:删除(传入id)
        WebClient
                .create("http://localhost:8080/ws/mlgbUser/bjService/bjUser/100")
                .delete();
    }
    /**
     * 查询全部
     * @throws Exception
     */
    public static void findAll() throws Exception {
        Collection collection =
                WebClient
                        .create("http://localhost:8080/ws/mlgbUser/bjService/bjUser")
                        .accept(MediaType.APPLICATION_JSON)
                        .getCollection(BeiJingUser.class);
        System.out.println(collection);
    }
}


5.启动访问:

 

spring+cxf以jax-rs规范实现webservice(restful风格)_第6张图片

 spring+cxf以jax-rs规范实现webservice(restful风格)_第7张图片

你可能感兴趣的:(webservice)