• Spring用gradle构建restful websrevice


    创建文件夹restdemo 算是project名

    在restdemo创建文件夹srcmainjavahello

    在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'
    }
    

    切换到srcmainjavahello 文件下

    创建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啊,没看出来哪里不同了
  • 相关阅读:
    雅米新一代企业管理平台
    Telerik.Web.UI应用到sharepoint2010注意事项
    SharePoint Server 2010 容量管理:软件边界和限制
    【读】构建可枚举类型(IEnumerator和IEnumerable)
    【读】C# 窗口间传递数据
    分类(转)
    “盗”亦有道
    English学习指南
    爱情是什么?
    客户需要的26个待遇 (转)
  • 原文地址:https://www.cnblogs.com/bashala/p/4954960.html
Copyright © 2020-2023  润新知