为什么我的客户不起作用?

时间:2015-07-03 09:54:01

标签: java rest jersey

我开始在Java链接中关注REST的vogella教程:http://www.vogella.com/tutorials/REST/article.html

但是我无法创建客户端,这是我的代码:

package com.vogella.jersey.jaxb.client;

import java.net.URI;

import javax.ws.rs.client.Client;
import javax.ws.rs.client.ClientBuilder;
import javax.ws.rs.client.WebTarget;
import javax.ws.rs.core.MediaType;
import javax.ws.rs.core.UriBuilder;

import org.glassfish.jersey.client.ClientConfig;

public class TodoTest {

  public static void main(String[] args) {
    ClientConfig config = new ClientConfig();
    Client client = ClientBuilder.newClient(config);

    WebTarget target = client.target(getBaseURI());
    // Get XML
    String xmlResponse = target.path("rest").path("todo").request()
        .accept(MediaType.TEXT_XML).get(String.class);
    // Get XML for application
    String xmlAppResponse =target.path("rest").path("todo").request()
        .accept(MediaType.APPLICATION_XML).get(String.class);

    // For JSON response also add the Jackson libraries to your webapplication
    // In this case you would also change the client registration to
    // ClientConfig config = new ClientConfig().register(JacksonFeature.class);
    // Get JSON for application
    // System.out.println(target.path("rest").path("todo").request()
    // .accept(MediaType.APPLICATION_JSON).get(String.class));

    System.out.println(xmlResponse);
    System.out.println(xmlAppResponse);
  }

  private static URI getBaseURI() {
    return UriBuilder.fromUri("http://localhost:8080/_com.vogella.jersey.jaxb").build();
  }
}

我收到此错误

For build() - The method build(Object[]) in the type UriBuilder is not applicable for the arguments ()

For accept() - The method accept(String[]) in the type Invocation.Builder is not applicable for the arguments (String)

我尝试用accept() -

来做这件事
String[] appXML = new String[1];
appXML[0] = MediaType.APPLICATION_XML;
String xmlAppResponse =(String) target.path("rest").path("todo").request()
    .accept(appXML).get(String.class);

这与build() -

.build(null);

但我收到此错误

Exception in thread "main" javax.ws.rs.NotFoundException: HTTP 404 Not Found
    at org.glassfish.jersey.client.JerseyInvocation.convertToException(JerseyInvocation.java:990)
    at org.glassfish.jersey.client.JerseyInvocation.translate(JerseyInvocation.java:799)
    at org.glassfish.jersey.client.JerseyInvocation.access$500(JerseyInvocation.java:91)
    at org.glassfish.jersey.client.JerseyInvocation$2.call(JerseyInvocation.java:687)
    at org.glassfish.jersey.internal.Errors.process(Errors.java:315)
    at org.glassfish.jersey.internal.Errors.process(Errors.java:297)
    at org.glassfish.jersey.internal.Errors.process(Errors.java:228)
    at org.glassfish.jersey.process.internal.RequestScope.runInScope(RequestScope.java:444)
    at org.glassfish.jersey.client.JerseyInvocation.invoke(JerseyInvocation.java:683)
    at org.glassfish.jersey.client.JerseyInvocation$Builder.method(JerseyInvocation.java:411)
    at org.glassfish.jersey.client.JerseyInvocation$Builder.get(JerseyInvocation.java:307)
    at de.vogella.jersey.jaxb.client.TodoTest.main(TodoTest.java:25)

错误在构建(null);

2 个答案:

答案 0 :(得分:0)

根据您提供的说明存在类型不匹配

  1. 类型不匹配#1

    For build() - The method build(Object[]) in the type UriBuilder is not applicable for the arguments ()
    

    这意味着该方法需要Object数组,但您没有传递任何内容。

  2. 类型不匹配#2

    For accept() - The method accept(String[]) in the type Invocation.Builder is not applicable for the arguments (String)
    

    这意味着该方法需要String数组,但您传递的是简单的String

答案 1 :(得分:0)

好的,所以我现在有点惭愧。

由于@Kaliappan说它的URI问题,我试图得到这个

http://localhost:8080/_com.vogella.jersey.jaxb

但我有

_

现在我有了这个URI

http://localhost:8080/com.vogella.jersey.jaxb

一切正常。非常感谢,你们都很棒!