连接到 127.0.0.1 端口 8080 失败:连接被拒绝
connect to 127.0.0.1 port 8080 failed: Connection refused
我使用 Spring(不是 Boot)创建了一个简单的 RestController
。
@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);
}
}
但是当我调用此方法并使用 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
另外:我推送 MocMvc-test 但它失败了,结果如下:
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));
}
}
在控制台中我看到 System.out.println(userRestController.get(2));
的输出
所以我的 CRUD 方法效果很好。
为了在不使用 Spring 引导的情况下使用 Spring 参与 HTTP 请求,您通常需要以下内容:
- 使用spring-webmvc 依赖项。我知道你已经有了。
- 将您的应用程序打包为 war。如果你用maven构建,你需要在你的pom.xml
中加上<packaging>war</packaging>
句
- 在您的 web.xml 中声明
org.springframework.web.servlet.DispatcherServlet
. You can also use the org.springframework.web.servlet.support.AbstractAnnotationConfigDispatcherServletInitializer
以注册它。
- 在您的应用程序上下文文件或 class 中定义映射和配置,如果您使用 java-config。
- 将您的 war 文件部署到 Servlet 容器中,例如 Tomcat 或 Jetty。
我举个例子 Spring 5 here.
您可以找到另一个示例 Spring 4 here。
我使用 Spring(不是 Boot)创建了一个简单的 RestController
。
@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);
}
}
但是当我调用此方法并使用 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
另外:我推送 MocMvc-test 但它失败了,结果如下:
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));
}
}
在控制台中我看到 System.out.println(userRestController.get(2));
的输出
所以我的 CRUD 方法效果很好。
为了在不使用 Spring 引导的情况下使用 Spring 参与 HTTP 请求,您通常需要以下内容:
- 使用spring-webmvc 依赖项。我知道你已经有了。
- 将您的应用程序打包为 war。如果你用maven构建,你需要在你的pom.xml 中加上
- 在您的 web.xml 中声明
org.springframework.web.servlet.DispatcherServlet
. You can also use theorg.springframework.web.servlet.support.AbstractAnnotationConfigDispatcherServletInitializer
以注册它。 - 在您的应用程序上下文文件或 class 中定义映射和配置,如果您使用 java-config。
- 将您的 war 文件部署到 Servlet 容器中,例如 Tomcat 或 Jetty。
<packaging>war</packaging>
句
我举个例子 Spring 5 here.
您可以找到另一个示例 Spring 4 here。