测试Rest服务时出错

时间:2015-01-27 16:41:12

标签: java eclipse rest tomcat7

我正在写简单的Helloworld Rest课程。我正在使用eclipse和tomcat 7.我添加了jersy jar。 我编写了java程序来测试Rest服务。我在线程“main”com.sun.jersey.api.client.UniformInterfaceException中得到名为Exception的异常:

我正在学习本教程。我给了正确的URL,但我仍然不知道为什么我会收到此错误。

我的休息班是     package cs9322.simple.rest.hello;

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

@Path("/helloworld")
public class HelloWorld {
@GET
@Produces(MediaType.TEXT_XML)
public String sayHelloXML() { 
   return "<?xml version=\"1.0\"?>" + "<msg>" + "Hello World in REST" + "    
}
@GET
@Produces(MediaType.TEXT_HTML)
public String sayHelloHtml() {
    return "<html> " + "<title>" + "Hello Jersey" + "</title>"
            + "<body><h1>" + "Hello World in REST" + "</body></h1>" + "
}

}
My client class:
package cs9322.simple.rest.hello.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());
System.out.println(service.path("rest").path("helloworld").accept(
MediaType.TEXT_PLAIN).get(ClientResponse.class).toString());    
System.out.println(service.path("rest").path("helloworld").accept(
MediaType.TEXT_XML).get(String.class));
    // Accept Header set to HTML
System.out.println(service.path("rest").path("helloworld").accept(
            MediaType.TEXT_HTML).get(String.class));

}

// Here, the Web application root ... 
// (then the remainder of the path should contain 'rest/*')
private static URI getBaseURI() {
return UriBuilder.fromUri(
"http://localhost:8080/cs9322.simple.rest.hello").build();
}

}

0 个答案:

没有答案