New project directory: Hello
Project Structure directory:mkdir src\main\java\Hello
Create Gradle Project build file: Build.gradle
buildscript { repositories { mavencentral () } Dependencies { classpath ("Org.springframework.boot: Spring-boot-gradle-plugin:1.4.3.release ") }} apply plugin: ' Java ' apply plugin: ' eclipse ' apply plugin: ' idea ' apply plugin: ' org.springframework.boot ' jar { baseName = ' Gs-rest-service ' version = ' 0.1.0 '} repositories { mavencentral ()} sourcecompatibility = 1.8targetcompatibility = 1.8 dependencies { compile (" Org.springframework.boot:spring-boot-starter-web ") testcompile (' Org.springframework.boot: Spring-boot-starter-test ')}
Create entity classes-vectors for interactive data: 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; }}
new controller: src/main/java/hello/greetingcontroller.java
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; @RestControllerpublic classgreetingcontroller { private static final string template = "hello, %s!"; private final atomiclong counter = new atomiclong (); @RequestMapping ("/greeting") public greetinggreeting (@RequestParam (value= "name", defaultvalue= "World") string name) { return new greeting (Counter.incrementandget (), String.format (template, name)); }}
Program Entry: Src/main/java/hello/greetingcontroller.java
Package Hello; Import Org.springframework.boot.springapplication;import Org.springframework.boot.autoconfigure.SpringBootApplication; @SpringBootApplicationpublic class Application {public static void main (string[] args) {Springapplication.run (Application.class, args); }}
View Build command:gradletasks
To Run the architecture command: Gradle Build /bootrun generating Jar packages
Start the Java jar package: Java-jar Build/libs/gs-rest-service-0.1.0.jar
or from build to run one in place: gradle Clean Build && java -jarbuild/libs/gs-rest-service-0.1.0.jar
650) this.width=650; "src=" Http://s2.51cto.com/wyfs02/M00/8D/1F/wKioL1iKCH7gtljJAADJkBVZKLI730.png "title=" 2017-01-26_223215.png "alt=" Wkiol1ikch7gtljjaadjkbvzkli730.png "/>
Command-typed: http://localhost:8080/greeting
Get output:
650) this.width=650; "src=" Http://s4.51cto.com/wyfs02/M01/8D/22/wKiom1iKCPrRANMuAAAU8kEQacE474.png "style=" float: none; "title=" 2017-01-26_223318.png "alt=" Wkiom1ikcprranmuaaau8keqace474.png "/>
Parameters passed in Name:
650) this.width=650; "src=" Http://s3.51cto.com/wyfs02/M01/8D/1F/wKioL1iKCP6j-cnsAAAXfptJdbo317.png "style=" float: none; "title=" 2017-01-26_223402.png "alt=" Wkiol1ikcp6j-cnsaaaxfptjdbo317.png "/>
about Rest Introduction of:
Http://spring.io/understanding/REST
This article is from the "lybing" blog, make sure to keep this source http://lybing.blog.51cto.com/3286625/1894301
Quickly create Restful Web Service with spring boot