基于Spring4的全注解实现Restful开发

[Author]: kwu

全注解实现Spring Restful开发,restful提供了快速的交互形式,以json的为数据传递的格式。

1、Restful控制类实现,RequestMapping设置访问restful的路径,RequestParam设置参数的默认值

[java]  view plain copy
  1. package com.hexun.restful;  
  2.   
  3. import org.springframework.web.bind.annotation.ControllerAdvice;  
  4. import org.springframework.web.bind.annotation.RequestMapping;  
  5. import org.springframework.web.bind.annotation.RequestParam;  
  6. import org.springframework.web.bind.annotation.RestController;  
  7. import org.springframework.web.servlet.mvc.method.annotation.AbstractJsonpResponseBodyAdvice;  
  8.   
  9. import com.hexun.bean.CharsForPvuvip;  
  10. import com.hexun.dao.ChartDao;  
  11.   
  12.   
  13. @RestController  
  14. public class RestfulController {  
  15.   
  16.     @ControllerAdvice  
  17.     static class JsonpAdvice extends AbstractJsonpResponseBodyAdvice {  
  18.         public JsonpAdvice() {  
  19.             super("callback");  
  20.         }  
  21.     }  
  22.   
  23.     @RequestMapping("/pv")  
  24.     public CharsForPvuvip getPvuvipByday(@RequestParam(value = "limit", defaultValue = "15"int limit) {  
  25.         CharsForPvuvip data = new CharsForPvuvip();  
  26.         try {  
  27.             data = ChartDao.getPvuvip(limit);  
  28.         } catch (Exception e) {  
  29.             e.printStackTrace();  
  30.         }  
  31.         return data;  
  32.     }  
  33. }  

省略了业务的DAO类,这里根据不同的需求来实现


2、POJO类

[java]  view plain copy
  1. package com.hexun.bean;  
  2.   
  3.   
  4. public class CharsForPvuvip {  
  5.     private String[] titles;  
  6.     private String[] days;  
  7.     private Integer[] uvs;  
  8.     private Integer[] pvs;  
  9.     private Integer[] ipcnts;  
  10.   
  11.     public String[] getTitles() {  
  12.         return titles;  
  13.     }  
  14.   
  15.     public void setTitles(String[] titles) {  
  16.         this.titles = titles;  
  17.     }  
  18.   
  19.     public String[] getDays() {  
  20.         return days;  
  21.     }  
  22.   
  23.     public void setDays(String[] days) {  
  24.         this.days = days;  
  25.     }  
  26.   
  27.     public Integer[] getUvs() {  
  28.         return uvs;  
  29.     }  
  30.   
  31.     public void setUvs(Integer[] uvs) {  
  32.         this.uvs = uvs;  
  33.     }  
  34.   
  35.     public Integer[] getPvs() {  
  36.         return pvs;  
  37.     }  
  38.   
  39.     public void setPvs(Integer[] pvs) {  
  40.         this.pvs = pvs;  
  41.     }  
  42.   
  43.     public Integer[] getIpcnts() {  
  44.         return ipcnts;  
  45.     }  
  46.   
  47.     public void setIpcnts(Integer[] ipcnts) {  
  48.         this.ipcnts = ipcnts;  
  49.     }  
  50.   
  51. }  


3、程序入口

[java]  view plain copy
  1. package com.hexun.client;  
  2.   
  3. import org.springframework.boot.SpringApplication;  
  4. import org.springframework.boot.autoconfigure.EnableAutoConfiguration;  
  5. import org.springframework.context.annotation.ComponentScan;  
  6.   
  7. @ComponentScan({"com.hexun.restful"})  
  8. @EnableAutoConfiguration  
  9. public class Application {  
  10.   
  11.     public static void main(String[] args) {  
  12.         SpringApplication.run(Application.class, args);  
  13.     }  
  14. }  

注解@ComponentScan 指定了控制类的路径,如果不指定spring就在当前路径扫描


4、pom.xml文件,依赖管理与用的是maven

[html]  view plain copy
  1. <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"  
  2.     xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">  
  3.     <modelVersion>4.0.0</modelVersion>  
  4.   
  5.     <groupId>com.ganymede</groupId>  
  6.     <artifactId>restful</artifactId>  
  7.     <version>0.0.1-SNAPSHOT</version>  
  8.   
  9.     <name>restful</name>  
  10.     <url>http://maven.apache.org</url>  
  11.   
  12.     <properties>  
  13.         <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>  
  14.         <java.version>1.7</java.version>  
  15.     </properties>  
  16.   
  17.     <parent>  
  18.         <groupId>org.springframework.boot</groupId>  
  19.         <artifactId>spring-boot-starter-parent</artifactId>  
  20.         <version>1.2.3.RELEASE</version>  
  21.     </parent>  
  22.   
  23.     <dependencies>  
  24.         <dependency>  
  25.             <groupId>org.springframework.boot</groupId>  
  26.             <artifactId>spring-boot-starter-web</artifactId>  
  27.         </dependency>  
  28.         <dependency>  
  29.                 <groupId>mysql</groupId>  
  30.                 <artifactId>mysql-connector-java</artifactId>  
  31.                 <version>5.1.26</version>  
  32.         </dependency>  
  33.     </dependencies>  
  34.   
  35.     <build>  
  36.         <plugins>  
  37.             <plugin>  
  38.                 <groupId>org.springframework.boot</groupId>  
  39.                 <artifactId>spring-boot-maven-plugin</artifactId>  
  40.             </plugin>  
  41.             <plugin>  
  42.                 <artifactId>maven-assembly-plugin</artifactId>  
  43.                     <configuration>  
  44.                         <archive>  
  45.                             <manifest>  
  46.                                 <mainClass>com.hexun.client.Application</mainClass>  
  47.                             </manifest>  
  48.                         </archive>  
  49.                         <descriptorRefs>  
  50.                             <descriptorRef>jar-with-dependencies</descriptorRef>  
  51.                         </descriptorRefs>                       
  52.                     </configuration>  
  53.             </plugin>  
  54.         </plugins>  
  55.     </build>  
  56.   
  57. </project>  

5、打包运行jar,在源码路径中输入

[plain]  view plain copy
  1. mvn assembly:assembly  


6、运行jar执行,参数 --server.port=9088 为指定restful服务的端口

[plain]  view plain copy
  1. nohup java -jar restful-0.0.1-SNAPSHOT.jar --server.port=9088 2>&1 > restful.log &  

运行截图如下:

基于Spring4的全注解实现Restful开发_第1张图片

restful 浏览器直接访问返回json数据,前台显示UI获得数据后,以图表展示给用户。

基于Spring4的全注解实现Restful开发_第2张图片


你可能感兴趣的:(java,spring,json,数据,开发)