Я начал следующий vogella учебник для REST в Java ссылке: 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)
Я попытался для этого необходимо принять() -
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(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);
Это проблема с URL. что URL-адрес убьет браузер ...? что-то вроде этого.?. http: // localhost: 8080/_com.vogella.jersey.jaxb/path/todo – Kaliappan
http: // localhost: 8080/com.vogella.jersey.jaxb/rest/todo и я получаю этот ответ xml version = "1.0" encoding = "UTF-8" standalone = "true"?> - Это мой первый TODO описание> Это мой первый TODO –
Кажется, вы дали подчёркивание перед ком в TodoTest класса. Так что url будет localhost: 8080/_com.vogella.jersey.jaxb/rest/todo – Kaliappan