Java 连接到127.0.0.1端口8080失败:连接被拒绝
我使用Spring(而不是Boot)创建了一个简单的 但当我调用此方法并使用curl时,我收到:Java 连接到127.0.0.1端口8080失败:连接被拒绝,java,spring,rest,curl,spring-restcontroller,Java,Spring,Rest,Curl,Spring Restcontroller,我使用Spring(而不是Boot)创建了一个简单的 但当我调用此方法并使用curl时,我收到: $ 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 另外:我推送M
$ 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
另外:我推送MocMvc测试,结果失败,结果如下:
java.lang.AssertionError: Status expected:<200> but was:<404>
Expected :200
Actual :404
在控制台中,我看到System.out.println(userRestController.get(2))的输出代码>
因此,我的CRUD方法运行良好。为了在不使用Spring引导的情况下使用Spring处理HTTP请求,您通常需要以下内容:
使用SpringWebMVC依赖项。我知道你已经有了
将应用程序打包为war。如果使用maven构建,则需要在pom.xml中添加war
语句
在web.xml中声明。您还可以使用来注册它
如果使用java配置,请在应用程序上下文文件或类中定义映射和配置
将war文件部署在类似Tomcat或Jetty的Servlet容器中
我举了一个Spring5的例子
您可以在Spring 4中找到另一个示例。部署了哪台服务器?这是否回答了您的问题?你开始你的春季计划了吗?它应该已经打印出它正在侦听的端口。向我们显示(仍在运行)程序的输出。服务器。。。我错过了这个细节…服务器正在运行吗?我99%确定服务器没有运行。我忘记了web.xml中的Dispatcher servlet!
java.lang.AssertionError: Status expected:<200> but was:<404>
Expected :200
Actual :404
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));
}
}