Spring Boot 2.X 实战教程(14)开发Web应用程序
14.开发Web应用程序
Spring Boot非常适合Web应用程序开发。您可以使用嵌入式Tomcat,Jetty,Undertow或Netty创建自包含的HTTP服务器。大多数Web应用程序使用该spring-boot-starter-web模块快速启动和运行。
14.1“Spring Web MVC框架”
Spring Web MVC框架是一个丰富的“模型视图控制器” Web框架。Spring MVC允许您创建特殊@Controller或@RestControllerbean来处理传入的HTTP请求。控制器中的方法通过使用@RequestMapping注释映射到HTTP 。
以下代码显示了@RestController为JSON数据提供服务的典型代码:
l User.java
package com.example.demo.user;
/**
* 用户实体类
*
* @author 大强
*
*/
public class User {
private Long userId;
private String username;
private String password;
public Long getUserId() {
return userId;
}
public void setUserId(Long userId) {
this.userId = userId;
}
public String getUsername() {
return username;
}
public void setUsername(String username) {
this.username = username;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
}
l Customer.java
package com.example.demo.customer;
/**
* 客户实体类
*
* @author 大强
*
*/
public class Customer {
private Long customerId;
private String customerName;
private String customerSex;
private String customerAge;
public Long getCustomerId() {
return customerId;
}
public void setCustomerId(Long customerId) {
this.customerId = customerId;
}
public String getCustomerName() {
return customerName;
}
public void setCustomerName(String customerName) {
this.customerName = customerName;
}
public String getCustomerSex() {
return customerSex;
}
public void setCustomerSex(String customerSex) {
this.customerSex = customerSex;
}
public String getCustomerAge() {
return customerAge;
}
public void setCustomerAge(String customerAge) {
this.customerAge = customerAge;
}
}
UserController.java
package com.example.demo.user;
import java.util.ArrayList;
import java.util.List;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;
import com.example.demo.customer.Customer;
/**
* 用户控制器
*
* @author 大强
*
*/
@RestController
@RequestMapping(value = "/users")
public class UserController {
/**
* 根据主键查询单个用户
*
* @param userId
* @return
*/
@RequestMapping(value = "/{userId}", method = RequestMethod.GET)
public User getUser(@PathVariable Long userId) {
User user = new User();
user.setUserId(userId);
user.setUsername("大强");
user.setPassword("123456");
return user;
}
/**
* 根据用户主键查询条件客户列表
*
* @param userId
* @return
*/
@RequestMapping(value = "/{userId}/customers", method = RequestMethod.GET)
List<Customer> getUserCustomers(@PathVariable Long userId) {
List<Customer> list = new ArrayList<Customer>();
Customer customer1 = new Customer();
customer1.setCustomerId(Long.valueOf(1));
customer1.setCustomerName("张三");
customer1.setCustomerSex("男");
customer1.setCustomerAge("20");
list.add(customer1);
Customer customer2 = new Customer();
customer2.setCustomerId(Long.valueOf(2));
customer2.setCustomerName("李四");
customer2.setCustomerSex("女");
customer2.setCustomerAge("18");
list.add(customer2);
return list;
}
/**
* 新增用户
*
* @param user
* @return
*/
@RequestMapping(value = "/", method = RequestMethod.POST)
public User addUser(@RequestBody User user) {
user.setUserId(Long.valueOf(1));
user.setUsername("大强");
user.setPassword("123456");
return user;
}
/**
* 修改用户
*
* @param user
* @return
*/
@RequestMapping(value = "/", method = RequestMethod.PUT)
public User updateUser(@RequestBody User user) {
user.setUserId(Long.valueOf(1));
user.setUsername("大强");
user.setPassword("123456");
return user;
}
/**
* 删除用户
*
* @param userId
* @return
*/
@RequestMapping(value = "/{userId}", method = RequestMethod.DELETE)
public User deleteUser(@PathVariable Long userId) {
User user = new User();
user.setUserId(Long.valueOf(1));
user.setUsername("大强");
user.setPassword("123456");
return user;
}
}
14.1.1静态内容
默认情况下,Spring Boot从类路径中的/static( /public或/resources或/META-INF/resources)目录或者根目录中提供静态内容ServletContext。它使用ResourceHttpRequestHandlerSpring MVC,以便您可以通过添加自己WebMvcConfigurer的addResourceHandlers方法来修改该行为并覆盖该 方法。
14.1.2欢迎页面
Spring Boot支持静态和模板化欢迎页面。它首先在配置的静态内容位置中查找index.html文件。如果找不到,则查找index模板。
14.1.3自定义Favicon
SpringBoot在配置的静态内容位置和类路径的根目录中查找favicon.ico(按此顺序)。
14.1.4错误处理
默认情况下,Spring Boot提供了一个/error以合理方式处理所有错误的映射,并将其注册为servlet容器中的“全局”错误页面。对于计算机客户端,它会生成一个JSON响应,其中包含错误,HTTP状态和异常消息的详细信息。对于浏览器客户端,有一个“whitelabel”错误视图,它以HTML格式呈现相同的数据(以自定义它,添加View解析后的数据error)。要完全替换默认行为,您可以实现 ErrorController并注册该类型的bean定义,或者添加类型的bean ErrorAttributes以使用现有机制但替换内容。
例如:增加以下文件/demo/src/main/resources/public/error/404.html。
如有疑问,请观看视频:https://ke.qq.com/course/428845