gpt4 book ai didi

Java Spring 与 swagger2 未启动服务器

转载 作者:行者123 更新时间:2023-12-02 08:52:02 26 4
gpt4 key购买 nike

我正在尝试为大学类(class)创建一个 Java Spring Web 应用程序。我创建了连接到两个服务类的 Rest Controller 类。服务类通过JPA 连接到数据库。当我摆脱与构建问题相关的错误时,我决定检查一切是否在 swagger 中正确可见。这里出现了问题,因为尽管创建了配置文件,但服务器无法启动。应用程序编译正确,但 Tomcat 服务器未出现,并且无法通过 localhost:8080 连接到服务器。如果有任何错误,我会处理,但在控制台中一切看起来都很好,或者我只是这么认为。

休息 Controller 类:

@RestController
public class JDBCController {
@Autowired
private IDelegationService delegationService;
@Autowired
private IUserService userService;

@RequestMapping(value = "/registerUser", method = RequestMethod.POST)
@ResponseBody
public void registerUser(@ModelAttribute User user) {

userService.save(user);

}

@RequestMapping(value = "/getAllUsers", method = RequestMethod.GET)
@ResponseBody
public List<User> getAllUsers() {
return userService.findAll();
}

@RequestMapping(value = "/changePassword", method = RequestMethod.PUT)
@ResponseBody
public void changePassword(@RequestParam("userId") long userId, @RequestParam("newPassword") String newPassword) {
userService.updatePassword(userId, newPassword);
}

@RequestMapping(value = "/deleteUserById", method = RequestMethod.DELETE)
@ResponseBody
public boolean deleteUserById(@RequestParam("userId") long userId) {

return userService.deleteById(userId);

}

@RequestMapping(value = "/getAllUsersByRoleName", method = RequestMethod.GET)
@ResponseBody
public List<User> getAllUsersByRoleName(@RequestParam("roleName") String roleName) {
return userService.getAllUsersByRoleName(roleName);
}

@RequestMapping(value = "/addDelegation", method = RequestMethod.POST)
@ResponseBody
public void addDelegation(@RequestParam("userId") long userId, @ModelAttribute Delegation delegation) {

delegationService.save(userId, delegation);

}

@RequestMapping(value = "/removeDelegation", method = RequestMethod.DELETE)
@ResponseBody
public boolean removeDelegation(@RequestParam("userId") long userId,
@RequestParam("delegationId") long delegationId) {
if (delegationId != 0L) {
return delegationService.deleteById(delegationId);
} else if (userId != 0L) {
return delegationService.deleteByUser(userId);
}
return false;

}

@RequestMapping(value = "/changeDelegation", method = RequestMethod.PUT)
@ResponseBody
public void changeDelegation(@RequestParam("delegationId") long delegationId,
@ModelAttribute Delegation delegation) {
delegationService.updateDelegation(delegationId, delegation);
}

@RequestMapping(value = "/getAllDelegations", method = RequestMethod.GET)
@ResponseBody
public List<Delegation> getAllDelegations(){
return delegationService.findAll();

}

@RequestMapping(value = "/getAllDelegationsOrderByDateStartDesc", method = RequestMethod.GET)
@ResponseBody
public List<Delegation> getAllDelegationsOrderByDateStartDesc(){
return delegationService.findAllOrderByDateStartDesc();

}

@RequestMapping(value = "/getAllDelegationsByUserOrderByDateStartDesc", method = RequestMethod.GET)
@ResponseBody
public List<Delegation> getAllDelegationsByUserOrderByDateStartDesc(@RequestParam("userId") Long userId){
return delegationService.findByUserOrderByDateStartDesc(userId);

}

}

Swagger 配置类:

@Configuration
@EnableSwagger2
public class SpringFoxConfig {

@Bean
public Docket mainConfig() {
return new Docket(DocumentationType.SWAGGER_2)
.select().apis(RequestHandlerSelectors.any())
.paths(PathSelectors.any())
.build()
.pathMapping("/swagger")
.directModelSubstitute(LocalDate.class, String.class)
.genericModelSubstitutes(ResponseEntity.class);
}
}

pom 文件:

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.2.5.RELEASE</version>
<relativePath /> <!-- lookup parent from repository -->
</parent>
<groupId>com.project</groupId>
<artifactId>Laboratorium1</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>Laboratorium1</name>
<description>Demo project for Spring Boot</description>

<properties>
<java.version>1.8</java.version>
</properties>

<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-jpa</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-jdbc</artifactId>
</dependency>

<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<scope>runtime</scope>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<optional>true</optional>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
<exclusions>
<exclusion>
<groupId>org.junit.vintage</groupId>
<artifactId>junit-vintage-engine</artifactId>
</exclusion>
</exclusions>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-web</artifactId>
</dependency>

<dependency>
<groupId>io.springfox</groupId>
<artifactId>springfox-swagger2</artifactId>
<version>2.9.2</version>
</dependency>
<dependency>
<groupId>io.springfox</groupId>
<artifactId>springfox-swagger-ui</artifactId>
<version>2.9.2</version>
</dependency>
</dependencies>

<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>

</project>

控制台:

