含泪踩坑日记~~记录美好生活

SpringBoot

SpringBoot国际化配置

加入spring容器时,方法名必须是localeResolver!!!

在这里插入图片描述

SpringBoot文件上传

1.form表单提交方式必须是multipart/form-data
在这里插入图片描述
2.注意name定义的名字必须跟Contoller中接收名相同
在这里插入图片描述
在这里插入图片描述

前后端访问时一定要解决跨域问题

/**
 * MVC扩展配置
 * @author Tu_Yooo
 * @Date 2021/5/25 12:35
 */
@Configuration
public class WebMvcConfig implements WebMvcConfigurer {
    
    

    /**
     * 解决跨域问题
     */
    @Override
    public void addCorsMappings(CorsRegistry registry) {
    
    
        registry.addMapping("/**")
                .allowedOrigins("*")
                .allowedMethods("GET", "HEAD", "POST", "PUT", "DELETE", "OPTIONS")
                .allowCredentials(true)
                .maxAge(3600)
                .allowedHeaders("*");
    }

    private CorsConfiguration buildConfig() {
    
    
        CorsConfiguration corsConfiguration = new CorsConfiguration();
        corsConfiguration.addAllowedOrigin("*");
        corsConfiguration.addAllowedHeader("*");
        corsConfiguration.addAllowedMethod("*");
        corsConfiguration.addExposedHeader("Authorization");
        return corsConfiguration;
    }

    @Bean
    public CorsFilter corsFilter() {
    
    
        UrlBasedCorsConfigurationSource source = new UrlBasedCorsConfigurationSource();
        source.registerCorsConfiguration("/**", buildConfig());
        return new CorsFilter(source);
    }
    
}

如果使用security还需要对security进行配置

@Configuration
@EnableWebSecurity
@EnableGlobalMethodSecurity(prePostEnabled = true)//开启注解标注 哪些方法需要鉴权
public class Securityconfig extends WebSecurityConfigurerAdapter {
    
    


    @Override
    protected void configure(HttpSecurity http) throws Exception {
    
    
        //允许跨域 关闭csrf
        http.cors().and().csrf().disable();
        
        //登录配置
        
        //禁用Session
        
        //配置拦截规则
        
        //异常处理器
    }
    
}

thymeleaf

thymeleaf转换时间戳

前台调用后台提供的时间戳转换方法即可

前端页面:

<td th:text="${#dates.format(cont.getDateTime(),'yyyy-MM-dd HH:mm:ss')}">20</td>

在实体类中添加:

    //解决thymeleaf 转换时间戳问题
    public Date getDateTime(){
    
    
        //时间戳需要 * 1000 不然会转换成1970年
        return new Date(this.created * 1000L);
    }

Shiro

登录认证执行两次才跳转首页

必须点两次按钮,后台执行两次认证才可以跳转首页
在这里插入图片描述
结果是因为在ShiroFilterFactoryBean中没有把登录路径设置为无需认证即可访问!!!
在这里插入图片描述

CSS

Element-ui

高度height设置100%,不能铺满页面

用100vh

.el-container {
    
    
        padding: 0;
        margin: 0;
        height: 100vh;
    }

Swagger

报错:
java.lang.NumberFormatException: For input string: ""
这是因为 springfox-swagger2(2.9.2) 依赖于 swagger-models(1.5.20) ;

解决:

<!--添加swagger-models依赖 -->
 <!-- springfox-swagger2 -->
        <dependency>
            <groupId>io.springfox</groupId>
            <artifactId>springfox-swagger2</artifactId>
            <version>2.9.2</version>
        </dependency>
        <!--swagger-ui -->
        <dependency>
            <groupId>io.springfox</groupId>
            <artifactId>springfox-swagger-ui</artifactId>
            <version>2.9.2</version>
        </dependency>
        <dependency>
            <groupId>io.swagger</groupId>
            <artifactId>swagger-models</artifactId>
            <version>1.5.21</version>
        </dependency>

SpringCloud NetFlix

聚合项目公用API问题

在聚合项目中编写公用的API或接口,编写完成以后一定要先Maven install

否则在项目中使用会找不到依赖
在这里插入图片描述
最好将所有项目都先install再运行

FEIGN传参MULTIPARTFILE问题解决

使用Feign进行文件上传时

步骤1:
消费方编写:

    @ApiOperation("博客文章图片上传")
    @PostMapping("/uploadfile")
    public Result saveImages(@ApiParam(value = "file",name = "文件数组",required = true) @RequestParam("image") MultipartFile image){
    
    
        return attachFeignService.saveImages(image);
    }

feign接口编写:

 @PostMapping(value = "/attach/uploadfile",consumes = {
    
    MediaType.MULTIPART_FORM_DATA_VALUE})
    public Result saveImages(@RequestPart("image") MultipartFile image);

此处注意点:

  1. 使用@RequestPart(“image”)注解
  2. 设置传参格式consumes = {MediaType.MULTIPART_FORM_DATA_VALUE}

服务提供方:

@ApiOperation("博客文章图片上传")
    @PostMapping("/uploadfile")
    public Result saveImages(@RequestPart("image") MultipartFile image){
    
    
        Attach upload = attachService.upload(image,"image");
        //记录附件信息
        upload.setCreated(LocalDateTime.now());
        attachService.save(upload);
        return Result.success(upload.getFkey());
    }

使用@RequestPart(“image”)注解

猜你喜欢

转载自blog.csdn.net/weixin_46684099/article/details/116211275