001.Jersey框架—基于JavaSE创建简单RESTful服务

说明:本笔记是在学习《Java RESTful Web Service实战》一书的笔记

一、项目结构如下图

001.Jersey框架—基于JavaSE创建简单RESTful服务_第1张图片
项目结构

二、POM.xml如下



    4.0.0

    com.airkisser
    simple-service
    jar
    1.0-SNAPSHOT
    simple-service

    
        
            
                org.glassfish.jersey
                jersey-bom
                ${jersey.version}
                pom
                import
            
        
    

    
        
            org.glassfish.jersey.containers
            jersey-container-grizzly2-http
        
        
        
            junit
            junit
            4.9
            test
        
    

    
        
            
                org.apache.maven.plugins
                maven-compiler-plugin
                2.5.1
                true
                
                    1.7
                    1.7
                
            
            
                org.codehaus.mojo
                exec-maven-plugin
                1.2.1
                
                    
                        
                            java
                        
                    
                
                
                    com.airkisser.Main
                
            
        
    

    
        2.9
        UTF-8
    

三、代码分析

Main.java(程序入口)

package com.airkisser;

import org.glassfish.grizzly.http.server.HttpServer;
import org.glassfish.jersey.grizzly2.httpserver.GrizzlyHttpServerFactory;
import org.glassfish.jersey.server.ResourceConfig;

import java.io.IOException;
import java.net.URI;

public class Main {
    // Base URI the Grizzly HTTP server will listen on
    public static final String BASE_URI = "http://localhost:8080/simple-service/";

    /**
     * Starts Grizzly HTTP server exposing JAX-RS resources defined in this application.
     * @return Grizzly HTTP server.
     */
    public static HttpServer startServer() {
        // 配置扫描包
        final ResourceConfig rc = new ResourceConfig().packages("com.airkisser.api");

        // create and start a new instance of grizzly http server
        // exposing the Jersey application at BASE_URI
        return GrizzlyHttpServerFactory.createHttpServer(URI.create(BASE_URI), rc);
    }

    /**
     * Main method.
     * @param args
     * @throws IOException
     */
    public static void main(String[] args) throws IOException {
        final HttpServer server = startServer();
        System.out.println(String.format("Jersey app started with WADL available at "
                + "%sapplication.wadl\nHit enter to stop it...", BASE_URI));
        System.in.read();
        server.shutdownNow();
    }
}

DeviceResource.java(资源)

package com.airkisser.api;

import com.airkisser.dao.DeviceDao;
import com.airkisser.entity.Device;

import javax.ws.rs.*;
import javax.ws.rs.core.MediaType;

@Path("device")
public class DeviceResource {

    private final DeviceDao deviceDao;

    // 注入Dao
    public DeviceResource() {
        this.deviceDao = new DeviceDao();
    }

    @GET
    @Produces({MediaType.APPLICATION_JSON,MediaType.APPLICATION_XML})
    public Device get(@QueryParam("ip") final String deviceIp){
        Device result = null;
        if(deviceIp != null){
            result = deviceDao.getDevice(deviceIp);
        }
        return result;
    }

    @PUT
    @Produces({MediaType.APPLICATION_JSON,MediaType.APPLICATION_XML})
    public Device put(final Device device){
        Device result = null;
        if(device != null) {
            result = deviceDao.updateDevice(device);
        }
        return result;
    }

}

DeviceDao.java(模拟的Dao)

package com.airkisser.dao;

import com.airkisser.entity.Device;

import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.ConcurrentMap;

public class DeviceDao {

    private ConcurrentMap fakeDB = new ConcurrentHashMap<>();

    public DeviceDao() {
        fakeDB.put("10.11.58.163", new Device("10.11.58.163"));
        fakeDB.put("10.11.58.185", new Device("10.11.58.185"));
    }

    public Device getDevice(String deviceIp) {
        return fakeDB.get(deviceIp);
    }

    public Device updateDevice(Device device) {
        String ip = device.getDeviceIp();
        if (ip != null && fakeDB.containsKey(ip)) {
           fakeDB.put(ip, device);
        }
        return fakeDB.get(ip);
    }
    
}

Device.java

package com.airkisser.entity;

import javax.xml.bind.annotation.XmlAttribute;
import javax.xml.bind.annotation.XmlRootElement;

@XmlRootElement(name = "device")
public class Device {

    private String deviceIp;

    private int deviceStatus;

    public Device() {
    }

    public Device(String deviceIp) {
        this.deviceIp = deviceIp;
    }

    // @XmlAttribute只能注解在get方法上,不能直接注解到属性上
    @XmlAttribute
    public String getDeviceIp() {
        return deviceIp;
    }

    public void setDeviceIp(String deviceIp) {
        this.deviceIp = deviceIp;
    }

    @XmlAttribute
    public int getDeviceStatus() {
        return deviceStatus;
    }

    public void setDeviceStatus(int deviceStatus) {
        this.deviceStatus = deviceStatus;
    }
}

四、测试

ResourceTest.java

package com.airkisser;

import com.airkisser.entity.Device;
import org.glassfish.grizzly.http.server.HttpServer;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;

import javax.ws.rs.client.Client;
import javax.ws.rs.client.ClientBuilder;
import javax.ws.rs.client.Entity;
import javax.ws.rs.client.WebTarget;
import javax.ws.rs.core.MediaType;

import static org.junit.Assert.assertEquals;

public class ResourceTest {

    private HttpServer server;
    private WebTarget target;

    @Before
    public void setUp() throws Exception {
        // start the server
        server = Main.startServer();
        // create the client
        Client c = ClientBuilder.newClient();

        // uncomment the following line if you want to enable
        // support for JSON in the client (you also have to uncomment
        // dependency on jersey-media-json module in pom.xml and Main.startServer())
        // --
        // c.configuration().enable(new org.glassfish.jersey.media.json.JsonJaxbFeature());

        target = c.target(Main.BASE_URI);
    }

    @After
    public void tearDown() throws Exception {
        server.shutdownNow();
    }

    @Test
    public void testGetDevice() {
        final String testIp = "10.11.58.185";
        final Device device = target.path("device").queryParam("ip", testIp).request().get(Device.class);
        assertEquals(testIp, device.getDeviceIp());
    }

    @Test
    public void testPutDevice() {
        final String testIp = "10.11.58.185";
        final Device device = new Device(testIp);
        device.setDeviceStatus(1);
        Entity entity = Entity.entity(device, MediaType.APPLICATION_XML_TYPE);
        final Device result = target.path("device").request().put(entity, Device.class);
        assertEquals(1, result.getDeviceStatus());
    }
}

SoapUI工具测试

device资源的get方法测试

device资源的get方法测试

device资源的put方法测试

001.Jersey框架—基于JavaSE创建简单RESTful服务_第2张图片
device资源的put方法测试

备注

启动程序后,输入http://localhost:8080/simple-service/application.wadl可查看WADL内容


    
    
    
        
            
        
    
    
        
            
                
                    
                    
                
            
            
                
                    
                
                
                    
                    
                
            
        
    

你可能感兴趣的:(001.Jersey框架—基于JavaSE创建简单RESTful服务)