First, Introduction: Using Spring boot We can easily create a standalone rest Web service, embedded tomact, we can only "run" to see the effect.
Spring boot uses gradle or MAVEN to build the way to introduce third-party libraries, so I don't have to worry about which libraries we're introducing, and using spring boot eliminates a lot of tedious configuration.
Next, we'll implement the rest Web service like C # MVC with spring boot.
Second, the effect: the classic Hello world.
This will be the end result, no need to configure deployment tomact, we only need a "run", the Rest Web service is finished.
Iii. Preparatory work:
- A self-satisfied IDE, I chose Eclipse Luna. You can go to the official website by yourself.
- JDK1.8 or higher
- gradle2.3+ or maven3.0+
Here I will use the Gradle construction method, is also the reason that I choose Luna, it integrates the Gradle project creation inside.
Iv. start creating, we open Eclipse Luna, and if it doesn't open, check to see if the JDK is installed and Java_home is configured in the environment variable.
As for how to configure the JDK, a lot of online, Baidu or Google can search.
First open Eclipse Luna and create a gradle Project.
Select New-other-gradle-gradle Project.
I'm named Gradletestone here. Once created, it's probably like this:
Our job is simply to create a gradle Project, and our goal is to build a spring Web program.
V. Add spring support to the project.
Take a look at the code below: We'll replace the Build.gradle file in Gradletestone with the code.
Buildscript {repositories {mavencentral ()} dependencies {classpath ("Org.springframework.boot:spring-boot-gradle-plugin:1.3.1.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.8targetcompatibility= 1.8Dependencies {compile ("Org.springframework.boot:spring-boot-starter-web") Testcompile ("Junit:junit")}task Wrapper (type:wrapper) {gradleversion= ' 2.3 '}
What does it mean inside, we don't care about it first.
Vi. use Gradle to automatically add spring-dependent jar packages.
We right click on our new project to find Gradle Select Refresh All, open Eclipse Luna's console view, we observed that it is downloading the relevant jar package, here do not be impatient, need to wait, when we
See "Successful", output when we can operate it. Let's first verify that the spring-related jar is loaded.
Did you build a success!
Seven, add code,
1, under the SRC Org.gradle package, the establishment of greeting class.
PackageOrg.gradle; Public classGreeting {Private Final LongID; Private FinalString content; PublicGreeting (LongID, String content) { This. ID =ID; This. Content =content; } Public LonggetId () {returnID; } PublicString getcontent () {returncontent; }}
2. Create a resource controller. It's showtime.
PackageHello;ImportJava.util.concurrent.atomic.AtomicLong;Importorg.springframework.web.bind.annotation.RequestMapping;ImportOrg.springframework.web.bind.annotation.RequestParam;ImportOrg.springframework.web.bind.annotation.RestController; @RestController Public classGreetingcontroller {Private Static FinalString template = "Hello,%s!"; Private FinalAtomiclong counter =NewAtomiclong (); @RequestMapping ("/greeting") PublicGreeting Greeting (@RequestParam (value= "name", defaultvalue= "World") (String name) {return NewGreeting (Counter.incrementandget (), String.Format (template, name)); }}
3, boot spring application start.
Package Hello; Import org.springframework.boot.SpringApplication; Import org.springframework.boot.autoconfigure.SpringBootApplication; @SpringBootApplication Public class Application { publicstaticvoid main (string[] args) { Springapplication.run (Application. class , args);} }
Eight, Gradle Build.
In the project, right-click to locate Gradle check Gradle build in the task parameter in the Select Build. Build, after successful build.
Find the application we created in the project, right-click to select "Run"-"Java application" Next is the moment to witness the miracle:
When you see this output, your app builds successfully and reads the output carefully:
Tomact has been started, the port is 8080, if the port is accounted for, the port occupied by the application is turned off.
Nine, the browser to see the effect.
Ten, welcome the technical exchange discussion. Reference Material: http://spring.io/guides/gs/rest-service/
Spring Boot builds a rest-based web service