Restful is a concept, Rest is an architecture, the only instance of its binding is http.
关于restful:https://www.runoob.com/w3cnote/restful-architecture.html
Content comes from the spring's official website: https://spring.io/guides/gs/rest-service/
使用 Spring/SpringBoot/Maven/Tomcat
1. Use Maven project build IDEA
After you've created to create a package hello in the src / main / java
Directory structure:
└── src
└── main
└── java
└── hello
Configuration pom.xml
-
<?xml version="1.0" encoding="UTF-8"?> <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd"> <modelVersion>4.0.0</modelVersion> <groupId>lsl</groupId> <artifactId>RestfulWebServiceHelloworld</artifactId> <version>1.0-SNAPSHOT</version> <parent> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-parent</artifactId> <version>2.1.6.RELEASE</version> </parent> <dependencies> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-test</artifactId> <scope>test</scope> </dependency> <dependency> <groupId>com.jayway.jsonpath</groupId> <artifactId>json-path</artifactId> <scope>test</scope> </dependency> </dependencies> <properties> <java.version>1.8</java.version> </properties> <build> <plugins> <plugin> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-maven-plugin</artifactId> </plugin> </plugins> </build> </project>
To configure groupId which according to their settings and artifactId
About Plug-ins, the official website described as such:
The Spring Boot Maven plugin provides many convenient features:
-
It collects all the jars on the classpath and builds a single, runnable "über-jar", which makes it more convenient to execute and transport your service.
-
It searches for the
public static void main()
method to flag as a runnable class. -
It provides a built-in dependency resolver that sets the version number to match Spring Boot dependencies. You can override any version you wish, but it will default to Boot’s chosen set of versions.
2. Write code in the package hello:
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; } }
Now as a web server, you must have data transmission and data transmission format, used here to convert json object instance to json format and transmitted.
Specific process is: 1. get the specific format to access the site resources, such as http: // localhost: 8080 / greeting name = User?
2. Back format by parsing the content corresponding to the request, such as the object instance corresponding Greeting
automatically using the spring jackson JSON object into json format, very convenient.
Spring uses the Jackson JSON library to automatically marshal instances of type Greeting
into JSON.
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; @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)); } }
Wherein the annotation is mapped to the http request to ensure @RequestMapping method Greeting ()
The @RequestMapping
annotation ensures that HTTP requests to /greeting
are mapped to the greeting()
method.
@RequestMapping default mapping all annotations http request, it can be confirmed by a fixed parameter request @RequestMapping (method = GET) this method
@RequestMapping
maps all HTTP operations by default. Use @RequestMapping(method=GET)
to narrow this mapping.
Notes @RequestParam
the request query value (variable) and specific parameters variable bindings,? Name = to match the time when the User access to resources in this way value = name of the case and, therefore, the local method specific parameters assigned User name, in other words, the visitor know that the server has a name such parameters and want to assign to this parameter, then use? name = User in this way, the server know that name is transferred came in, carry out specific assignments and returns the corresponding specific data (objects). If you do not use the default value.
@RequestParam
binds the value of the query string parameter name
into the name
parameter of the greeting()
method. If the name
parameter is absent in the request, the defaultValue
of "World" is used.
Annotations @RestController table returns Specific examples of such domain (instance object) as a control instead of a single view
The traditional mechanism MVC return a single fixed view (view), and Restful Web Service Controller (Controller) can be dynamically (based on demand) returns the corresponding page
Greeting class must be transmission in JSON format, spring will automatically perform this conversion.
3. To make the program executable
src/main/java/hello/Application.java
-
package hello; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; //注解@SpringBootApplication包含许多其他注解,无需配置xml文件即可自动配置获取classpath,bean,执行main() @SpringBootApplication public class Application { public static void main(String[] args) { //SpringApplication.run(Application.class, args)该方法自动运行应用程序,无需配置web.xml或其他xml文件 //该应用是100%纯java应用程序,SpringBoot自动帮你解决许多配置问题 SpringApplication.run(Application.class, args); } }
Deploying the application to build the jar file or war files will skip it here, the details of self-examination
Direct execution Application, the following interface
. ____ _ __ _ _
/\\ / ___'_ __ _ _(_)_ __ __ _ \ \ \ \
( ( )\___ | '_ | '_| | '_ \/ _` | \ \ \ \
\\/ ___)| |_)| | | | | || (_| | ) ) ) )
' |____| .__|_| |_|_| |_\__, | / / / /
=========|_|==============|___/=/_/_/_/
:: Spring Boot :: (v2.1.6.RELEASE)
2019-06-26 17:25:09.098 INFO 460 --- [ main] hello.Application : Starting Application on DESKTOP-DF9BL8S with PID 460 (started by lsl in H:\练习\java\javaProject\RestfulWebServiceHelloWorld)
2019-06-26 17:25:09.114 INFO 460 --- [ main] hello.Application : No active profile set, falling back to default profiles: default
2019-06-26 17:25:10.630 INFO 460 --- [ main] o.s.b.w.embedded.tomcat.TomcatWebServer : Tomcat initialized with port(s): 8080 (http)
2019-06-26 17:25:10.697 INFO 460 --- [ main] o.apache.catalina.core.StandardService : Starting service [Tomcat]
2019-06-26 17:25:10.697 INFO 460 --- [ main] org.apache.catalina.core.StandardEngine : Starting Servlet engine: [Apache Tomcat/9.0.21]
2019-06-26 17:25:10.864 INFO 460 --- [ main] o.a.c.c.C.[Tomcat].[localhost].[/] : Initializing Spring embedded WebApplicationContext
2019-06-26 17:25:10.864 INFO 460 --- [ main] o.s.web.context.ContextLoader : Root WebApplicationContext: initialization completed in 1672 ms
2019-06-26 17:25:11.082 INFO 460 --- [ main] o.s.s.concurrent.ThreadPoolTaskExecutor : Initializing ExecutorService 'applicationTaskExecutor'
2019-06-26 17:25:11.286 INFO 460 --- [ main] o.s.b.w.embedded.tomcat.TomcatWebServer : Tomcat started on port(s): 8080 (http) with context path ''
2019-06-26 17:25:11.301 INFO 460 --- [ main] hello.Application : Started Application in 3.199 seconds (JVM running for 4.314)
The success of the implementation, of course, can be seen using TomcatWebServer, so if unsuccessful should install a Tomcat
Then enter in your browser http://127.0.0.1:8080/greeting
Or http://127.0.0.1:8080/greeting?name=lsl98 etc.
Page content appears
{"id":3,"content":"Hello, lsl98!"}
Page log is Spring log, it is also very show gas.
Above, also considered one of the most basic structures of the back-end server (not deployed).