连接到127.0.0.1端口8080失败:连接被拒绝

问题描述 投票:2回答:1

我使用Spring(不是Boot)创建了一个简单的RestController

RestController

但是当我调用此方法并使用curl时,我收到:

@RestController
@RequestMapping(UserRestController.REST_URL)
public class UserRestController extends AbstractUserController {

    static final String REST_URL = "/users";

    @GetMapping(produces = MediaType.APPLICATION_JSON_VALUE)
    public User get(int id) {
      return super.get(id);
  }
}

另外:我推送了MocMvc-test,但失败并显示以下结果:

$ curl -v http://localhost:8080/users
Trying 127.0.0.1...
TCP_NODELAY set
connect to 127.0.0.1 port 8080 failed: Connection refused
Failed to connect to localhost port 8080: Connection refused

我通过以下方式运行我的应用程序:

java.lang.AssertionError: Status expected:<200> but was:<404>
  Expected :200
  Actual   :404

在Concole中,我看到System.out.println(userRestController.get(2))的输出;所以我的CRUD方法效果很好。

java spring rest curl spring-restcontroller
1个回答
0
投票

您需要运行您的spring boot应用程序。

从命令行运行以下命令

public static void main(String[] args) {

try (ConfigurableApplicationContext appCtx = new 
ClassPathXmlApplicationContext("spring/spring-app.xml")) {
 System.out.println("Bean definition names: " + 
  Arrays.toString(appCtx.getBeanDefinitionNames()));
UserRestController userRestController = 
  appCtx.getBean(UserRestController.class);
        System.out.println(userRestController.get(2));
    }
}

从vs代码,单击mvn spring-boot:run 运行该应用程序。

Run

© www.soinside.com 2019 - 2024. All rights reserved.