发表于: 2017-09-19 22:05:08

2 780


今天完成的事情:听老大讲解复盘项目,学习jersey。

老大详细讲解了金融产品需要了解的一些金融概念,以及金融产品的风险。讲的打款时出现的问题,以及日息的计算都是我之前没有详细考虑的。


学习了一下jersey

Jersey是JAX-RS(JSR311)开源参考实现用于构建RESTful Web service,它包含三个部分:

  核心服务器(Core Server) 通过提供JSR 311中标准化的注释和API标准化,可以用直观的方式开发RESTful Web服务。

  核心客户端(Core Client) Jersey客户端API能够帮助开发者与RESTful服务轻松通信;

  集成(Integration) Jersey还提供可以轻松继承Spring、Guice、Apache Abdera的库

代码实战

项目结构

pom文件

<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</artifactId>
<packaging>jar</packaging>
<version>1.0-SNAPSHOT</version>
<name>simple-service</name>

<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-grizzly2-http</artifactId>
   </dependency>
   <!-- uncomment this to get JSON support:
    <dependency>
       <groupId>org.glassfish.jersey.media</groupId>
       <artifactId>jersey-media-moxy</artifactId>
   </dependency>
   -->
   <dependency>
       <groupId>junit</groupId>
       <artifactId>junit</artifactId>
       <version>4.9</version>
       <scope>test</scope>
   </dependency>
</dependencies>

<build>
   <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>
       <plugin>
           <groupId>org.codehaus.mojo</groupId>
           <artifactId>exec-maven-plugin</artifactId>
           <version>1.2.1</version>
           <executions>
               <execution>
                   <goals>
                       <goal>java</goal>
                   </goals>
               </execution>
           </executions>
           <configuration>
               <mainClass>com.airkisser.Main</mainClass>
           </configuration>
       </plugin>
   </plugins>
</build>

<properties>
   <jersey.version>2.9</jersey.version>
   <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>
</project>

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

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;
   }
}


明天计划的事情:改方案,讲小课堂

遇到的问题:等PM确定资金流向

收获:学习Jersey,金融产品注意事项


返回列表 返回列表
评论

    分享到