简体   繁体   English

Spring 使用 http://localhost/test 从浏览器调用时启动代码不起作用

[英]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.

相关问题 从Spring Boot测试调用的@Caching方法[注有@Transactional]无法正常工作 - @Caching method called from spring boot test [annotated with @Transactional] not working 在 Spring Boot 中使用 WebClientProvider 时测试收到的状态码 - Test received status code when using WebClientProvider in spring boot 从@MvcTest注释测试调用时,Spring Boot Controller的行为有所不同 - Spring Boot Controller differs in behaviour when called from @MvcTest annotated test 当 api 从 angular 调用到 spring 引导时,未调用后映射 - Postmapping not getting called when api called from angular to spring boot 使用 Angular 和 Spring 引导从本地主机获取数据 - Getting data from localhost using Angular and Spring Boot Spring Boot-MockMVC测试无法正常工作 - Spring Boot - MockMVC test not working 从 spring 引导自定义验证器返回不同的 HTTP 状态代码 - Return different HTTP status code from spring boot custom validators 当使用Spring + Spring Boot + STS时,Spring Project目录中的localhost“ home”文件夹在哪里? - When using Spring + Spring Boot + STS, where in the Spring Project directory is the localhost “home” folder? 如何在 Spring Boot 中使用真实数据库测试代码? - How to test code using real database in Spring Boot? Spring 启动默认为 HTTPS,但使用 HTTP 测试配置文件 - Spring boot by default HTTPS but test profile with HTTP
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM