- 示例:在classpath下创建application.properties文件
server.display-name=xmsApp
server.servlet-path=/
server.port=80
=============================================
=========大家好,我是xms======================
=========这是我的第一个Springboot程序============
==============================================
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-devtools</artifactId>
<optional>true</optional>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
<configuration>
<fork>true</fork>
</configuration>
</plugin>
</plugins>
</build>
- 开启热部署之后修改代码,会自动重启容器,但是重启容器的时间会大大减短,因为热部署只会重新加载被修改的文件.
- 在控制器中使用@RestController注解配置,
- @RequestMapping(value="/userid/{id}")注解定义了方法的映射路径,其中"{id}"为从路径上获取的内容,并将{id}获取到的内容,赋予方法的参数 id.
- 对应的方法参数需要使用"@PathVariable "标明.
- 示例:
@RequestMapping("/hello/{info}")
public String echo(@PathVariable String info){
return "echo:"+info;
}
- 该方法将路径中{info}部分获取到并赋予方法参数"info"
- 测试路径:
http://localhost/hello/rest风格路径测试
- 此时会将/hello/路径后面部分的内容获取到,并传到方法之中,然后返回到页面之中.
- 在类上使用@RestController注解表明该类使用Rest风格的url路径,并且所有方法均返回JSON数据.
- 在类上使用@RequestMapping注解来定义访问该类的url映射路径为"/Student".
- 如果要访问该类中的映射方法,则需要加上类上的映射路径,例如getStudent()方法上的@RequestMapping注解设置的映射路径为"/getStudent",该方法所属类的映射路径为"/Student",那么要访问getStudent()方法的url则为
ip地址:端口号/Student/getStudent
package app.controllers;
import app.entity.Student;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
@RestController
@RequestMapping(value="/Student")
public class StudentController {
@RequestMapping("/getStudent/name={name}")
public Student getStudent(@PathVariable String name){
Student student = new Student();
student.setName(name);
student.setSid(java.util.UUID.randomUUID().toString());
return student;
}
}
http://localhost/Student/getStudent/xms/22