snjl

我大概率会编程。


  • 首页

  • 标签

  • 分类

  • 归档

  • 搜索

springboot:使用Swagger2构建强大的RESTful API文档

发表于 2018-12-01 | 分类于 springboot
字数统计: 1.7k 字 | 阅读时长 ≈ 6 分钟

项目地址:https://github.com/snjl/springboot.swagger2.git

由于Spring Boot能够快速开发、便捷部署等特性,相信有很大一部分Spring Boot的用户会用来构建RESTful API。而我们构建RESTful API的目的通常都是由于多终端的原因,这些终端会共用很多底层业务逻辑,因此我们会抽象出这样一层来同时服务于多个移动端或者Web前端。

这样一来,我们的RESTful API就有可能要面对多个开发人员或多个开发团队:IOS开发、Android开发或是Web开发等。为了减少与其他团队平时开发期间的频繁沟通成本,传统做法我们会创建一份RESTful API文档来记录所有接口细节,然而这样的做法有以下几个问题:

由于接口众多,并且细节复杂(需要考虑不同的HTTP请求类型、HTTP头部信息、HTTP请求内容等),高质量地创建这份文档本身就是件非常吃力的事,下游的抱怨声不绝于耳。
随着时间推移,不断修改接口实现的时候都必须同步修改接口文档,而文档与代码又处于两个不同的媒介,除非有严格的管理机制,不然很容易导致不一致现象。

阅读全文 »

springboot:数据存储篇-SQL关系型数据库之MyBatis的使用

发表于 2018-12-01 | 分类于 springboot
字数统计: 2.9k 字 | 阅读时长 ≈ 15 分钟

项目地址:https://github.com/snjl/springboot.mybatis.git

添加依赖

这里需要添加mybatis-spring-boot-starter依赖跟mysql依赖:

1
2
3
4
5
6
7
8
9
10
11
 <!--最新版本,匹配spring Boot1.5 or higher-->
<dependency>
<groupId>org.mybatis.spring.boot</groupId>
<artifactId>mybatis-spring-boot-starter</artifactId>
<version>1.3.0</version>
</dependency>

<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
</dependency>

这里不引入spring-boot-starter-jdbc依赖,是由于mybatis-spring-boot-starter中已经包含了此依赖。

阅读全文 »

springboot:默认日志logback配置解析

发表于 2018-11-30 | 分类于 springboot
字数统计: 1.7k 字 | 阅读时长 ≈ 7 分钟

项目地址:https://github.com/snjl/springboot.log.git

Spring Boot在所有内部日志中使用Commons Logging,但是默认配置也提供了对常用日志的支持,如:Java Util Logging,Log4J, Log4J2和Logback。每种Logger都可以通过配置使用控制台或者文件输出日志内容。

默认日志Logback

SLF4J——Simple Logging Facade For Java,它是一个针对于各类Java日志框架的统一Facade抽象。Java日志框架众多——常用的有java.util.logging, log4j, logback,commons-logging, Spring框架使用的是Jakarta Commons Logging API (JCL)。而SLF4J定义了统一的日志抽象接口,而真正的日志实现则是在运行时决定的——它提供了各类日志框架的binding。

Logback是log4j框架的作者开发的新一代日志框架,它效率更高、能够适应诸多的运行环境,同时天然支持SLF4J。

默认情况下,Spring Boot会用Logback来记录日志,并用INFO级别输出到控制台。

日志输出内容元素具体如下:

  • 时间日期:精确到毫秒
  • 日志级别:ERROR, WARN, INFO, DEBUG or TRACE
  • 进程ID
  • 分隔符:— 标识实际日志的开始
  • 线程名:方括号括起来(可能会截断控制台输出)
  • Logger名:通常使用源代码的类名
  • 日志内容
阅读全文 »

springboot:静态资源和拦截器处理

发表于 2018-11-30 | 分类于 springboot
字数统计: 1.8k 字 | 阅读时长 ≈ 8 分钟

项目地址:https://github.com/snjl/jdbcTemplete.dudu.static.git

前面章节我们也有简单介绍过SpringBoot中对静态资源的默认支持,今天详细的来介绍下默认的支持,以及自定义扩展如何实现。

默认资源映射

Spring Boot 默认为我们提供了静态资源处理,使用 WebMvcAutoConfiguration 中的配置各种属性。
建议大家使用Spring Boot的默认配置方式,提供的静态资源映射如下:

  • classpath:/META-INF/resources
  • classpath:/resources
  • classpath:/static
  • classpath:/public

    阅读全文 »

springboot:spring task定时任务

发表于 2018-11-30 | 分类于 springboot
字数统计: 356 字 | 阅读时长 ≈ 2 分钟

项目地址:https://github.com/snjl/springboot.springtask.git

创建一个项目,在DemoApplication上加上注解@EnableScheduling:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
package com.example.demo;

import com.example.demo.task.Task;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.scheduling.annotation.EnableScheduling;

