Zzx Blog

Since 2017


  • 首页

  • 标签

  • 分类

  • 归档

  • 公益404

axios下载文件总结

发表于 2018-08-03 | 分类于 Vue

总结关于axios下载文件所遇到的问题

  1. 下载文件通常用a标签跳转url,但是需要手动处理参数,比如加token等,参数过多时不适用
  2. axios下载通常方式为添加response拦截的方式
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    // contenttype为后端返回的type,可以自定义
    if (res.headers && (res.headers['content-type'] === 'application/x-msdownload' || res.headers['content-type'] === 'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet')) {
    downloadUrl(res.request.responseURL)
    return
    }

    // download url
    const downloadUrl = url => {
    let iframe = document.createElement('iframe')
    iframe.style.display = 'none'
    iframe.src = url
    iframe.onload = function () {
    document.body.removeChild(iframe)
    }
    document.body.appendChild(iframe)
    }
  • 这种方式通过构建iframe来跳转下载
    阅读全文 »

JavaCastException-devtools

发表于 2018-07-26 | 分类于 Java , SpringBoot

研究quartz的过程中,碰到一个bug,不过早晚也会遇到的bug.经过面向搜索引擎编程总算找到

了问题所在,再次记录一下。

  • 先上代码

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    @Override
    protected void executeInternal(JobExecutionContext context) throws JobExecutionException {
    ScheduleJobEntity scheduleJobtest=new ScheduleJobEntity();
    System.out.println(context.getMergedJobDataMap()
    .get(ScheduleJobEntity.JOB_PARAM_KEY).getClass().getClassLoader());
    System.out.println(scheduleJobtest.getClass().getClassLoader());
    if (context.getMergedJobDataMap()
    .get(ScheduleJobEntity.JOB_PARAM_KEY) instanceof ScheduleJobEntity) {
    scheduleJobtest = (ScheduleJobEntity) context.getMergedJobDataMap()
    .get(ScheduleJobEntity.JOB_PARAM_KEY);
    }
    .....省略
    } catch (Exception e) {
    logger.error("任务执行失败,任务ID:" + scheduleJob.getJobId(), e);

    //任务执行总时长
    long times = System.currentTimeMillis() - startTime;
    log.setTimes((int)times);

    //任务状态 0:成功 1:失败
    log.setStatus(1);
    log.setError(StringUtils.substring(e.toString(), 0, 2000));
    }finally {
    scheduleJobLogService.insert(log);
    }
    }
  • 上面代码中的context 包含和任务执行的entity,但是是个object,需要转换一下,一开始

  • 使用强转,运行到转换的地方就会报出JavaCastException: com.zzx.springboot.modules.job.entity.ScheduleJobEntity can not be casted to
    com.zzx.springboot.modules.job.entity.ScheduleJobEntity
  • 明明是两个一样类型的entity,但就是不能转换,经过多方查找,总算找到问题所在,
  • 原因是因为devtools的热部署导致了我的ScheduleJobEntit实体是由devtools
    的restartClassLoader加载的,导致不是原生的appclassloader
  • 解决方案
  1. 不用devtools
  2. idea安装Jrebel插件(好用的一笔)

ngrok搭建个人服务器,及使用

发表于 2018-05-15 | 分类于 Ngrok , 内网穿透

ngrok搭建个人节点,条件:

  1. 服务器一台,含公网ip(本文用的阿里云centos)
  2. 域名一个,并解析到服务器ip(最好用泛解析,本文例子:域名zzx.com, 泛解析A类地址
    “ * ngrok” 到服务器IP ),则之后ngrok用的域名即为ngrok.zzx.com

    步骤

  3. 安装git 和Golang

    1
    apt-get install build-essential golang mercurial git
  4. 下载ngrok源码,地址为ngrok源码地址

    1
    git clone https://github.com/tutumcloud/ngrok.git ngrok
  5. 到ngrok目录,然后生成自签名证书

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    cd ngrok

    NGROK_DOMAIN="ngrok.zzx.com"

    openssl genrsa -out base.key 2048

    openssl req -new -x509 -nodes -key base.key -days 10000 -subj "/CN=$NGROK_DOMAIN" -out base.pem

    openssl genrsa -out server.key 2048

    openssl req -new -key server.key -subj "/CN=$NGROK_DOMAIN" -out server.csr

    openssl x509 -req -in server.csr -CA base.pem -CAkey base.key -CAcreateserial -days 10000 -out server.crt
