我制作了一个简单的Spring引导应用程序,但是由于某种原因,当我尝试访问它时总是得到404。它以前工作过,我不确定发生了什么变化。 这是我的Controller方法:
@RequestMapping("/countries/{country_name}")
public ModelAndView returnCountry(@PathVariable("country_name") String name) {
try {
JSONObject response = JsonReader.readJsonFromUrl("https://covid2019-api.herokuapp.com/v2/country/" + name);
JSONObject data = (JSONObject) response.get("data");
Country country = new Country(name, data.getInt("confirmed"), data.getInt("deaths"), data.getInt("recovered"), data.getInt("active"));
return country;
} catch (JSONException exception) {
return new CovidExceptions("API error");
} catch (NullPointerException exception) {
return new CovidExceptions("Country not found! Use English country code");
} catch (IOException exception) {
return new CovidExceptions("Could not read from the API");
}
}
我认为这是错误:
o.s.w.s.r.ResourceHttpRequestHandler : Resource not found
我试图将Country类移到其他目的地,但没有一个起作用。
这也是我的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.3.0.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>de.fh.albsig</groupId>
<artifactId>CovidTracker</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>jar</packaging>
<name>CovidTracker</name>
<description>Tracker for Covid-19 cases</description>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-tomcat</artifactId>
<scope>provided</scope>
</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.json</groupId>
<artifactId>json</artifactId>
<version>20190722</version>
</dependency>
<dependency>
<groupId>com.fasterxml.jackson.dataformat</groupId>
<artifactId>jackson-dataformat-xml</artifactId>
</dependency>
<!-- https://mvnrepository.com/artifact/org.apache.commons/commons-lang3 -->
<dependency>
<groupId>org.apache.commons</groupId>
<artifactId>commons-lang3</artifactId>
<version>3.10</version>
</dependency>
<!-- https://mvnrepository.com/artifact/io.quarkus/quarkus-junit5 -->
<dependency>
<groupId>io.quarkus</groupId>
<artifactId>quarkus-junit5</artifactId>
<version>1.4.2.Final</version>
<scope>test</scope>
</dependency>
<!-- https://mvnrepository.com/artifact/org.mockito/mockito-all -->
<dependency>
<groupId>org.mockito</groupId>
<artifactId>mockito-all</artifactId>
<version>1.10.19</version>
<scope>test</scope>
</dependency>
<!-- https://mvnrepository.com/artifact/org.apache.maven.plugins/maven-checkstyle-plugin -->
<dependency>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-checkstyle-plugin</artifactId>
<version>3.1.1</version>
</dependency>
<!-- <!– https://mvnrepository.com/artifact/com.google.code.findbugs/findbugs –>-->
<!-- <dependency>-->
<!-- <groupId>com.google.code.findbugs</groupId>-->
<!-- <artifactId>findbugs</artifactId>-->
<!-- <version>3.0.1</version>-->
<!-- </dependency>-->
</dependencies>
<reporting>
<plugins>
<plugin>
<groupId>com.google.code.findbugs</groupId>
<artifactId>findbugs</artifactId>
<version>3.0.1</version>
</plugin>
</plugins>
</reporting>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
<!-- <plugin>-->
<!-- <groupId>org.apache.maven.plugins</groupId>-->
<!-- <artifactId>maven-checkstyle-plugin</artifactId>-->
<!-- <version>3.1.1</version>-->
<!-- <configuration>-->
<!-- <encoding>UTF-8</encoding>-->
<!-- <consoleOutput>true</consoleOutput>-->
<!-- <failsOnError>false</failsOnError>-->
<!-- <linkXRef>false</linkXRef>-->
<!-- </configuration>-->
<!-- <executions>-->
<!-- <execution>-->
<!-- <id>validate</id>-->
<!-- <phase>validate</phase>-->
<!-- <goals>-->
<!-- <goal>check</goal>-->
<!-- </goals>-->
<!-- </execution>-->
<!-- </executions>-->
<!-- </plugin>-->
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-pmd-plugin</artifactId>
<version>3.13.0</version>
<executions>
<execution>
<id>validate</id>
<phase>validate</phase>
<goals>
<goal>check</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>
</build>
</project>
让我知道您是否还需要其他东西。
实际上,这是整个git repo:https://github.com/BlameFelix/CovidTracker 因此,master上的代码可以正常工作,而在测试分支上则显示404错误。我比较了代码,但似乎无法找出问题所在。
答案 0 :(得分:1)
您的方法返回类型为ModelAndView,并且您正在从方法内部返回国家对象。您的MasterBranch控制器具有正确的代码。