美文网首页
002.Jersey框架—基于JavaEE创建简单RESTful

002.Jersey框架—基于JavaEE创建简单RESTful

作者: airkisser | 来源:发表于2017-01-11 16:53 被阅读0次

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

一、项目结构如下图

项目结构

二、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/maven-v4_0_0.xsd">

    <modelVersion>4.0.0</modelVersion>

    <groupId>com.airkisser</groupId>
    <artifactId>simple-service-webapp</artifactId>
    <packaging>war</packaging>
    <version>1.0-SNAPSHOT</version>
    <name>simple-service-webapp</name>

    <build>
        <finalName>simple-service-webapp</finalName>
        <plugins>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-compiler-plugin</artifactId>
                <version>2.5.1</version>
                <inherited>true</inherited>
                <configuration>
                    <source>1.7</source>
                    <target>1.7</target>
                </configuration>
            </plugin>
        </plugins>
    </build>

    <dependencyManagement>
        <dependencies>
            <dependency>
                <groupId>org.glassfish.jersey</groupId>
                <artifactId>jersey-bom</artifactId>
                <version>${jersey.version}</version>
                <type>pom</type>
                <scope>import</scope>
            </dependency>
        </dependencies>
    </dependencyManagement>

    <dependencies>
        <dependency>
            <groupId>org.glassfish.jersey.containers</groupId>
            <artifactId>jersey-container-servlet-core</artifactId>
            <!-- use the following artifactId if you don't need servlet 2.x compatibility -->
            <!-- artifactId>jersey-container-servlet</artifactId -->
        </dependency>
        <!-- uncomment this to get JSON support
        <dependency>
            <groupId>org.glassfish.jersey.media</groupId>
            <artifactId>jersey-media-moxy</artifactId>
        </dependency>
        -->
    </dependencies>
    <properties>
        <jersey.version>2.9</jersey.version>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
    </properties>
</project>

三、代码分析

web.xml(web配置)

<?xml version="1.0" encoding="UTF-8"?>
<!-- This web.xml file is not required when using Servlet 3.0 container,
     see implementation details http://jersey.java.net/nonav/documentation/latest/jax-rs.html -->
<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">
    <servlet>
        <servlet-name>Jersey Web Application</servlet-name>
        <servlet-class>org.glassfish.jersey.servlet.ServletContainer</servlet-class>
        <init-param><!--资源包扫描配置-->
            <param-name>jersey.config.server.provider.packages</param-name>
            <param-value>com.airkisser</param-value>
        </init-param>
        <load-on-startup>1</load-on-startup>
    </servlet>
    <servlet-mapping>
        <servlet-name>Jersey Web Application</servlet-name>
        <url-pattern>/webapi/*</url-pattern>
    </servlet-mapping>
</web-app>

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<String, Device> 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;
    }
}

四、测试

SoapUI工具测试

device资源的get方法测试

device资源的get方法测试

device资源的put方法测试

device资源的put方法测试

备注

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

<application xmlns="http://wadl.dev.java.net/2009/02">
    <doc xmlns:jersey="http://jersey.java.net/" jersey:generatedBy="Jersey: 2.9 2014-05-22 05:12:10"/>
    <doc xmlns:jersey="http://jersey.java.net/"
         jersey:hint="This is simplified WADL with user and core resources only. To get full WADL with extended resources use the query parameter detail. Link: http://localhost:8080/webapi/application.wadl?detail=true"/>
    <grammars>
        <include href="application.wadl/xsd0.xsd">
            <doc title="Generated" xml:lang="en"/>
        </include>
    </grammars>
    <resources base="http://localhost:8080/webapi/">
        <resource path="device">
            <method id="put" name="PUT">
                <response>
                    <ns2:representation xmlns:ns2="http://wadl.dev.java.net/2009/02" xmlns="" element="device"
                                        mediaType="application/json"/>
                    <ns2:representation xmlns:ns2="http://wadl.dev.java.net/2009/02" xmlns="" element="device"
                                        mediaType="application/xml"/>
                </response>
            </method>
            <method id="get" name="GET">
                <request>
                    <param xmlns:xs="http://www.w3.org/2001/XMLSchema" name="ip" style="query" type="xs:string"/>
                </request>
                <response>
                    <ns2:representation xmlns:ns2="http://wadl.dev.java.net/2009/02" xmlns="" element="device"
                                        mediaType="application/json"/>
                    <ns2:representation xmlns:ns2="http://wadl.dev.java.net/2009/02" xmlns="" element="device"
                                        mediaType="application/xml"/>
                </response>
            </method>
        </resource>
    </resources>
</application>

相关文章

  • 002.Jersey框架—基于JavaEE创建简单RESTful

    说明:本笔记是在学习《Java RESTful Web Service实战》一书的笔记 一、项目结构如下图 二、P...

  • rustful

    rustful是针对Rust的RESTful web 框架。rustful的主要目标是为基于HTTP的程序创建简单...

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

    说明:本笔记是在学习《Java RESTful Web Service实战》一书的笔记 一、项目结构如下图 二、P...

  • Retrofit源码解析

    Retrofit简介 Retrofit是一个基于OKHttp的RESTful网络请求框架。简单来说,Retrofi...

  • RESTful API实现

    基于Django实现RESTful API 路由 视图 结果 基于Django REST Framework框架实...

  • 基于springboot创建RESTful风格接口

    基于springboot创建RESTful风格接口 RESTful API风格 特点: URL描述资源 使用HTT...

  • 如何创建spring restful api

    最近在研究spring框架,今天试着写了一个简单的user操作的restful api, 以下是创建的步骤: 1....

  • restful简介与风格设计

    restful:是一种架构风格,是一种理论框架,可以让项目更简单/更清晰. restful框架关心的有:资源:所谓...

  • Retrofit库

    Retrofit介绍 Retrofit是Square公司基于Restful风格推出的网络框架封装Retrofit是...

  • Retrofit 入门

    Retrofit 简介 Retrofit 是 Square 公司基于 Restful 风格推出的网络框架封装。Re...

网友评论

      本文标题:002.Jersey框架—基于JavaEE创建简单RESTful

      本文链接:https://www.haomeiwen.com/subject/rjerbttx.html