使用自定义提供程序

时间:2018-02-12 22:35:05

标签: java jax-rs apache-wink

我使用JAXRS设置了REST服务。我已经像这样配置了应用程序:

WEB.XML

  <servlet>
    <description>
    JAX-RS Tools Generated - Do not modify</description>
    <servlet-name>JAX-RS Servlet</servlet-name>
    <servlet-class>com.ibm.websphere.jaxrs.server.IBMRestServlet</servlet-class>
<init-param>
            <param-name>javax.ws.rs.Application</param-name>
            <param-value>com.test.gateway.service.ValidationServiceApplication</param-value>
    </init-param>
 <!--   <init-param>
            <param-name />
            <param-value />
        </init-param> -->
    <load-on-startup>1</load-on-startup>
  </servlet>

ValidationServiceApplication.java

    import java.util.HashSet;
    import java.util.Set;

    import javax.ws.rs.core.Application;


    public class ValidationServiceApplication extends Application{
        @Override
        public Set<Class<?>> getClasses() {
            Set<Class<?>> classes = new HashSet<Class<?>>();
            classes.add(ValidationService.class);
            return classes;
        }
    }

ValidationService.java

import javax.ws.rs.GET;
import javax.ws.rs.POST;
import javax.ws.rs.Path;
import javax.ws.rs.PathParam;
import javax.ws.rs.Produces;

import com.test.gateway.bo.AuthenticationManager;

@Path(value="/validationService")
public class ValidationService {

    @GET
    @Produces(value="text/plain")
    @Path(value="{token}")
    public String getPropety(@PathParam("token") String token) {
     String status = AuthenticationManager.getInstance().getTokenStatus(token);
     return status;
    }
}

在我的消费者中,我需要向服务发送请求并获得响应。我有以下代码:

public String validateToken(String token){

    ClientConfig clientConfig = new ApacheHttpClientConfig();
    javax.ws.rs.core.Application app = new javax.ws.rs.core.Application() {
           public Set<Class<?>> getClasses() {
               Set<Class<?>> classes = new HashSet<Class<?>>();
               classes.add(ValidationService.class);
               return classes;
           }
    };
    clientConfig.applications(app);
    RestClient client = new RestClient(clientConfig);
    Resource resource = client.resource("http://localhost:9081/sample-server-gateway/validation/validationService/" + token);
    String response = resource.accept("text/plain").get(String.class);  
    System.err.println("the response code is: " +  response);
    return response;
}

此代码基于我审核的几个例子;我不理解这个设置。如果我的ValidationService.java(在上面的代码中看到)存在于我的提供者中,我怎样才能从我的消费者那里访问这个类?或者是否有必要通过JAR文件或其他方法将此资源包含在我的使用者中?任何信息都表示赞赏。

此代码似乎有效,但我收到“读取超时”异常。

    public String validateToken(String token){

    ClientConfig clientConfig = new ApacheHttpClientConfig();
    RestClient client = new RestClient(clientConfig);
    Resource resource = client.resource("http://localhost:9081/sample-server-gateway/validation/validationService/" + token);
    String response = resource.accept("text/plain").get(String.class);  
    System.err.println("the response code is: " +  response);
    return response;
}

以下是堆栈跟踪:

[2/12/18 16:54:06:591 CST] 0000008d webapp        E com.ibm.ws.webcontainer.webapp.WebApp logServletError SRVE0293E: [Servlet Error]-[ServletNameNotFound]: org.apache.wink.client.ClientRuntimeException: java.lang.RuntimeException: java.net.SocketTimeoutException: Read timed out
    at org.apache.wink.client.internal.ResourceImpl.invoke(ResourceImpl.java:240)
    at org.apache.wink.client.internal.ResourceImpl.invoke(ResourceImpl.java:189)
    at org.apache.wink.client.internal.ResourceImpl.get(ResourceImpl.java:302)
    at com.hecorp.gateway.bo.AuthenticationManager.validateToken(AuthenticationManager.java:75)
    at com.hecorp.gateway.servlet.GatewayFilter.doFilter(GatewayFilter.java:67)
    at com.ibm.ws.webcontainer.filter.FilterInstanceWrapper.doFilter(FilterInstanceWrapper.java:195)
    at com.ibm.ws.webcontainer.filter.WebAppFilterChain.doFilter(WebAppFilterChain.java:91)
    at com.ibm.ws.webcontainer.filter.WebAppFilterManager.doFilter(WebAppFilterManager.java:967)
    at com.ibm.ws.webcontainer.filter.WebAppFilterManager.invokeFilters(WebAppFilterManager.java:1107)
    at com.ibm.ws.webcontainer.webapp.WebApp.handleRequest(WebApp.java:3980)
    at com.ibm.ws.webcontainer.webapp.WebGroup.handleRequest(WebGroup.java:304)
    at com.ibm.ws.webcontainer.WebContainer.handleRequest(WebContainer.java:1016)
    at com.ibm.ws.webcontainer.WSWebContainer.handleRequest(WSWebContainer.java:1817)
    at com.ibm.ws.webcontainer.channel.WCChannelLink.ready(WCChannelLink.java:200)
    at com.ibm.ws.http.channel.inbound.impl.HttpInboundLink.handleDiscrimination(HttpInboundLink.java:463)
    at com.ibm.ws.http.channel.inbound.impl.HttpInboundLink.handleNewRequest(HttpInboundLink.java:530)
    at com.ibm.ws.http.channel.inbound.impl.HttpInboundLink.processRequest(HttpInboundLink.java:316)
    at com.ibm.ws.http.channel.inbound.impl.HttpICLReadCallback.complete(HttpICLReadCallback.java:88)
    at com.ibm.ws.tcp.channel.impl.AioReadCompletionListener.futureCompleted(AioReadCompletionListener.java:175)
    at com.ibm.io.async.AbstractAsyncFuture.invokeCallback(AbstractAsyncFuture.java:217)
    at com.ibm.io.async.AsyncChannelFuture.fireCompletionActions(AsyncChannelFuture.java:161)
    at com.ibm.io.async.AsyncFuture.completed(AsyncFuture.java:138)
    at com.ibm.io.async.ResultHandler.complete(ResultHandler.java:204)
    at com.ibm.io.async.ResultHandler.runEventProcessingLoop(ResultHandler.java:775)
    at com.ibm.io.async.ResultHandler$2.run(ResultHandler.java:905)
    at com.ibm.ws.util.ThreadPool$Worker.run(ThreadPool.java:1892)
Caused by: java.lang.RuntimeException: java.net.SocketTimeoutException: Read timed out
    at org.apache.wink.client.internal.handlers.ApacheHttpClientConnectionHandler.handle(ApacheHttpClientConnectionHandler.java:82)
    at org.apache.wink.client.internal.handlers.HandlerContextImpl.doChain(HandlerContextImpl.java:52)
    at org.apache.wink.client.internal.handlers.AcceptHeaderHandler.handle(AcceptHeaderHandler.java:79)
    at org.apache.wink.client.internal.handlers.HandlerContextImpl.doChain(HandlerContextImpl.java:52)
    at org.apache.wink.client.internal.ResourceImpl.invoke(ResourceImpl.java:227)
    ... 25 more
Caused by: java.net.SocketTimeoutException: Read timed out
    at java.net.SocketInputStream.socketRead0(Native Method)
    at java.net.SocketInputStream.read(SocketInputStream.java:164)
    at java.net.SocketInputStream.read(SocketInputStream.java:134)
    at org.apache.http.impl.io.AbstractSessionInputBuffer.fillBuffer(AbstractSessionInputBuffer.java:130)
    at org.apache.http.impl.io.SocketInputBuffer.fillBuffer(SocketInputBuffer.java:127)
    at org.apache.http.impl.io.AbstractSessionInputBuffer.readLine(AbstractSessionInputBuffer.java:233)
    at org.apache.http.impl.conn.DefaultResponseParser.parseHead(DefaultResponseParser.java:98)
    at org.apache.http.impl.io.AbstractMessageParser.parse(AbstractMessageParser.java:210)
    at org.apache.http.impl.AbstractHttpClientConnection.receiveResponseHeader(AbstractHttpClientConnection.java:271)
    at org.apache.http.impl.conn.DefaultClientConnection.receiveResponseHeader(DefaultClientConnection.java:227)
    at org.apache.http.impl.conn.AbstractClientConnAdapter.receiveResponseHeader(AbstractClientConnAdapter.java:209)
    at org.apache.http.protocol.HttpRequestExecutor.doReceiveResponse(HttpRequestExecutor.java:292)
    at org.apache.http.protocol.HttpRequestExecutor.execute(HttpRequestExecutor.java:126)
    at org.apache.http.impl.client.DefaultRequestDirector.execute(DefaultRequestDirector.java:483)
    at org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:641)
    at org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:576)
    at org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:554)
    at org.apache.wink.client.internal.handlers.ApacheHttpClientConnectionHandler.processRequest(ApacheHttpClientConnectionHandler.java:102)
    at org.apache.wink.client.internal.handlers.ApacheHttpClientConnectionHandler.handle(ApacheHttpClientConnectionHandler.java:79)
    ... 29 more

如果我从浏览器点击了网址,我会收到成功的结果。

enter image description here

1 个答案:

答案 0 :(得分:0)

扩展readTimeOut允许代码运行。

public String validateToken(String token){

    ClientConfig clientConfig = new ApacheHttpClientConfig();
    clientConfig.readTimeout(300000);
    RestClient client = new RestClient(clientConfig);
    Resource resource = client.resource("http://localhost:9081/sample-server-gateway/validation/validationService/" + token);
    String response = resource.accept(MediaType.TEXT_PLAIN).get(String.class);  
    System.err.println("the response code is: " +  response);
    return response;
}