阅读全文 »

MyBatis Generator使用及自定义Oracle和Mysql分页插件

发表于 2018-04-26 | 分类于 Java , SpringMVC , MyBatis

用MBG插件生成Pojo,PojoExample(条件类),–Mapper(dao层接口),–Mapper.xml文件,并重写PluginAdapter类,生成自定义分页代码。

Maven加入插件,自定义分页插件依赖

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
<!--mybatis 逆向生成插件-->
<plugin>
<groupId>org.mybatis.generator</groupId>
<artifactId>mybatis-generator-maven-plugin</artifactId>
<version>1.3.5</version>
<!-- 定义配置文件 -->
<configuration>
<configurationFile>src/main/resources/generatorConfig.xml</configurationFile>
<verbose>true</verbose>
<overwrite>true</overwrite>
</configuration>
<executions>
<execution>
<id>Generate MyBatis Artifacts</id>
</execution>
</executions>
<dependencies>
<dependency>
<groupId>org.mybatis.generator</groupId>
<artifactId>mybatis-generator-core</artifactId>
<version>1.3.5</version>
</dependency>
<!-- 自定义插件 -->
<dependency>
<groupId>com.zzxcn</groupId>
<artifactId>PaginationPlugin</artifactId>
<version>1.0</version>
</dependency>
</dependencies>
</plugin>
阅读全文 »

Spring切面记录操作日志

发表于 2018-04-18 | 分类于 Java , Spring

关于Spring AOP的使用–记录系统操作日志

  1. AOP:面向切面编程(面向接口的编程(⊙﹏⊙)),通过预编译方式和运行期动态代理实现程序功能的统一维护的一种技术。
  2. 记录数据库操作日志的方法一般有三种:
  • 最原始的,每次执行操作语句后执行记录方法,不好管理,效率低
  • 拦截器:新建一个拦截器的class继承spring web的HandlerInterceptorAdapter类,重写:
  • preHandle:它会在处理方法之前执行,可以用来做一些编码处理、安全限制之类的操作。
  • postHandle:它是在方法执行后开始返回前执行,可以进行日志记录、修改ModelView之类的操作。
  • afterCompletion:最后执行,无论出错与否都会执行这个方法,可以用来记录异常信息和一些必要的操作记录。
  • afterConcurrentHandlingStarted:controller方法异步开始执行时就开始执行这个方法,而postHandle需要等到controller异步执行完成后再执行。
    • 缺点:比较麻烦,不能获取参数
  • AOP,通过加入注解@Aspect,创建切入点。加入注解@Pointcut,配置接入点(配置切入入口,模块,如Controller,Dao),然后在配置文件配置,即aspectj动态代理。

    具体实现

    阅读全文 »

SpringBoot项目部署问题

发表于 2018-04-08 | 分类于 Java , SpringBoot , Spring

SpringBoot项目部署到linux系统相关问题

  • linux tomcat日志错误Cannot run without an instance id & java.net.UnknownHostException
  • 解决方案:vi 编辑 /etc/hosts文件,将当前服务器主机名加入127.0.0.1这一行即可
  • 获取服务器服务器主机名方法:命令行输入hostname

war方式和jar方式部署

  • war方式即为发布到Tomcat之类的web服务器。
  • jar方式:
  1. pom文件配置jar
  2. maven打包,根目录运行mvn package
  3. 运行jar,java -jar **.jar

关于JavaBean-POJO

发表于 2018-03-20 | 分类于 Java

POJO实体类属性-private

  • 为了安全
  • 为什么安全?别的类可以通过直接调用对象.属性名调用或者任意赋值。
  • 用get,set可以控制某些属性只读或只写
  • POJO本身就是对外开放的接口,通过方法调用

