快速搭建RESTful Web Service(Spring Boot)

注:源自Spring官方文档的DEMO
完整项目github地址:https://github.com/RottenCuke/Spring
IDEA+MAVEN搭建该项目

编写pom.xml,自动添加依赖

利用MAVEN可以解决jar版本冲突问题

<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/maven-v4_0_0.xsd">
  <modelVersion>4.0.0</modelVersion>
  <groupId>com</groupId>
  <artifactId>Spring</artifactId>
  <packaging>war</packaging>
  <version>1.0-SNAPSHOT</version>
  <name>Spring Maven Webapp</name>
  <url>http://maven.apache.org</url>
  <parent>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-parent</artifactId>
    <version>1.5.2.RELEASE</version>
  </parent>

  <dependencies>
    <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>
    <dependency>
      <groupId>com.jayway.jsonpath</groupId>
      <artifactId>json-path</artifactId>
      <scope>test</scope>
    </dependency>
  </dependencies>

  <properties>
    <java.version>1.8</java.version>
  </properties>


  <build>
    <plugins>
      <plugin>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-maven-plugin</artifactId>
      </plugin>
    </plugins>
  </build>

  <repositories>
    <repository>
      <id>spring-releases</id>
      <url>https://repo.spring.io/libs-release</url>
    </repository>
  </repositories>
  <pluginRepositories>
    <pluginRepository>
      <id>spring-releases</id>
      <url>https://repo.spring.io/libs-release</url>
    </pluginRepository>
  </pluginRepositories>
</project>

编写返回数据实体类

类路径:/src/main/java/hello/

注释:
1.  id是greeting的唯一标识
2. context是内容
package hello;

/**
 * Created by abb on 17-5-16.
 */
public class Greeting {

    private final long id;
    private final String context;

    public Greeting(long id, String context) {
        this.id = id;
        this.context = context;
    }

    public long getId() {
        return id;
    }

    public String getContext() {
        return context;
    }

}

编写Controller类

类路径:/src/main/java/hello/

注释:
1.  @RestController注解表明这个类是Controller类
2. @RequestMapping注解保证HTTP请求/greeting时能够被映射到greeting方法,可以指明访问方式:@RequestMapping(method=GET)/@RequestMapping(method=PUT)等
3.  @RequestParam绑定请求的参数,可设置默认值。即访问时URL若没有带参数,则参数值为默认值
4. 返回数据是一个Greeting对象,但Spring的 MappingJackson2HttpMessageConverter 会自动将Greeting实例对象转换为JSON数据
package hello;

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

import java.util.concurrent.atomic.AtomicLong;

/**
 * Created by abb on 17-5-16.
 * controller
 */
@RestController
public class GreetingController {

   private static final String template = "Helle, %s!";
   private final AtomicLong counter = new AtomicLong();

   @RequestMapping
   public Greeting greeting(@RequestParam(value="name", defaultValue="world!") String name){
      return new Greeting(counter.incrementAndGet(),
              String.format(template,name));
   }

}

编写Application类

类路径:/src/main/java/hello/

注释:
1.  @SpringBootApplication注解包括了@Configuration、@EnableAutoConfiguration、@EnableWebMVC、@ComponentScan
2. 想要访问项目必须先启动main方法,用Main方法中的 SpringApplication.run() 方法发布项目.使用这种方法在HTTP运行时将项目动态加载到TOMCAT中,而不是部署一个外部实例
package hello;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

/**
 * Created by abb on 17-5-16.
 */

@SpringBootApplication
public class Application {
    public static void main(String[] args){
        SpringApplication.run(Application.class, args);
    }
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值