Spring Boot 微服务极简化开发:修订间差异
跳到导航
跳到搜索
(创建页面,内容为“使用 sprint Boot 框架,结合自定义类,实现 webservice 极简化开发。 === Setup === ==== Project Setting ==== <small><nowiki>Modules Source Folders: src/main/java Resource Folders: src/main/resources Test Source Folders: src/test/java Test Resource Folders: src/test/resources Excluded Folders: target</nowiki></small> ==== pom ==== <small><nowiki><!-- spring-boot --> <parent> <groupId>org.springframework.boot</groupId> <artifactI…”) |
|||
第95行: | 第95行: | ||
src/main/resources/static // 更改需重启 | src/main/resources/static // 更改需重启 | ||
<small><nowiki><html> | <small><nowiki><html> | ||
<p>This is | <p>This webservice is running...</p> | ||
</html></nowiki></small> | </html></nowiki></small> | ||
2024年10月24日 (四) 14:49的版本
使用 sprint Boot 框架,结合自定义类,实现 webservice 极简化开发。
Setup
Project Setting
Modules Source Folders: src/main/java Resource Folders: src/main/resources Test Source Folders: src/test/java Test Resource Folders: src/test/resources Excluded Folders: target
pom
<!-- spring-boot --> <parent> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-parent</artifactId> <version>3.3.2</version> <relativePath/> </parent> <dependencies> <!-- spring-boot --> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-thymeleaf</artifactId> </dependency> <!--log4j--> <dependency> <groupId>log4j</groupId> <artifactId>log4j</artifactId> <version>1.2.17</version> </dependency> <!--slf4j--> <!-- Conflicts with spring boot <dependency> <groupId>org.slf4j</groupId> <artifactId>slf4j-log4j12</artifactId> <version>${slf4j.version}</version> </dependency> --> <dependency> <groupId>org.slf4j</groupId> <artifactId>slf4j-api</artifactId> <version>2.0.9</version> </dependency> <!--Json--> <dependency> <groupId>com.google.code.gson</groupId> <artifactId>gson</artifactId> <version>2.8.9</version> </dependency> <dependency> <groupId>com.alibaba</groupId> <artifactId>fastjson</artifactId> <version>1.2.28</version> </dependency> <!--h2--> <dependency> <groupId>com.h2database</groupId> <artifactId>h2</artifactId> <version>2.1.212</version> </dependency> <build> <plugins> <plugin> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-maven-plugin</artifactId> </plugin> </plugins> </build> </dependencies>
application.yaml
src/main/resources
spring.application.name: SprintBootDemo spring.profiles.active: production server: port: 8080 spring.thymeleaf: mode: HTML cache: false encoding: UTF-8 enabled: true prefix: classpath:/templates/ suffix: .html
index.html
src/main/resources/static // 更改需重启
<html> <p>This webservice is running...</p> </html>
com.udef
BASE/CNFYAML/DB/SQLData
etc
log4j.properties/sysdb.yaml/db.yaml
config
sql1.yaml // 更改不需重启
Service
src/main/java/SprintBoot/SprintBootStart.java
package SprintBoot; import com.udf.SQLData; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; @SpringBootApplication public class SprintBootStart { public static SQLData[] sql = new SQLData[10]; public SprintBootStart() { sql[0] = new SQLData("h2local", "db.yaml"); } public static void main(String[] args) { SpringApplication.run(SprintBootStart.class, args); } }
src/main/java/SprintBoot/sql1/sql1.java
package SprintBoot; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RequestMethod; import org.springframework.web.bind.annotation.RestController; import java.util.LinkedHashMap; import java.util.Map; import com.udf.SQLData; import static SprintBoot.SprintBootStart.sql; @RestController @RequestMapping("/sql") public class sql1 { String appName; public sql1() { appName = "sql"; } @RequestMapping(value = "/get1", method = RequestMethod.GET) public Map<String, Object> getTest1() { //BASE.UDEFLOG = "ALL"; SQLData sql1 = sql[0]; sql1.setSQLYaml("config/sql1.yaml"); Map<String, String> m1 = new LinkedHashMap<>(); // get data m1.put("api", "SE_PROD"); m1.put("where.status", "1"); m1.put("where.ca", "in ('LRL', 'adam')"); return sql1.SQL(m1); } }
运行调试没问题的话,就可以打 jar 包了。
package
set main class
src/main/resources/META-INF/MANIFEST.MF
Manifest-Version: 1.0 Main-Class: SprintBoot.SprintBootStart
maven build
maven -> XXX —> Lifecycle -> package -> build
RUN
java -jar XXX.jar
Error
Artifact
Project Structure -> Artifact Build Artifact java -jar XXX.jar -> Error: No active profile set, falling back to 1 default profile: "default"
Solution: maven package
没有主清单属性
1. pom 中未引入 spring-boot-maven-plugin 2. MANIFEST.MF 中启动类设置错误(若只有一个 main,倒也无妨) 3. MF 文件也可通过 Project Setting/Artifacts 生成
找不到或无法加载主类
// 明明有,但是却无法 run/debug reload all maven