我的spring启动应用程序运行,但当我在postman中访问它时,它只为每个函数调用返回“httpstatus404–notfound”。我试过很多办法,都没有用。
文件夹结构
(图片)
FlightPlanner应用程序.java
package com.bogen.FlightPlanner;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
@SpringBootApplication
public class FlightPlannerApplication {
public static void main(String[] args) {
SpringApplication.run(FlightPlannerApplication.class, args);
}
}
airportapi.java文件
package com.bogen.FlightPlanner.api;
import java.util.List;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.server.ResponseStatusException;
import com.bogen.FlightPlanner.model.Airport;
import com.bogen.FlightPlanner.service.AirportService;
@RestController
@RequestMapping("AirportAPI")
public class AirportAPI {
@Autowired
private AirportService airportService;
@GetMapping("/getAirports")
public ResponseEntity<?> getAirports() {
try {
List<Airport> airports = airportService.getAirports();
return new ResponseEntity<List<Airport>>(airports, HttpStatus.OK);
}
catch (Exception e) {
throw new ResponseStatusException(HttpStatus.BAD_REQUEST, e.getMessage());
}
}
}
airportserviceimpl.java文件
import java.util.List;
import javax.transaction.Transactional;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.bogen.FlightPlanner.dao.AirportDAO;
import com.bogen.FlightPlanner.model.Airport;
@Service(value="airportService")
@Transactional
public class AirportServiceImpl implements AirportService {
@Autowired
AirportDAO airportDAO;
@Override
public List<Airport> getAirports() {
List<Airport> airports = null;
try {
airports = airportDAO.getAirports();
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return airports;
}
}
而airportdaoimpl.java文件正如您所期望的那样,用 @Repository
并包含一个用 @PersistenceContext
.
以下是控制台日志:
2020-12-02 12:48:43.236 INFO 4452 --- [ main] c.b.F.FlightPlannerApplication : Starting FlightPlannerApplication using Java 15.0.1 on DESKTOP-38S2QIU with PID 4452 (C:\Users\bogen\Downloads\FlightPlanner\target\classes started by bogen in C:\Users\bogen\Downloads\FlightPlanner)
2020-12-02 12:48:43.238 INFO 4452 --- [ main] c.b.F.FlightPlannerApplication : No active profile set, falling back to default profiles: default
2020-12-02 12:48:43.552 INFO 4452 --- [ main] .s.d.r.c.RepositoryConfigurationDelegate : Bootstrapping Spring Data JPA repositories in DEFERRED mode.
2020-12-02 12:48:43.562 INFO 4452 --- [ main] .s.d.r.c.RepositoryConfigurationDelegate : Finished Spring Data repository scanning in 5 ms. Found 0 JPA repository interfaces.
2020-12-02 12:48:43.894 INFO 4452 --- [ main] o.s.b.w.embedded.tomcat.TomcatWebServer : Tomcat initialized with port(s): 8080 (http)
2020-12-02 12:48:43.901 INFO 4452 --- [ main] o.apache.catalina.core.StandardService : Starting service [Tomcat]
2020-12-02 12:48:43.901 INFO 4452 --- [ main] org.apache.catalina.core.StandardEngine : Starting Servlet engine: [Apache Tomcat/9.0.39]
2020-12-02 12:48:43.973 INFO 4452 --- [ main] o.a.c.c.C.[.[localhost].[/Project] : Initializing Spring embedded WebApplicationContext
2020-12-02 12:48:43.973 INFO 4452 --- [ main] w.s.c.ServletWebServerApplicationContext : Root WebApplicationContext: initialization completed in 708 ms
2020-12-02 12:48:44.095 INFO 4452 --- [ main] o.s.s.concurrent.ThreadPoolTaskExecutor : Initializing ExecutorService 'applicationTaskExecutor'
2020-12-02 12:48:44.123 INFO 4452 --- [ task-1] o.hibernate.jpa.internal.util.LogHelper : HHH000204: Processing PersistenceUnitInfo [name: default]
2020-12-02 12:48:44.162 INFO 4452 --- [ task-1] org.hibernate.Version : HHH000412: Hibernate ORM core version 5.4.23.Final
2020-12-02 12:48:44.263 INFO 4452 --- [ main] o.s.b.w.embedded.tomcat.TomcatWebServer : Tomcat started on port(s): 8080 (http) with context path '/Project'
2020-12-02 12:48:44.265 INFO 4452 --- [ main] DeferredRepositoryInitializationListener : Triggering deferred initialization of Spring Data repositories…
2020-12-02 12:48:44.265 INFO 4452 --- [ main] DeferredRepositoryInitializationListener : Spring Data repositories initialized!
2020-12-02 12:48:44.268 INFO 4452 --- [ task-1] o.hibernate.annotations.common.Version : HCANN000001: Hibernate Commons Annotations {5.1.2.Final}
2020-12-02 12:48:44.272 INFO 4452 --- [ main] c.b.F.FlightPlannerApplication : Started FlightPlannerApplication in 1.272 seconds (JVM running for 1.516)
2020-12-02 12:48:44.348 INFO 4452 --- [ task-1] com.zaxxer.hikari.HikariDataSource : HikariPool-1 - Starting...
2020-12-02 12:48:44.591 INFO 4452 --- [ task-1] com.zaxxer.hikari.HikariDataSource : HikariPool-1 - Start completed.
2020-12-02 12:48:44.610 INFO 4452 --- [ task-1] org.hibernate.dialect.Dialect : HHH000400: Using dialect: org.hibernate.dialect.MySQL8Dialect
2020-12-02 12:48:45.032 INFO 4452 --- [ task-1] o.h.e.t.j.p.i.JtaPlatformInitiator : HHH000490: Using JtaPlatform implementation: [org.hibernate.engine.transaction.jta.platform.internal.NoJtaPlatform]
2020-12-02 12:48:45.038 INFO 4452 --- [ task-1] j.LocalContainerEntityManagerFactoryBean : Initialized JPA EntityManagerFactory for persistence unit 'default'
最后,pom.xml文件:
<?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.4.0</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>com.bogen</groupId>
<artifactId>FlightPlanner</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>FlightPlanner</name>
<description>Flight Planner project for Spring Boot</description>
<properties>
<java.version>15</java.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-jpa</artifactId>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<scope>runtime</scope>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<!-- https://mvnrepository.com/artifact/org.springframework/spring-web -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-web</artifactId>
<version>5.3.1</version>
</dependency>
<!-- https://mvnrepository.com/artifact/org.springframework.boot/spring-boot-starter-tomcat -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-tomcat</artifactId>
<version>2.4.0</version>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>
如预期的那样,应用程序无限期运行。来自 Postman 的get请求转到url:http://localhost:8080/project/airportapi/getairports。
有什么想法吗?
1条答案
按热度按时间fivyi3re1#
添加以下依赖项