我用如下pom.xml创建了一个Spring Boot应用程序
<?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>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.1.2.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>com.tcs</groupId>
<artifactId>hotel</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>demo</name>
<description>Hotel project for Spring Boot</description>
<properties>
<java.version>1.8</java.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>
我的Application类在com.demo.
中,而我的控制器在com.demo.controller
包中。在我的控制器类下面给出。
package com.demo.controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
@RestController
public class HotelController {
@RequestMapping("/test")
public String getString() {
return "Helloworld";
}
}
运行Spring Boot应用程序后,我尝试通过localhost:8080/test
访问我的rest API,但是该API无法访问。它说“无法访问此站点”。
答案 0 :(得分:2)
缺少方法类型GET,您可以通过两个选项进行操作:
@RequestMapping(value = "/test", method = RequestMethod.GET)
public String getString() {
return "Helloworld";
}
或
@GetMapping("/test")
public String getString() {
return "Helloworld";
}