@SpringBootApplication
@EnableScheduling
public class DemoApplication {
@Autowired
private Task task;

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

在demo包里创建Task.java:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
package com.example.demo.task;

import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

@Component
public class Task {
private int count=0;

@Scheduled(cron="*/6 * * * * ?")
private void process(){
System.out.println("[" + Thread.currentThread().getName() + "]" + "this is scheduler task runing "+(count++));
}

public int getCount() {
return count;
}

public void setCount(int count) {
this.count = count;
}
}

使用@Scheduled注解,就会定时执行。

log日志:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
[scheduling-1]this is scheduler task runing  0
[scheduling-1]this is scheduler task runing 1
[scheduling-1]this is scheduler task runing 2
[scheduling-1]this is scheduler task runing 3
[scheduling-1]this is scheduler task runing 4
[scheduling-1]this is scheduler task runing 5
[scheduling-1]this is scheduler task runing 6
[scheduling-1]this is scheduler task runing 7
[scheduling-1]this is scheduler task runing 8
[scheduling-1]this is scheduler task runing 9
[scheduling-1]this is scheduler task runing 10
[scheduling-1]this is scheduler task runing 11
[scheduling-1]this is scheduler task runing 12
[scheduling-1]this is scheduler task runing 13
[scheduling-1]this is scheduler task runing 14
[scheduling-1]this is scheduler task runing 15
[scheduling-1]this is scheduler task runing 16
[scheduling-1]this is scheduler task runing 17
[scheduling-1]this is scheduler task runing 18
[scheduling-1]this is scheduler task runing 19
[scheduling-1]this is scheduler task runing 20
[scheduling-1]this is scheduler task runing 21

不需要加入其它配置文件和注解。

spring task定时任务(一)

发表于 2018-11-30 | 分类于 spring
字数统计: 430 字 | 阅读时长 ≈ 2 分钟

项目地址:https://github.com/snjl/spring.springtask.git

SprngTask没有专门的包,其核心类位于spring-context包中。所以引入spring的核心包此功能即可使用。

  在实际的项目中,我们经常将job作为action层,在job中注入service去操作底层的dao,或者定时的向其他系统拉取数据,再或者向其他系统推送数据。

阅读全文 »

springboot使用JdbcTemplate(二)

发表于 2018-11-29 | 分类于 springboot
字数统计: 2.8k 字 | 阅读时长 ≈ 14 分钟

参考博客 http://tengj.top/2017/04/13/springboot8/

项目地址:https://github.com/snjl/springboot.jdbcTemplete.git

添加依赖

1
2
3
4
5
6
7
8
9
在pom.xml里添加spring-boot-starter-jdbc依赖跟mysql依赖:
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-jdbc</artifactId>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
</dependency>
阅读全文 »

springboot使用thymeleaf开发web应用

发表于 2018-11-29 | 分类于 springboot
字数统计: 2.3k 字 | 阅读时长 ≈ 10 分钟

项目地址:https://github.com/snjl/springboot.thymeleaf_demo.git

Spring Web MVC

Spring Web MVC框架(通常简称为”Spring MVC”)是一个富”模型,视图,控制器”的web框架。

Spring MVC允许你创建特定的@Controller或@RestController beans来处理传入的HTTP请求。

阅读全文 »

requests包的异常处理

发表于 2018-11-26 | 分类于 python , 爬虫
字数统计: 197 字 | 阅读时长 ≈ 1 分钟

可以使用raise_for_status()来获取所有错误,并且在except语句中使用 requests.RequestException来得到错误原因:

def get_bs_obj(link):
    try:
        response = requests.get(link, headers=headers, timeout=10)
        response.raise_for_status()
        bs_obj = bs(response.text)
        return bs_obj
    except requests.RequestException as e:
        print(e)
        return None

例如会产生报错:

HTTPConnectionPool(host='synthezise.christuniversity.in', port=80): Max retries exceeded with url: / (Caused by NewConnectionError('<urllib3.connection.HTTPConnection object at 0x0000021B9CB8A0F0>: Failed to establish a new connection: [Errno 11001] getaddrinfo failed',))

HTTPConnectionPool(host='icu2018cls.umk.edu.my', port=80): Max retries exceeded with url: / (Caused by NewConnectionError('<urllib3.connection.HTTPConnection object at 0x0000021B9CB8ACF8>: Failed to establish a new connection: [Errno 11001] getaddrinfo failed',))

HTTPSConnectionPool(host='icsah.eu', port=443): Max retries exceeded with url: /events (Caused by SSLError(SSLError("bad handshake: SysCallError(-1, 'Unexpected EOF')",),))

HTTPSConnectionPool(host='icsah.eu', port=443): Max retries exceeded with url: /events (Caused by SSLError(SSLError("bad handshake: SysCallError(-1, 'Unexpected EOF')",),))

numpy常用函数

发表于 2018-11-26 | 分类于 python
字数统计: 455 字 | 阅读时长 ≈ 2 分钟

np.linspace(start, stop, num=50, endpoint=True, retstep=False, dtype=None)

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
start标量
序列的起始值。
stop标量
除非`endpoint`设置为False,否则序列的结束值。
在这种情况下,序列由除``num + 1``的最后一个组成
均匀间隔的样本,以便排除`stop`。注意这一步
当`endpoint`为False时,大小会改变。

num:int,可选
要生成的样本数。默认值为50.必须为非负数。

endpoint:bool,可选
如果为True,则`stop`是最后一个样本。否则,它不包括在内。
默认为True。

retstep:bool,可选
如果为True,则返回(`samples`,`step`),其中`step`是间距
样本之间。

dtype:dtype,可选
输出数组的类型。如果没有给出`dtype`,推断数据
从其他输入参数中键入。
阅读全文 »
1…161718…21
snjl

snjl

越过山丘,才发现无人等候。

203 日志
44 分类
107 标签
RSS
GitHub E-Mail Weibo
© 2019 snjl
总访问量次 | 总访客人 |
由 Hexo 强力驱动
|
主题 — NexT.Mist v5.1.4