1:常用的注释
@RequestParam
@RestController
public class HelloController {
@GetMapping("hello")
public String hello(@RequestParam("nickname") String name)
{
System.out.println(name);
return "dasda";
}
}
当网址传输数据给后端时方法里面的参数要和网址上的参数名要保持一致
但加上@RequestParam()注释就可随意变更参数名
@SpringBootApplication 它可以用于标记主应用程序类,并指示Spring Boot启动和配置该应用程序。
@RestController 请求数据 返回的是文本数据 默认转换为json格式
@Controller 请求页面数据 返回的是页面 .html
@GetMappering
@PutMappering
@postMappering
@DeleteMappering
@RequestMapping("");
@Override
告诉编译器某个方法是一个重写父类中的方法,编译器会检查该方法是否正确地重写了父类中的方法,如果没有正确地重写,则会产生编译错误。
@Configuration 当一个类被其注解标记时,Spring容器会将其识别为一个配置类,并在初始化应用程序的上下文时使用该类
@Bean
是一个注解,用于指示一个方法或者一个构造函数将会产生一个Spring管理的Bean对象。它通常用于@Configuration类中的方法,用于定义和配置Bean。
@RequestBody:用于从请求体中获取JSON类型的数据。示例代码:
@PostMapping("/example")
public String example(@RequestBody DataModel data) {
// 处理data的逻辑
return "response";
}
@Configuration:用于标识一个类为配置类,它通常用于定义Bean、配置相关的实例等。示例代码:
@Configuration
public class AppConfig {
// 其他配置和Bean的定义
}
2:常用的配置
1:修改静态文件的访问路径
spring.mvc.static-path-pattern=/static/**
spring.web.resources.static-locations=classpath:/css
/static/** 可改为自己在前端想要的路径
2:修改一次响应和单个文件传输的最大大小
spring.servlet.multipart.max-request-size=10MB
spring.servlet.multipart.max-file-size=10MB
3:接收网页文件保存到本地
@PostMapping("/upload")
public String up(@RequestParam("nickname") String nickname, MultipartFile f, HttpServletRequest request) throws IOException
{
//HttpServletRequest 网页自动返回的一个参数 可以用于获取请求的URL、请求方法、请求头、请求参数等信息
//getServletContext() 方法返回与当前请求关联的ServletContext对象,该对象代表整个web应用程序的上下文。
//getRealPath("/upload/") 方法接受相对于web应用程序根目录的路径参数,并返回该路径在服务器文件系统中的实际物理路径。
String path=request.getServletContext().getRealPath("/upload/");
saveFile(f,path);
System.out.println(path);
return "传输成功";
}
private void saveFile(MultipartFile f, String path) throws IOException {
File dir =new File(path);
if(!dir.exists())dir.mkdir();
File file =new File(path+f.getOriginalFilename());
f.transferTo(file);
}
}
在apipost里面发送请求
注意红框里面
4:拦截器
1:在com/example/demo/interceptor包下新建LoginInterceptor.java文件生成拦截器,可自己写判断语句,返回true为能正常访问网址
public class LoginInterceptor implements HandlerInterceptor{
@Override
public boolean preHandle (HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception
{
System.out.println("dgg");
return false;
}
}
2:在com/example/demo/config包下新建WebConfig.java文件添加拦截器,自己设置拦截路径
@Configuration
public class WebConfig implements WebMvcConfigurer {
@Override
public void addInterceptors(InterceptorRegistry registry)
{
registry.addInterceptor(new LoginInterceptor()).addPathPatterns("/hello/**");
}
}
拦截器是在网页生成前就已经运行
5:使用Swagger 生成Web Api 文档
1:在pom.xml文件内插入
<dependency>
<groupId>io.springfox</groupId>
<artifactId>springfox-swagger2</artifactId>
<version>2.9.2</version>
</dependency>
<dependency>
<groupId>io.springfox</groupId>
<artifactId>springfox-swagger-ui</artifactId>
<version>2.9.2</version>
</dependency>
如果version里面标红就多等一会儿,多重启几次
2:然后在com/example/demo/config包下创建SwaggerConfig.java 文件
@Configuration //告诉Spring 这是一个配置类
@EnableSwagger2 //启用Swagger2功能
public class SwaggerConfig {
@Bean
public Docket createRestApi(){
return new Docket(DocumentationType.SWAGGER_2)
.groupName("测试")
.apiInfo(apiInfo())
.select()
.apis(RequestHandlerSelectors.basePackage("com"))//com包下的所有API都交给Swagger2
.paths(PathSelectors.any()).build();
}
private ApiInfo apiInfo()
{
return new ApiInfoBuilder()
.title("演示项目API")//标题
.description("演示项目")//描述
.version("1.0")//版本
.build();
}
}
如果显示 404 则说明有其他类继承了WebMvcConfigurationSupport,在yml中配置的相关内容会失效。 需要重新指定静态资源
记得加 spring.mvc.pathmatch.matching-strategy=ANT_PATH_MATCHER
在继承WebMvcConfigurationSupport 的类里面添加
@Override
public void addResourceHandlers(ResourceHandlerRegistry registry) {
registry.addResourceHandler("/**").addResourceLocations(
"classpath:/static/");
registry.addResourceHandler("swagger-ui.html").addResourceLocations(
"classpath:/META-INF/resources/");
registry.addResourceHandler("/webjars/**").addResourceLocations(
"classpath:/META-INF/resources/webjars/");
WebMvcConfigurer.super.addResourceHandlers(registry);
}
点击端口http://localhost:8080/swagger-ui.html#/
成功页面