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


[Author]: kwu

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

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

package com.hexun.restful;

import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.servlet.mvc.method.annotation.AbstractJsonpResponseBodyAdvice;

import com.hexun.bean.CharsForPvuvip;
import com.hexun.dao.ChartDao;


@RestController
public class RestfulController {

	@ControllerAdvice
	static class JsonpAdvice extends AbstractJsonpResponseBodyAdvice {
		public JsonpAdvice() {
			super("callback");
		}
	}

	@RequestMapping("/pv")
	public CharsForPvuvip getPvuvipByday(@RequestParam(value = "limit", defaultValue = "15") int limit) {
		CharsForPvuvip data = new CharsForPvuvip();
		try {
			data = ChartDao.getPvuvip(limit);
		} catch (Exception e) {
			e.printStackTrace();
		}
		return data;
	}
}

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


2、POJO类

package com.hexun.bean;


public class CharsForPvuvip {
	private String[] titles;
	private String[] days;
	private Integer[] uvs;
	private Integer[] pvs;
	private Integer[] ipcnts;

	public String[] getTitles() {
		return titles;
	}

	public void setTitles(String[] titles) {
		this.titles = titles;
	}

	public String[] getDays() {
		return days;
	}

	public void setDays(String[] days) {
		this.days = days;
	}

	public Integer[] getUvs() {
		return uvs;
	}

	public void setUvs(Integer[] uvs) {
		this.uvs = uvs;
	}

	public Integer[] getPvs() {
		return pvs;
	}

	public void setPvs(Integer[] pvs) {
		this.pvs = pvs;
	}

	public Integer[] getIpcnts() {
		return ipcnts;
	}

	public void setIpcnts(Integer[] ipcnts) {
		this.ipcnts = ipcnts;
	}

}


3、程序入口

package com.hexun.client;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
import org.springframework.context.annotation.ComponentScan;

@ComponentScan({"com.hexun.restful"})
@EnableAutoConfiguration
public class Application {

    public static void main(String[] args) {
        SpringApplication.run(Application.class, args);
    }
}

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


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

<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/xsd/maven-4.0.0.xsd">
	<modelVersion>4.0.0</modelVersion>

	<groupId>com.ganymede</groupId>
	<artifactId>restful</artifactId>
	<version>0.0.1-SNAPSHOT</version>

	<name>restful</name>
	<url>http://maven.apache.org</url>

	<properties>
		<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
		<java.version>1.7</java.version>
	</properties>

	<parent>
		<groupId>org.springframework.boot</groupId>
		<artifactId>spring-boot-starter-parent</artifactId>
		<version>1.2.3.RELEASE</version>
	</parent>

	<dependencies>
		<dependency>
			<groupId>org.springframework.boot</groupId>
			<artifactId>spring-boot-starter-web</artifactId>
		</dependency>
		<dependency>
				<groupId>mysql</groupId>
				<artifactId>mysql-connector-java</artifactId>
				<version>5.1.26</version>
		</dependency>
	</dependencies>

	<build>
		<plugins>
			<plugin>
				<groupId>org.springframework.boot</groupId>
				<artifactId>spring-boot-maven-plugin</artifactId>
			</plugin>
			<plugin>
				<artifactId>maven-assembly-plugin</artifactId>
					<configuration>
						<archive>
							<manifest>
								<mainClass>com.hexun.client.Application</mainClass>
							</manifest>
						</archive>
						<descriptorRefs>
							<descriptorRef>jar-with-dependencies</descriptorRef>
						</descriptorRefs>						
					</configuration>
			</plugin>
		</plugins>
	</build>

</project>

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

mvn assembly:assembly


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

nohup java -jar restful-0.0.1-SNAPSHOT.jar --server.port=9088 2>&1 > restful.log &

运行截图如下:



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


相关内容