之前的项目使用的SpringBoot做的服务器端开发。发现很少的XML配置,全部通过注解完成,代码简单粗暴。但是门槛稍微高了一些,尤其是对一些对Spring不太了解或者不是很熟悉的pg来说。
接下来是新项目也需要一个排行榜的功能,需要搭建一个服务器,来接受排行榜,保存玩家信息的需求。于是从0开始利用Springboot搭建了一个服务器。因为国内SpringBoot方面的资料很少。
1、开始,用eclipse创建一个maven工程。Ctrl+N,输入maven,选择Maven Project
2、修改pom.xml
<?xml version="1.0" encoding="UTF-8"?>
<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.spring.boot</groupId>
<artifactId>springboot</artifactId>
<version>1.0-SNAPSHOT</version>
<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>
</dependencies>
</project>
3、入口类
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
@Configuration
@ComponentScan("xxx.api.server")
@EnableAutoConfiguration
public class APIServer {
public static void main(String[] args) {
SpringApplication.run(APIServer.class);
}
}
4、Controller
import java.util.Date;
import java.util.List;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;
import cubecat.api.server.dto.ranking.GetRanki