从0开始SpringBoot - 2: RESTful Web Service

65 阅读2分钟

RESTful Web Service

构建一个RESTful Web Service,SpringBoot 简化了配置 web.xml等步骤,通过 @SpringBootApplication 即可实现

web Service 启动

@SpringBootApplication 
public class Application { 
    
    public static void main(String[] args) { 
        SpringApplication.run(Application.class, args); 
    } 
}

@SpringBootApplication is a convenience annotation that adds all of the following:

  • @Configuration: Tags the class as a source of bean definitions for the application context.
  • @EnableAutoConfiguration: Tells Spring Boot to start adding beans based on classpath settings, other beans, and various property settings. For example, if spring-webmvc is on the classpath, this annotation flags the application as a web application and activates key behaviors, such as setting up a DispatcherServlet.
  • @ComponentScan: Tells Spring to look for other components, configurations, and services in the com/example package, letting it find the controllers.

The main() method uses Spring Boot’s SpringApplication.run() method to launch an application. Did you notice that there was not a single line of XML? There is no web.xml file, either. This web application is 100% pure Java and you did not have to deal with configuring any plumbing or infrastructure.

这里需要注意 @ComponentScan

image.png

默认情况下,ComponentScan只扫描SpringBootApplication所在目录(及其子目录),所以要加上配置

@Configuration
@ComponentScan("com.example.user")
public class AppConfig {
    
}

image.png

ComponentScan的配置方案:

  • 添加AppConfig
  • SpringBootApplication 放跟目录
  • AppConfig 配置 根目录.*

Controller Annotation

Spring MVC provides an annotation-based programming model where @Controller and @RestController components use annotations to express request mappings, request input, exception handling, and more. Annotated controllers have flexible method signatures and do not have to extend base classes nor implement specific interfaces. The following example shows a controller defined by annotations:

Annotation含义
@Controllerhandle mapping
@RestController@Controller and @ResponseBody returning data rather than a view.

Request Annotation

Annotation请求类型
@GetMappingGET 请求
@PostMappingPOST 请求
@PutMappingPUT 请求
@DeleteMappingDELETE 请求

Param Annotation

Annotation含义
@RequestParam绑定传递过来的参数
@PostMapping("/registerUser")
public User register(@RequestParam(value = "name") String name, @RequestParam(value = "age") Integer age) {
    User user = new User(10086,name,age,"man");
    return user;
}

混合参数

例如注册时,上传图片+用户名的场景

@PostMapping("/registerUser")
public User register(@RequestParam(value = "name") String name, @RequestParam(value = "age") Integer age,@RequestParam(value = "image") MultipartFile file) {
    // 处理文件和其他参数
    User user = new User(10086,name,age,"man");
    return user;
}

image.png

返回 视图

这里不使用 @RestController

@Controller
public class HomePageController {

    @GetMapping("/home")
    public String home() {
        System.out.println("request home ");
        return "home.html";
    }
}

这里使用了 thymeleaf,当然可以选择其他方案

<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-thymeleaf</artifactId>
    <version>2.3.3.RELEASE</version>
</dependency>