[英]Spring Boot code not working when called from browser using http://localhost/test
controller class: controller class:
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;
@RestController
public class HelloWorldController
{
//using get method and hello-world as URI
// @GetMapping(path="/hello-world-bean")
@RequestMapping(method=RequestMethod.GET, value="/test")
public String sayHello()
{
return "Hello !!!";
}
@RequestMapping(method=RequestMethod.GET, value = "/getSquare/{number}")
public int getSquareOfNumber(@PathVariable int number)
{
return number*number;
}
}
Application class:应用 class:
package com.firstService.server.main;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
@SpringBootApplication
public class RestfulWebServicesApplication {
public static void main(String[] args) {
SpringApplication.run(RestfulWebServicesApplication.class, args);
}
}
When I am trying to call url http://localhost/test (I am displaying Hello for url /hello), it is not getting called and showing me below error.当我尝试调用 url http://localhost/test时(我正在为 url /hello 显示 Hello),它没有被调用并在下面显示错误。
spring-boot-starter-web This dependency was missing in pom.xml. spring-boot-starter-web pom.xml 中缺少此依赖项。 Thanks Nico !
谢谢尼科!
I have added below dependency in pom.xml and url is changed to http://localhost:8080/hello我在 pom.xml 中添加了以下依赖项,并且 url 更改为 http://localhost:8080/hello
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
Can you try with explicitly adding port also on which your application is running like http://localhost:8080/test您是否可以尝试显式添加您的应用程序正在运行的端口,例如 http://localhost:8080/test
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.