gradle第二天(Building a RESTful Web Service)

1.创建项目project:

  在项目下用命令:mkdir -p src/main/java/hello/ 

  创建路径  src/main/java/hello/ 

2.在项目下添加文件:

  (1)在src/main/java/hello/下添加三个java文件,分别是:

    Application.java

 1 package hello;
 2 
 3 import org.springframework.boot.SpringApplication;
 4 import org.springframework.boot.autoconfigure.SpringBootApplication; 
 5 
 6 @SpringBootApplication
 7 public class Application {
 8     public static void main(String[] args) { 
 9         SpringApplication.run(Application.class, args);
10         } 
11     }

 

    Greeting.java

 1 package hello;
 2 public class Greeting {
 3     private final long id; 
 4     private final String content;
 5     public Greeting(long id, String content) { 
 6         this.id = id;
 7         this.content = content; 
 8         }
 9     public long getId() { 
10         return id;
11         }
12 public String getContent() { 
13     return content;
14         } 
15 }

 

    GreetingController.java

 1 package hello;
 2 
 3 import java.util.concurrent.atomic.AtomicLong;
 4 import org.springframework.web.bind.annotation.RequestMapping;
 5 import org.springframework.web.bind.annotation.RequestParam;
 6 import org.springframework.web.bind.annotation.RestController;
 7 
 8 @RestController
 9 public class GreetingController {
10     private static final String template = "Hello, %s!";
11     private final AtomicLong counter = new AtomicLong();
12 
13     @RequestMapping("/greeting")
14     public Greeting greeting(
15             @RequestParam(value = "name", defaultValue = "World") String name) {
16            return new Greeting(counter.incrementAndGet(), String.format(template,name));
17     }
18 }

 

  (2)在project目录下添加build.gradle文件:

    

 1 buildscript {
 2     repositories {
 3         mavenCentral()
 4     }
 5     dependencies {
 6         classpath("org.springframework.boot:spring-boot-gradle-plugin:1.2.5.RELEASE")
 7     }
 8 }
 9 
10 apply plugin: 'java'
11 apply plugin: 'eclipse'
12 apply plugin: 'idea'
13 apply plugin: 'spring-boot'
14 
15 jar {
16     baseName = 'gs-rest-service'
17     version =  '0.1.0'
18 }
19 
20 repositories {
21     mavenCentral()
22 }
23 
24 sourceCompatibility = 1.7
25 targetCompatibility = 1.7
26 
27 dependencies {
28     compile("org.springframework.boot:spring-boot-starter-web")
29     testCompile("junit:junit")
30 }
31 
32 task wrapper(type: Wrapper) {
33     gradleVersion = '2.3'
34 }

3.gradle build

  建立gradle环境

4.gradle run

  运行gradle服务

 

 

 

 

 

 

 

 

posted @ 2015-09-08 10:30  ZashioM  阅读(485)  评论(0编辑  收藏  举报