Spring依赖注入方式

发表于 2018-03-06 | 分类于 Java , Spring

Spring依赖注入方式

  1. @Autowired与@Resource都可以用来装配bean. 都可以写在字段上,或写在setter方法上。
  2. @Autowired默认按类型装配(这个注解是属业spring的),默认情况下必须要求依赖对象必须存在,如果要允许null值,可以设置它的required属性为false,如:@Autowired(required=false) ,如果我们想使用名称装配可以结合@Qualifier注解进行使用,如下:

    1
    2
    @Autowired() @Qualifier("baseDao")    
    private BaseDao baseDao;
  3. @Resource 是JDK1.6支持的注解,默认按照名称进行装配,名称可以通过name属性进行指定,如果没有指定name属性,当注解写在字段上时,默认取字段名,按照名称查找,如果注解写在setter方法上默认取属性名进行装配。当找不到与名称匹配的bean时才按照类型进行装配。但是需要注意的是,如果name属性一旦指定,就只会按照名称进行装配。只不过注解处理器我们使用的是Spring提供的,是一样的,无所谓解耦不解耦的说法,两个在便利程度上是等同的。

    1
    2
    @Resource(name="baseDao")    
    private BaseDao baseDao;
  • 他们的主要区别就是@Autowired是默认按照类型装配的 @Resource默认是按照名称装配的byName 通过参数名 自动装配,如果一个bean的name 和另外一个bean的 property 相同,就自动装配。byType 通过参数的数据类型自动自动装配,如果一个bean的数据类型和另外一个bean的property属性的数据类型兼容,就自动装配
    阅读全文 »

SpringBoot注解

发表于 2018-03-05 | 分类于 Java , SpringBoot , Spring

##SpringBoot注解使用

  1. @SpringBootApplication
  • @SpringBootApplication = (默认属性)@Configuration + @EnableAutoConfiguration + @ComponentScan。
    1
    2
    3
    4
    5
    6
    @SpringBootApplication  
    public class ApplicationMain {
    public static void main(String[] args) {
    SpringApplication.run(Application.class, args);
    }
    }
  • @Configuration:提到@Configuration就要提到他的搭档@Bean。使用这两个注解就可以创建一个简单的spring配置类,可以用来替代相应的xml配置文件。
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    Spring配置文件
    <beans>
    <bean id = "car" class="com.test.Car">
    <property name="wheel" ref = "wheel"></property>
    </bean>
    <bean id = "wheel" class="com.test.Wheel"></bean>
    </beans>
    相当于
    @Configuration
    public class Conf {
    @Bean
    public Car car() {
    Car car = new Car();
    car.setWheel(wheel());
    return car;
    }
    @Bean
    public Wheel wheel() {
    return new Wheel();
    }
    }
阅读全文 »

Spring注解

发表于 2018-03-05 | 分类于 Java , Spring

Spring注解

  1. @Autowired
  • @Autowired 注释,它可以对类成员变量、方法及构造函数进行标注,完成自动装配的工作。 通过 @Autowired的使用来消除 set ,get方法。在使用@Autowired之前,我们对一个bean配置起属性时,是这用用的

    1
    <property name="属性名" value=" 属性值"/>
  • 具体例子:

  • UserRepository.java

    1
    2
    3
    4
    5
    6
    1 package com.proc.bean.repository;
    2
    3 public interface UserRepository {
    4
    5 void save();
    6 }
  • 这里定义了一个UserRepository接口,其中定义了一个save方法

  • UserRepositoryImps.java
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    package com.proc.bean.repository;

    import org.springframework.stereotype.Repository;

    @Repository("userRepository")
    public class UserRepositoryImps implements UserRepository{

    @Override
    public void save() {
    System.out.println("UserRepositoryImps save");
    }
    }
阅读全文 »
123
Banksys Zhuang

Banksys Zhuang

Technology

21 日志
13 分类
17 标签
微博 知乎
© 2017 — 2018 Banksys Zhuang
由 Hexo 强力驱动
|
主题 — NexT.Gemini v5.1.3