spring-boot项目实战开发手册
第一节–hello spring-boot
Author:瑾墨
相关工具的使用:
Maven 配置相关jar包。
Idea 2018
浏览器:Google 浏览器
pom.xml 配置
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>1.4.3.RELEASE</version>
</parent>
<dependencies>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.11</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
</dependencies>
通过maven配置好相关的 spring-boot的jar包
Application.java
关于Application.java本地直接运行spring-boot,设置主方法main
package com.jinmo.web;
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);
}
}
HelloWorldController.java
package com.jinmo.web;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;
@RestController
@RequestMapping("/hellow")
public class HelloWorldController {
@RequestMapping(value = "/{name}", method = RequestMethod.GET)
public String sayWorld(@PathVariable("name") String name) {
return "Hello Spring-Boot :" + name;
}
}
相关分析:
@RestController:
@RestController注解相当于@ResponseBody + @Controller合在一起的作用。
@RequestMapping(“/hellow”)
代表要跳转的Action为hellow
@RequestMapping(value = “/{name}”, method = RequestMethod.GET)
value的值为/{name}
而name是 return “Hello Spring-Boot :” + name;
即返回的method
结果:
浏览器运行:http://localhost:8080/hellow/jinmo
返回页面:Hello Spring-Boot :jinmo
Hellow为Action事件,jinmo为method的传递值。
<center style="display:none !important;visibility:collapse !important;height:0 !important;white-space:nowrap;width:100%;overflow:hidden">%23spring-boot%u9879%u76EE%u5B9E%u6218%u5F00%u53D1%u624B%u518C%0A---%0A%23%23%u7B2C%u4E00%u8282--hello%20spring-boot%0AAuthor%uFF1A%u747E%u58A8%0A%0A%3E%20%u76F8%u5173%u5DE5%u5177%u7684%u4F7F%u7528%uFF1A%0A%3E%20Maven%20%u914D%u7F6E%u76F8%u5173jar%u5305%u3002%0A%3E%20Idea%202018%20%0A%3E%20%u6D4F%u89C8%u5668%uFF1AGoogle%20%u6D4F%u89C8%u5668%0A%0A%23%23pom.xml%20%u914D%u7F6E%0A%60%60%60xml%0A%20%20%3Cparent%3E%0A%20%20%20%20%3CgroupId%3Eorg.springframework.boot%3C/groupId%3E%0A%20%20%20%20%3CartifactId%3Espring-boot-starter-parent%3C/artifactId%3E%0A%20%20%20%20%3Cversion%3E1.4.3.RELEASE%3C/version%3E%0A%20%20%3C/parent%3E%0A%0A%20%20%20%20%3C%21--Junit%u5355%u5143%u6D4B%u8BD5--%3E%0A%20%20%3Cdependencies%3E%0A%20%20%20%20%3Cdependency%3E%0A%20%20%20%20%20%20%3CgroupId%3Ejunit%3C/groupId%3E%0A%20%20%20%20%20%20%3CartifactId%3Ejunit%3C/artifactId%3E%0A%20%20%20%20%20%20%3Cversion%3E4.11%3C/version%3E%0A%20%20%20%20%20%20%3Cscope%3Etest%3C/scope%3E%0A%20%20%20%20%3C/dependency%3E%0A%20%20%20%20%20%20%20%20%3C%21--spring-boot%u6838%u5FC3%u7EC4%u4EF6--%3E%0A%20%20%20%20%3Cdependency%3E%0A%20%20%20%20%20%20%3CgroupId%3Eorg.springframework.boot%3C/groupId%3E%0A%20%20%20%20%20%20%3CartifactId%3Espring-boot-starter-web%3C/artifactId%3E%0A%20%20%20%20%3C/dependency%3E%0A%20%20%3C/dependencies%3E%0A%20%20%0A%60%60%60%0A%u901A%u8FC7maven%u914D%u7F6E%u597D%u76F8%u5173%u7684%20spring-boot%u7684jar%u5305%0A%0A%0A%23%23Application.java%0A%u5173%u4E8EApplication.java%u672C%u5730%u76F4%u63A5%u8FD0%u884Cspring-boot%uFF0C%u8BBE%u7F6E%u4E3B%u65B9%u6CD5main%0A%0A%60%60%60java%0Apackage%20com.jinmo.web%3B%0A%0Aimport%20org.springframework.boot.SpringApplication%3B%0Aimport%20org.springframework.boot.autoconfigure.SpringBootApplication%3B%0A%0ASpringBootApplication%0Apublic%20class%20Application%20%7B%0A%20%20%20%20public%20static%20void%20main%28String%5B%5D%20args%29%20%7B%0A%20%20%20%20%20%20%20%20SpringApplication.run%28Application.class%2C%20args%29%3B%0A%20%20%20%20%7D%0A%7D%0A%60%60%60%0A%0A%23%23HelloWorldController.java%0A%60%60%60java%0Apackage%20com.jinmo.web%3B%0A%0Aimport%20org.springframework.web.bind.annotation.PathVariable%3B%0Aimport%20org.springframework.web.bind.annotation.RequestMapping%3B%0Aimport%20org.springframework.web.bind.annotation.RequestMethod%3B%0Aimport%20org.springframework.web.bind.annotation.RestController%3B%0A%0A@RestController%0A@RequestMapping%28%22/hellow%22%29%0Apublic%20class%20HelloWorldController%20%7B%0A%0A%20%20%20%20@RequestMapping%28value%20%3D%20%22/%7Bname%7D%22%2C%20method%20%3D%20RequestMethod.GET%29%0A%20%20%20%20public%20String%20sayWorld%28@PathVariable%28%22name%22%29%20String%20name%29%20%7B%0A%20%20%20%20%20%20%20return%20%22Hello%20Spring-Boot%20%uFF1A%22%20+%20name%3B%0A%20%20%20%20%7D%0A%7D%0A%60%60%60%0A%23%23%u76F8%u5173%u5206%u6790%uFF1A%0A%0A%3D%3D@RestController%3D%3D%uFF1A%0A%3E%20%3D%3D@RestController%3D%3D%u6CE8%u89E3%u76F8%u5F53%u4E8E%3D%3D@ResponseBody%3D%3D%20%uFF0B%20%3D%3D@Controller%3D%3D%u5408%u5728%u4E00%u8D77%u7684%u4F5C%u7528%u3002%0A%0A%3D%3D@RequestMapping%28%22/hellow%22%29%3D%3D%0A%u4EE3%u8868%u8981%u8DF3%u8F6C%u7684Action%u4E3Ahellow%0A%0A%3D%3D@RequestMapping%28value%20%3D%20%22/%7Bname%7D%22%2C%20method%20%3D%20RequestMethod.GET%29%3D%3D%0Avalue%u7684%u503C%u4E3A/%7Bname%7D%0A%u800Cname%u662F%20return%20%22Hello%20Spring-Boot%20%uFF1A%22%20+%20name%3B%0A%u5373%u8FD4%u56DE%u7684method%0A%0A%23%23%u7ED3%u679C%uFF1A%0A%u6D4F%u89C8%u5668%u8FD0%u884C%uFF1Ahttp%3A//localhost%3A8080/hellow/jinmo%0A%u8FD4%u56DE%u9875%u9762%uFF1AHello%20Spring-Boot%20%uFF1Ajinmo%0A%0AHellow%u4E3AAction%u4E8B%u4EF6%uFF0Cjinmo%u4E3Amethod%u7684%u4F20%u9012%u503C%u3002</center>