Springboot 实现 Restful 服务

首先要声明,这个demo很大程度上参考了这篇文章:

Springboot 实现 Restful 服务,基于 HTTP / JSON 传输

所以把它归为转载之列。


首先创建数据表并插入一条数据(数据库名随意):

[sql]  view plain  copy
  1. DROP TABLE IF EXISTS  `city`;  
  2. CREATE TABLE `city` (  
  3.   `id` int(10) unsigned NOT NULL AUTO_INCREMENT COMMENT '城市编号',  
  4.   `province_id` int(10) unsigned  NOT NULL COMMENT '省份编号',  
  5.   `city_name` varchar(25) DEFAULT NULL COMMENT '城市名称',  
  6.   `description` varchar(25) DEFAULT NULL COMMENT '描述',  
  7.   PRIMARY KEY (`id`)  
  8. ) ENGINE=InnoDB AUTO_INCREMENT=1 DEFAULT CHARSET=utf8;  
  9. INSERT city VALUES (1 ,1,'温岭市','BYSocket 的家在温岭。');  

接下去是项目结构:



添加依赖:

[html]  view plain  copy
  1. <!-- Spring Boot 启动父依赖 -->  
  2.    <parent>  
  3.        <groupId>org.springframework.boot</groupId>  
  4.        <artifactId>spring-boot-starter-parent</artifactId>  
  5.        <version>1.5.1.RELEASE</version>  
  6.    </parent>  
  7.   
  8.    <properties>  
  9.        <mybatis-spring-boot>1.2.0</mybatis-spring-boot>  
  10.        <mysql-connector>5.1.39</mysql-connector>  
  11.    </properties>  
  12.   
  13.    <dependencies>  
  14.   
  15.        <!-- Spring Boot Web 依赖 -->  
  16.        <dependency>  
  17.            <groupId>org.springframework.boot</groupId>  
  18.            <artifactId>spring-boot-starter-web</artifactId>  
  19.        </dependency>  
  20.   
  21.        <!-- Spring Boot Test 依赖 -->  
  22.        <dependency>  
  23.            <groupId>org.springframework.boot</groupId>  
  24.            <artifactId>spring-boot-starter-test</artifactId>  
  25.            <scope>test</scope>  
  26.        </dependency>  
  27.   
  28.        <!-- Spring Boot Mybatis 依赖 -->  
  29.        <dependency>  
  30.            <groupId>org.mybatis.spring.boot</groupId>  
  31.            <artifactId>mybatis-spring-boot-starter</artifactId>  
  32.            <version>${mybatis-spring-boot}</version>  
  33.        </dependency>  
  34.   
  35.        <!-- MySQL 连接驱动依赖 -->  
  36.        <dependency>  
  37.            <groupId>mysql</groupId>  
  38.            <artifactId>mysql-connector-java</artifactId>  
  39.            <version>${mysql-connector}</version>  
  40.        </dependency>  
  41.   
  42.        <!-- Junit -->  
  43.        <dependency>  
  44.            <groupId>junit</groupId>  
  45.            <artifactId>junit</artifactId>  
  46.            <version>4.12</version>  
  47.        </dependency>  
  48.    </dependencies>  

然后说说三个比较重要的文件,并附加部分说明:

1、配置文件application.properties(名字不可变)

[plain]  view plain  copy
  1. ## 数据源配置  
  2. spring.datasource.url=jdbc:mysql://localhost:3306/springbootdb?useUnicode=true&characterEncoding=utf8  
  3. spring.datasource.username=root  
  4. spring.datasource.password=123456  
  5. spring.datasource.driver-class-name=com.mysql.jdbc.Driver  
  6.   
  7. ## Mybatis 配置  
  8. mybatis.typeAliasesPackage=haha.domain  
  9. mybatis.mapperLocations=classpath*:haha/mapper/*.xml  

各配置项根据自己情况修改,应该是一目了然的。

附:如果mapper像我一样不放在resources目录下的话,请在pom.xml中添加如下代码,否则你的.xml文件可能不在build文件中。

[html]  view plain  copy
  1. <!--将xml文件打包-->  
  2. <build>  
  3.     <resources>  
  4.         <resource>  
  5.             <directory>src/main/java</directory>  
  6.             <includes>  
  7.                 <include>**/*.xml</include>  
  8.             </includes>  
  9.             <filtering>false</filtering>  
  10.         </resource>  
  11.     </resources>  
  12. </build>  

2、启动文件Application.java

[java]  view plain  copy
  1. // Spring Boot 应用的标识  
  2. @SpringBootApplication  
  3. // mapper 接口类扫描包配置  
  4. @MapperScan("haha.dao")  
  5. public class Application {  
  6.   
  7.     public static void main(String[] args) {  
  8.         // 程序启动入口  
  9.         // 启动嵌入式的 Tomcat 并初始化 Spring 环境及其各 Spring 组件  
  10.         SpringApplication.run(Application.class,args);  
  11.     }  
  12. }  
主要作用是创建一个Spring应用上下文(Application Context)。另外,注意这里的mapper接口类扫描包的配置,还有就是如果是部署到tomcat下这里也会进行修改。(自行百度吧,我就懒得说啦~~)

3、Controller类

[java]  view plain  copy
  1. @RestController  
  2. //是一类特殊的@Controller,它的返回值直接作为HTTP Response的Body部分返回给浏览器。  
  3. //@Controller并非直接将字符串返回给浏览器,而是寻找名字为返回值的模板进行渲染  
  4. public class CityController {  
  5.   
  6.     @Autowired  
  7.     private CityService cityService;  
  8.   
  9.     //@RequestMapping注解表明该方法处理那些URL对应的HTTP请求,也就是我们常说的URL路由(routing),请求的分发工作是有Spring完成的。  
  10.     //URL中的变量——PathVariable  
  11.     //例如@RequestMapping("/api/city/{id}")  
  12.     //URL中的变量可以用{variableName}来表示,同时在方法的参数中加上@PathVariable("variableName"),那么当请求被转发给该方法处理时,对应的URL中的变量会被自动赋值给被@PathVariable注解的参数  
  13.     @RequestMapping(value = "/api/city/{id}", method = RequestMethod.GET)  
  14.     public City findOneCity(@PathVariable("id") Long id) {  
  15.         return cityService.findCityById(id);  
  16.     }  
  17. }  
根据注释应当是一目了然的,在此我便不多赘述了。


最后便是运行了,启动main函数,发现其实这段程序是个web应用,放置在内嵌的Servlet容器中。


访问url:http://127.0.0.1:8080/api/city/1



以上便是全部内容啦,谢谢阅读~~


差点忘了放上demo了。。

猜你喜欢

转载自blog.csdn.net/wuqianjing/article/details/80440237