您的位置:首页 > 编程语言 > Java开发

SpringBoot快速开发Rest风格的Web应用

2017-08-30 16:53 323 查看
创建一个普通的maven项目,在pom.xml引入

<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>1.4.3.RELEASE</version>
</parent>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>

<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>


pom.xml内容:
<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>s.t.m</groupId>
<artifactId>stm-pro</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>jar</packaging>
<name>stm-pro</name>
<url>http://maven.apache.org</url>

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

<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>

<dependencies>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.7</version>
<scope>test</scope>
</dependency>

<dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-test</artifactId> <scope>test</scope> </dependency>
</dependencies>

<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<configuration>
<source>1.7</source>
<target>1.7</target>
<encoding>utf-8</encoding>
</configuration>
</plugin>
</plugins>
</build>
</project>


创建好maven项目后,创建一个类名字随意:
package s.t.m.main;

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

@EnableAutoConfiguration
@ComponentScan(basePackages={"s.t.m.controller","s.t.m.service","s.t.m.dao"})
public class Application {
public static void main(String[] args) {
SpringApplication.run(Application.class, args);
}
}

@ComponentScan要指定扫描的包,否则下面的controller可能不会被扫到,一定要指定包。

创建一个提供Rest服务的Controller
package s.t.m.controller;

import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
@RequestMapping("/goods")
public class GoodsController {
@RequestMapping("/query")
public String query(){
return "hello word";
}
}


这样一个Rest风格的Web应该就搭建完了,运行 Application,启动后,就可以使用http://localhost:8080/goods/query访问服务了。

注解说明

1).@SpringBootApplication

很多Spring Boot开发者总是使用 @Configuration , @EnableAutoConfiguration 和 @ComponentScan 注解他们的main类。由于这些注解被如此频繁地一块使用(特别是你遵循以上最佳实践时),Spring Boot提供一个方便的 @SpringBootApplication 选择。

该 @SpringBootApplication 注解等价于以默认属性使用 @Configuration , @EnableAutoConfiguration 和 @ComponentScan 。

2).@RestController和@RequestMapping注解

我们的Example类上使用的第一个注解是@RestController。被称为一个构造型(stereotype)注解。它为阅读代码的人们提供建议。对于Spring,该类扮演了一个特殊角色。在本示例中,我们的类是一个web @Controller ,所以当处理进来的web请求时,Spring会询问它。

@RequestMapping 注解提供路由信息。它告诉Spring任何来自"/"路径的HTTP请求都应该被映射到 home 方法。 

@RestController 注解告诉Spring以字符串的形式渲染结果,并直接返回给调用者。

注: @RestController 和 @RequestMapping 注解是Spring MVC注解(它们不是Spring Boot的特定部分)

         @Controller标识一个Spring类是Spring MVC controller处理器
         @RestController:  a convenience annotation that does nothing more than adding the
@Controller
 and
@ResponseBody
 annotations。            @RestController是@Controller和@ResponseBody的结合体,两个标注合并起来的作用。
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: