【问题标题】:Error in Java Rest Service : Connection refused: connectJava Rest 服务中的错误:连接被拒绝:连接
【发布时间】:2013-12-27 05:08:20
【问题描述】:

我正在尝试创建休息服务。但是运行后出现如下错误。

Exception in thread "main" com.sun.jersey.api.client.ClientHandlerException: java.net.ConnectException: Connection refused: connect
    at com.sun.jersey.client.urlconnection.URLConnectionClientHandler.handle(URLConnectionClientHandler.java:151)
    at com.sun.jersey.api.client.Client.handle(Client.java:648)
    at com.sun.jersey.api.client.WebResource.handle(WebResource.java:680)
    at com.sun.jersey.api.client.WebResource.access$200(WebResource.java:74)
    at com.sun.jersey.api.client.WebResource$Builder.get(WebResource.java:507)
    at astral.restservice.client.Test.main(Test.java:20)
Caused by: java.net.ConnectException: Connection refused: connect
    at java.net.DualStackPlainSocketImpl.connect0(Native Method)
    at java.net.DualStackPlainSocketImpl.socketConnect(Unknown Source)
    at java.net.AbstractPlainSocketImpl.doConnect(Unknown Source)
    at java.net.AbstractPlainSocketImpl.connectToAddress(Unknown Source)
    at java.net.AbstractPlainSocketImpl.connect(Unknown Source)
    at java.net.PlainSocketImpl.connect(Unknown Source)
    at java.net.SocksSocketImpl.connect(Unknown Source)
    at java.net.Socket.connect(Unknown Source)
    at java.net.Socket.connect(Unknown Source)
    at sun.net.NetworkClient.doConnect(Unknown Source)
    at sun.net.www.http.HttpClient.openServer(Unknown Source)
    at sun.net.www.http.HttpClient.openServer(Unknown Source)
    at sun.net.www.http.HttpClient.<init>(Unknown Source)
    at sun.net.www.http.HttpClient.New(Unknown Source)
    at sun.net.www.http.HttpClient.New(Unknown Source)
    at sun.net.www.protocol.http.HttpURLConnection.getNewHttpClient(Unknown Source)
    at sun.net.www.protocol.http.HttpURLConnection.plainConnect(Unknown Source)
    at sun.net.www.protocol.http.HttpURLConnection.connect(Unknown Source)
    at sun.net.www.protocol.http.HttpURLConnection.getInputStream(Unknown Source)
    at java.net.HttpURLConnection.getResponseCode(Unknown Source)
    at com.sun.jersey.client.urlconnection.URLConnectionClientHandler._invoke(URLConnectionClientHandler.java:249)
    at com.sun.jersey.client.urlconnection.URLConnectionClientHandler.handle(URLConnectionClientHandler.java:149)
    ... 5 more

以下是我的 web.xml:

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" id="WebApp_ID" version="2.5">
  <display-name>astral.restservice</display-name>
  <servlet>
    <servlet-name>Jersey REST Service</servlet-name>
    <servlet-class>com.sun.jersey.spi.container.servlet.ServletContainer</servlet-class>
    <init-param>
      <param-name>com.sun.jersey.config.property.packages</param-name>
      <param-value>astral.restservice</param-value>
    </init-param>
    <load-on-startup>1</load-on-startup>
  </servlet>
  <servlet-mapping>
    <servlet-name>Jersey REST Service</servlet-name>
    <url-pattern>/rest/*</url-pattern>
  </servlet-mapping>
</web-app>

Hello.java

package astral.restservice;

import javax.ws.rs.GET;
import javax.ws.rs.Path;
import javax.ws.rs.Produces;
import javax.ws.rs.core.MediaType;

// Plain old Java Object it does not extend as class or implements 
// an interface

// The class registers its methods for the HTTP GET request using the @GET annotation. 
// Using the @Produces annotation, it defines that it can deliver several MIME types,
// text, XML and HTML. 

// The browser requests per default the HTML MIME type.

//Sets the path to base URL + /hello
@Path("/hello")
public class Hello {

  // This method is called if TEXT_PLAIN is request
  @GET
  @Produces(MediaType.TEXT_PLAIN)
  public String sayPlainTextHello() {
    return "Hello Jersey";
  }

  // This method is called if XML is request
  @GET
  @Produces(MediaType.TEXT_XML)
  public String sayXMLHello() {
    return "<?xml version=\"1.0\"?>" + "<hello> Hello Jersey" + "</hello>";
  }

  // This method is called if HTML is request
  @GET
  @Produces(MediaType.TEXT_HTML)
  public String sayHtmlHello() {
    return "<html> " + "<title>" + "Hello Jersey" + "</title>"
        + "<body><h1>" + "Hello Jersey" + "</body></h1>" + "</html> ";
  }

} 

Test.java

package astral.restservice.client;

import java.net.URI;
import javax.ws.rs.core.MediaType;
import javax.ws.rs.core.UriBuilder;
import com.sun.jersey.api.client.Client;
import com.sun.jersey.api.client.ClientResponse;
import com.sun.jersey.api.client.WebResource;
import com.sun.jersey.api.client.config.ClientConfig;
import com.sun.jersey.api.client.config.DefaultClientConfig;
public class Test {
  public static void main(String[] args) {
    ClientConfig config = new DefaultClientConfig();
    Client client = Client.create(config);
    WebResource service = client.resource(getBaseURI());
    // Fluent interfaces
    System.out.println(service.path("rest").path("hello").accept(MediaType.TEXT_PLAIN).get(ClientResponse.class).toString());
    // Get plain text
    System.out.println(service.path("rest").path("hello").accept(MediaType.TEXT_PLAIN).get(String.class));
    // Get XML
    System.out.println(service.path("rest").path("hello").accept(MediaType.TEXT_XML).get(String.class));
    // The HTML
    System.out.println(service.path("rest").path("hello").accept(MediaType.TEXT_HTML).get(String.class));


  }

  private static URI getBaseURI() {
    return UriBuilder.fromUri("http://localhost:8080/astral.restservice").build();
  }

} 

这是我的项目结构

为什么我会收到此错误?提前致谢。

使用tomcat v7运行浏览器后出现以下错误

HTTP Status 500 - Servlet.init() for servlet Jersey REST Service threw exception

type Exception report

message Servlet.init() for servlet Jersey REST Service threw exception

description The server encountered an internal error that prevented it from fulfilling this request.

exception

javax.servlet.ServletException: Servlet.init() for servlet Jersey REST Service threw exception
    org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:472)
    org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:99)
    org.apache.catalina.valves.AccessLogValve.invoke(AccessLogValve.java:947)
    org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:408)
    org.apache.coyote.http11.AbstractHttp11Processor.process(AbstractHttp11Processor.java:1009)
    org.apache.coyote.AbstractProtocol$AbstractConnectionHandler.process(AbstractProtocol.java:589)
    org.apache.tomcat.util.net.JIoEndpoint$SocketProcessor.run(JIoEndpoint.java:310)
    java.util.concurrent.ThreadPoolExecutor.runWorker(Unknown Source)
    java.util.concurrent.ThreadPoolExecutor$Worker.run(Unknown Source)
    java.lang.Thread.run(Unknown Source)

root cause

com.sun.jersey.spi.inject.Errors$ErrorMessagesException
    com.sun.jersey.spi.inject.Errors.processErrorMessages(Errors.java:170)
    com.sun.jersey.spi.inject.Errors.postProcess(Errors.java:136)
    com.sun.jersey.spi.inject.Errors.processWithErrors(Errors.java:199)
    com.sun.jersey.server.impl.application.WebApplicationImpl.initiate(WebApplicationImpl.java:695)
    com.sun.jersey.server.impl.application.WebApplicationImpl.initiate(WebApplicationImpl.java:690)
    com.sun.jersey.spi.container.servlet.ServletContainer.initiate(ServletContainer.java:438)
    com.sun.jersey.spi.container.servlet.ServletContainer$InternalWebComponent.initiate(ServletContainer.java:287)
    com.sun.jersey.spi.container.servlet.WebComponent.load(WebComponent.java:587)
    com.sun.jersey.spi.container.servlet.WebComponent.init(WebComponent.java:213)
    com.sun.jersey.spi.container.servlet.ServletContainer.init(ServletContainer.java:342)
    com.sun.jersey.spi.container.servlet.ServletContainer.init(ServletContainer.java:516)
    javax.servlet.GenericServlet.init(GenericServlet.java:160)
    org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:472)
    org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:99)
    org.apache.catalina.valves.AccessLogValve.invoke(AccessLogValve.java:947)
    org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:408)
    org.apache.coyote.http11.AbstractHttp11Processor.process(AbstractHttp11Processor.java:1009)
    org.apache.coyote.AbstractProtocol$AbstractConnectionHandler.process(AbstractProtocol.java:589)
    org.apache.tomcat.util.net.JIoEndpoint$SocketProcessor.run(JIoEndpoint.java:310)
    java.util.concurrent.ThreadPoolExecutor.runWorker(Unknown Source)
    java.util.concurrent.ThreadPoolExecutor$Worker.run(Unknown Source)
    java.lang.Thread.run(Unknown Source)

note The full stack trace of the root cause is available in the Apache Tomcat/7.0.39 logs.
Apache Tomcat/7.0.39

我尝试使用命令提示符运行服务器,但出现以下错误, 我设置了 CATALINA_HOME,JAVA_HOME,CLASSPATH 并添加了 PATH。

**

D:\apache-tomcat-7.0.39\bin>startup
The CATALINA_HOME environment variable is not defined correctly
This environment variable is needed to run this program

D:\apache-tomcat-7.0.39\bin>startup.bat
The CATALINA_HOME environment variable is not defined correctly
This environment variable is needed to run this program

D:\apache-tomcat-7.0.39\bin>catalina.bat
The CATALINA_HOME environment variable is not defined correctly
This environment variable is needed to run this program

D:\apache-tomcat-7.0.39\bin>

**

【问题讨论】:

  • 连接被拒绝 = 端口未打开。您是否在尝试运行客户端之前启动了应用服务器?您的应用服务器是否配置为在端口 8080 上运行?可以用浏览器打吗?
  • @charlie 检查我编辑的问题

标签: java rest environment-variables


【解决方案1】:
http://localhost:8080

只需从您的网络浏览器访问此链接。我认为您的 apache 服务器正在其他端口上运行。请检查服务器属性,并适当更换端口。

【讨论】:

  • 请告诉我你在 Internet Explorer 或 firefox 或 chrome 中得到了什么,而不是在命令提示符下。
  • 哦,我的错,我认为您需要确保您正确设置此路径。参考:stackoverflow.com/questions/9361623/…
  • @Parvathy,你能告诉我你是如何解决这个问题的吗?我也面临同样的问题..我可以从浏览器访问 Rest 服务,但不能使用 java 中的 rest 客户端...
  • @spk 我的服务器在不同的端口上运行。我设置了端口,它工作正常。检查我的 conf 文件
【解决方案2】:

如果有人看到这篇文章,请检查您是如何调用应用程序的

我有:

java -jar test-suite.jar -dHost http://localhost:8080 -other params

我应该这样做:

java -dHost http://localhost:8080 -other params -jar test-suite.jar

简单的错误,把你的参数放在 -jar 前面

【讨论】:

    【解决方案3】:

    尝试打开端口。你可以这样做:

    iptables -P INPUT ACCEPT
    

    注意:以上命令打开所有端口

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-03-18
      • 2016-09-12
      • 2017-04-28
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多