首页 > 编程语言 > 详细

Spring用gradle构建restful websrevice

时间:2015-11-11 01:08:45      阅读:330      评论:0      收藏:0      [点我收藏+]

创建文件夹restdemo 算是project名

在restdemo创建文件夹\src\main\java\hello

在restdemo 创建 build.gradle

buildscript {
    repositories {
        mavenCentral()
    }
    dependencies {
        classpath("org.springframework.boot:spring-boot-gradle-plugin:1.2.7.RELEASE")
    }
}

apply plugin: ‘java‘
apply plugin: ‘eclipse‘
apply plugin: ‘idea‘
apply plugin: ‘spring-boot‘

jar {
    baseName = ‘gs-rest-service‘
    version =  ‘0.1.0‘
}

repositories {
    mavenCentral()
}

sourceCompatibility = 1.8
targetCompatibility = 1.8

dependencies {
    compile("org.springframework.boot:spring-boot-starter-web")
    testCompile("junit:junit")
}

task wrapper(type: Wrapper) {
    gradleVersion = ‘2.3‘
}

切换到\src\main\java\hello 文件下

创建Greeting.java

package hello;

public class Greeting {

    private final long id;
    private final String content;

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

    public long getId() {
        return id;
    }

    public String getContent() {
        return content;
    }
}

创建Controller类GreetingController

package hello;

import java.util.concurrent.atomic.AtomicLong;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

@RestController
public class GreetingController {

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

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

创建springboot启动类Application.java

package hello;

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

@SpringBootApplication
public class Application {

    public static void main(String[] args) {
        SpringApplication.run(Application.class, args);
    }
}

目前restdemo目录下为

技术分享

cmd进入命令行 :切换到restdemo目录下 输入 gradle build 之后

技术分享

将工程导入到sts中

技术分享

打开Application.java ,右击-->run as --> 1java application

技术分享

在浏览器输入http://localhost:8080/greeting后 在页面显示

{"id":1,"content":"Hello, World!"}
地址换为 http://localhost:8080/greeting?name=roodemo 页面显示
{"id":2,"content":"Hello, roodemo!"}
spring官网的栗子,官网:https://spring.io/guides/gs/rest-service/

看起来就是一般的springmvc啊,没看出来哪里不同了

Spring用gradle构建restful websrevice

原文:http://www.cnblogs.com/bashala/p/4954960.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!