[INFO] Scanning for projects...
[INFO]
[INFO] ---------------------< com.project:Laboratorium1 >----------------------
[INFO] Building Laboratorium1 0.0.1-SNAPSHOT
[INFO] --------------------------------[ jar ]---------------------------------
[INFO]
[INFO] >>> spring-boot-maven-plugin:2.2.5.RELEASE:run (default-cli) > test-compile @ Laboratorium1 >>>
[INFO]
[INFO] --- maven-resources-plugin:3.1.0:resources (default-resources) @ Laboratorium1 ---
[INFO] Using 'UTF-8' encoding to copy filtered resources.
[INFO] Copying 1 resource
[INFO] Copying 0 resource
[INFO]
[INFO] --- maven-compiler-plugin:3.8.1:compile (default-compile) @ Laboratorium1 ---
[INFO] Nothing to compile - all classes are up to date
[INFO]
[INFO] --- maven-resources-plugin:3.1.0:testResources (default-testResources) @ Laboratorium1 ---
[INFO] Using 'UTF-8' encoding to copy filtered resources.
[INFO] skip non existing resourceDirectory C:\Users\Admin\eclipse-workspace\PSS_Kulesza_Durol\src\test\resources
[INFO]
[INFO] --- maven-compiler-plugin:3.8.1:testCompile (default-testCompile) @ Laboratorium1 ---
[INFO] Nothing to compile - all classes are up to date
[INFO]
[INFO] <<< spring-boot-maven-plugin:2.2.5.RELEASE:run (default-cli) < test-compile @ Laboratorium1 <<<
[INFO]
[INFO]
[INFO] --- spring-boot-maven-plugin:2.2.5.RELEASE:run (default-cli) @ Laboratorium1 ---
[INFO] Attaching agents: []

. ____ _ __ _ _
/\\ / ___'_ __ _ _(_)_ __ __ _ \ \ \ \
( ( )\___ | '_ | '_| | '_ \/ _` | \ \ \ \
\\/ ___)| |_)| | | | | || (_| | ) ) ) )
' |____| .__|_| |_|_| |_\__, | / / / /
=========|_|==============|___/=/_/_/_/
:: Spring Boot :: (v2.2.5.RELEASE)

2020-03-16 20:18:07.785 INFO 4176 --- [ main] c.p.L.Laboratorium1Application : Starting Laboratorium1Application on DESKTOP-7SPKPTP with PID 4176 (C:\Users\Admin\eclipse-workspace\PSS_Kulesza_Durol\target\classes started by Admin in C:\Users\Admin\eclipse-workspace\PSS_Kulesza_Durol)
2020-03-16 20:18:07.788 INFO 4176 --- [ main] c.p.L.Laboratorium1Application : No active profile set, falling back to default profiles: default
2020-03-16 20:18:08.357 INFO 4176 --- [ main] .s.d.r.c.RepositoryConfigurationDelegate : Bootstrapping Spring Data JPA repositories in DEFAULT mode.
2020-03-16 20:18:08.456 INFO 4176 --- [ main] .s.d.r.c.RepositoryConfigurationDelegate : Finished Spring Data repository scanning in 90ms. Found 3 JPA repository interfaces.
2020-03-16 20:18:08.898 INFO 4176 --- [ main] o.hibernate.jpa.internal.util.LogHelper : HHH000204: Processing PersistenceUnitInfo [name: default]
2020-03-16 20:18:09.040 INFO 4176 --- [ main] org.hibernate.Version : HHH000412: Hibernate ORM core version 5.4.12.Final
2020-03-16 20:18:09.187 INFO 4176 --- [ main] o.hibernate.annotations.common.Version : HCANN000001: Hibernate Commons Annotations {5.1.0.Final}
2020-03-16 20:18:09.521 INFO 4176 --- [ main] com.zaxxer.hikari.HikariDataSource : HikariPool-1 - Starting...
2020-03-16 20:18:10.116 INFO 4176 --- [ main] com.zaxxer.hikari.HikariDataSource : HikariPool-1 - Start completed.
2020-03-16 20:18:10.137 INFO 4176 --- [ main] org.hibernate.dialect.Dialect : HHH000400: Using dialect: org.hibernate.dialect.MySQL8Dialect
2020-03-16 20:18:11.044 INFO 4176 --- [ main] o.h.e.t.j.p.i.JtaPlatformInitiator : HHH000490: Using JtaPlatform implementation: [org.hibernate.engine.transaction.jta.platform.internal.NoJtaPlatform]
2020-03-16 20:18:11.051 INFO 4176 --- [ main] j.LocalContainerEntityManagerFactoryBean : Initialized JPA EntityManagerFactory for persistence unit 'default'
2020-03-16 20:18:11.720 INFO 4176 --- [ main] c.p.L.Laboratorium1Application : Started Laboratorium1Application in 4.355 seconds (JVM running for 4.983)
2020-03-16 20:18:11.726 INFO 4176 --- [extShutdownHook] j.LocalContainerEntityManagerFactoryBean : Closing JPA EntityManagerFactory for persistence unit 'default'
2020-03-16 20:18:11.729 INFO 4176 --- [extShutdownHook] com.zaxxer.hikari.HikariDataSource : HikariPool-1 - Shutdown initiated...
2020-03-16 20:18:11.734 INFO 4176 --- [extShutdownHook] com.zaxxer.hikari.HikariDataSource : HikariPool-1 - Shutdown completed.
[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESS
[INFO] ------------------------------------------------------------------------
[INFO] Total time: 8.320 s
[INFO] Finished at: 2020-03-16T20:18:11+01:00
[INFO] ------------------------------------------------------------------------

提前感谢您的帮助。\

编辑1

这就是我的主要方法和类的样子:

@SpringBootApplication
public class Laboratorium1Application {

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

}

现在,好像应该在此处添加一些内容,以便可以看到配置文件。

我的项目文件夹:

enter image description here

现在我才注意到没有html文件。看看这些例子,我认为它会自行生成,但显然我错了。有人知道我可以从哪里下载它。

最佳答案

您需要在 pom.xml 文件中一个额外的依赖项来启动 Spring Boot(嵌入式 Tomcat)服务器:spring-boot-starter-web 添加该依赖项即可一切顺利。

 <dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>

关于Java Spring 与 swagger2 未启动服务器,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60712497/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com