我目前正在关注有关带有Spring Cloud Security的Java微服务的Pluralsight教程。运行此应用程序后,我将从日志中复制生成的安全密码,并在邮递员中致电 http://localhost:9001/services/tolldata对用户:用户和密码:使用生成的密码使用基本身份验证。但是它给出了404 Not Found作为响应。
我尝试从应用程序设置更改端口,结果相同。 试图在属性中也添加主机地址,结果相同。 删除了server.servlet.context-path,结果相同。 将@RequestMapping(“ / tolldata”)放在getTollData()方法上方,结果相同。
这是我的主班。
@SpringBootApplication
@RestController
public class DemoApplication {
public static void main(String[] args) {
SpringApplication.run(DemoApplication.class, args);
}
@RequestMapping("/tolldata")
public class TollUsage{
public String id;
public String stationId;
public String licensePlate;
public String timestamp;
public List<TollUsage> getTollData(){
TollUsage instance1 = new TollUsage("100", "station150", "B65GT1W", "2016-09-30T06:31:22");
TollUsage instance2 = new TollUsage("101", "station119", "AHY673B", "2016-09-30T06:32:50");
TollUsage instance3 = new TollUsage("102", "station150", "ZN2GP0", "2016-09-30T06:37:01");
List<TollUsage> tolls = new ArrayList<TollUsage>();
tolls.add(instance1);
tolls.add(instance2);
tolls.add(instance3);
return tolls;
}
public TollUsage(){}
public TollUsage(String id, String stationId, String licensePlate, String timestamp){
this.id = id;
this.stationId = stationId;
this.licensePlate = licensePlate;
this.timestamp = timestamp;
}
}
}
这是我的application.properties
server.port=9001
server.servlet.context-path=/services
最后是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
http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.example</groupId>
<artifactId>demo</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>jar</packaging>
<name>demo</name>
<description>Demo project for Spring Boot</description>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.1.0.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
<java.version>1.8</java.version>
<spring-cloud.version>Greenwich.M3</spring-cloud.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-security</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
</dependencies>
<dependencyManagement>
<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-dependencies</artifactId>
<version>${spring-cloud.version}</version>
<type>pom</type>
<scope>import</scope>
</dependency>
</dependencies>
</dependencyManagement>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
<repositories>
<repository>
<id>spring-milestones</id>
<name>Spring Milestones</name>
<url>https://repo.spring.io/milestone</url>
<snapshots>
<enabled>false</enabled>
</snapshots>
</repository>
</repositories>
答案 0 :(得分:1)
将@RestController
放到TollUsage
类上,而不是DemoApplication
上,然后将这些类拆分为不同的文件。
答案 1 :(得分:1)
尝试一下:
@SpringBootApplication
public class DemoApplication {
public static void main(String[] args) {
SpringApplication.run(DemoApplication.class, args);
}
}
@RequestMapping(value = "/tolldata")
@RestController
class TollUsage{
public String id;
public String stationId;
public String licensePlate;
public String timestamp;
@GetMapping
@CrossOrigin
@ResponseBody
public ResponseEntity<List<TollUsage>> getTollData(){
TollUsage instance1 = new TollUsage("100", "station150", "B65GT1W", "2016-09-30T06:31:22");
TollUsage instance2 = new TollUsage("101", "station119", "AHY673B", "2016-09-30T06:32:50");
TollUsage instance3 = new TollUsage("102", "station150", "ZN2GP0", "2016-09-30T06:37:01");
List<TollUsage> tolls = new ArrayList<TollUsage>();
tolls.add(instance1);
tolls.add(instance2);
tolls.add(instance3);
return new ResponseEntity<>(tolls, HttpStatus.OK);;
}
public TollUsage(){}
public TollUsage(String id, String stationId, String licensePlate, String timestamp){
this.id = id;
this.stationId = stationId;
this.licensePlate = licensePlate;
this.timestamp = timestamp;
}
}