创建文件夹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啊,没看出来哪里不同了