Spring + Firebird + REST。 第1部分项目配置

而不是加入


如果您要拆解某些东西,请尝试用一种清晰的语言来描述它,并找到可以阅读并发表评论的人(改写为R. Feynman,但确实如此)。
所有评论,甚至以“是的, 〜白色 ~~人 允许自己“欢迎。


目标


应用程序 -如果可能,显示有关产品进度(规模)的报告,并在企业内部通过网络分发这些数据(用于功能性);
个人 -对弹簧技术的一点了解


技术领域


  • 春季网
  • Spring JPA
  • 龙目岛
    • 胸腺
  • SpringFox Swagger(我会测试一下)
  • jaybird-jdk17版本3.0.5
  • 马文

动机气泉+火鸟


最近,为Ovsezavod运营商创建了Linux Mint的第一个客户端,并且Wine的报告显示并不总是足够的。 (其他所有功能都可以正常使用-Qt可视化-SCADA,Java SE存档)。


一些必须踩的耙子


  1. 不同版本的杰克逊依赖关系(固定),
  2. firebird未设置编码类型会导致默认值NONE,

在发布的末尾链接到git


杰克逊和所有人


不同的组件拧紧了不同版本的杰克逊,因为不舒服,有必要对其进行修复。
被团队揭露


mvn dependency:tree -Dincludes=com.fasterxml.jackson.core +- org.springframework.boot:spring-boot-starter-web:jar:2.1.0.RELEASE:compile [INFO] | \- org.springframework.boot:spring-boot-starter-json:jar:2.1.0.RELEASE:compile [INFO] | \- com.fasterxml.jackson.core:jackson-databind:jar:2.9.7:compile [INFO] \- io.springfox:springfox-swagger2:jar:2.7.0:compile [INFO] \- io.swagger:swagger-models:jar:1.5.13:compile [INFO] \- com.fasterxml.jackson.core:jackson-annotations:jar:2.8.5:compile 

我们修复pom。


  <dependency> <groupId>com.fasterxml.jackson.core</groupId> <artifactId>jackson-databind</artifactId> <version>2.9.7</version> <exclusions> <exclusion> <groupId>com.fasterxml.jackson.core</groupId> <artifactId>jackson-core</artifactId> </exclusion> <exclusion> <groupId>com.fasterxml.jackson.core</groupId> <artifactId>jackson-annotations</artifactId> </exclusion> </exclusions> </dependency> <dependency> <groupId>com.fasterxml.jackson.core</groupId> <artifactId>jackson-core</artifactId> <version>2.9.7</version> </dependency> <dependency> <groupId>com.fasterxml.jackson.core</groupId> <artifactId>jackson-annotations</artifactId> <version>2.9.7</version> </dependency> 

已更正,约。 原来。 如果您使用的是IDEA,则可以更轻松地查看“ 外部库” 。 有所有依赖关系,它们是正确的版本。


通用应用程序结构


我对应用程序的春季启动测试不熟悉,因此我将在不进行测试的情况下进行测试
(“是的,#肤色允许其自身”)。


应用配置


由于我是一位经验丰富的开发人员,因此不熟悉已建立的方法,因此我将:


  1. 在application.yml中(在建立数据库连接时)
     spring: datasource: driver-class-name: org.firebirdsql.jdbc.FBDriver url: jdbc:firebirdsql://host:3050//work/cmn/db/namedb.fdb?sql_dialect=3&charSet=utf-8 username: ****** password: ****** useUnicode: true characterEncoding: UTF-8 sql-script-encoding: UTF-8 jpa: show-sql: true 
  2. 在类中直接使用注释:

如果您未指定charSet = utf-8,则默认值为NONE :如果表也具有NONE,则根据firebirdsql.org,我们将获得无法读取的字符,或者:


 3.2.4 How can I solve the error “Connection rejected: No connection character set specified” If no character set has been set, Jaybird 3.0 will reject the connection with an SQLNonTransientConnectionException with message “Connection rejected: No connection character set specified (property lc_ctype, encoding, charSet or localEncoding). Please specify a connection character set (eg property charSet=utf-8) or consult the Jaybird documentation for more information.” 

最少的类和文件集


首先,index.html包含一个空的主体;
Swagger在API中运行,其包(基础)配置与项目包的其余部分放在同一级别。



添加到项目:


  • 包装型号
    • clacc CModule-aka 数据 (getter,成员的setter,这要归功于Lombok的最小“代码猴子”),aka Entity (数据库表的本质);
  • 软件包仓库
    • 接口CModuleRepository扩展了JpaRepository <CModule,String>(它将从数据库中选择数据),现在不需要添加任何内容(如Query );
  • 包装服务;
    • CModuleService类-又名Service和@Transactional(readOnly = true),用于处理存储库;
  • 包装资源
    • 类CModulesResource-又名@ RestController,@ RequestMapping(“ /模块”)将负责访问此地址。 响应机构会自己做(对我们而言,它看起来像这样)

我们将以所有方式使用RestController API,并向Swagger指出:


 @Configuration @EnableSwagger2 public class SwaggerConfiguration { @Bean public Docket documentation(){ return new Docket(DocumentationType.SWAGGER_2) .select() .apis(RequestHandlerSelectors.withClassAnnotation(RestController.class)) .paths(PathSelectors.any()) .build(); } } 

创建一个运行Spring Application的类:


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

为所需的表(我有模块)创建模型,存储库,请求:
现在您可以使用@RestController


 @RestController @RequestMapping("/modules") @Api(tags = "Modules", description = "Modules API") public class CModulesResource { .... @GetMapping(value = "/{name}") @ApiOperation(value = "Find module",notes = "Find the module by Name") @ApiResponses(value = { @ApiResponse(code = 200,message = "Modules found"), @ApiResponse(code = 404,message = "Modules not found"), }) 

api-带有描述的类的名称;
@ApiOperation方法名称及说明;
@ApiResponses返回的API代码;
@ApiResponse特定代码以及说明;


示例(是的,它仍然包含主要实体,我在本文中未描述)


现在,您可以使用REST API测试数据的选择。


二手文献清单:


 1. https://www.baeldung.com 2. https://docs.spring.io 3. Spring in Action, 5th Edition 4. https://www.firebirdsql.org/file/documentation/drivers_documentation/java/faq.html#how-can-i-solve-the-error-connection-rejected-no-connection-character-set-specified 

github链接


Github项目

Source: https://habr.com/ru/post/zh-CN432244/


All Articles