这里就不对REST做过多的讲解了,有兴趣的朋友可以去
https://jersey.java.net/
查看。下面就直奔主题了……
这个类通过@GET注解的方式注册资源,通过@Produces注解定义返回的类型,这里我们分别返回了文本、xml、html,也通过@Path定义了可以的URL;
参数“com.sun.jersey.config.property.package”定义了包的jersey将查找Web服务类。此属性必须指向你的资源类
1、使用Eclipse创建首个RESTful Webservice
1.1、使用jersey类库创建项目
创建动态的web项目
复制你下载的Jersey的所有jar到WEB/lib文件下
1.2、创建Hello类
内容如下:
package de.demo.jersey.first;
import javax.ws.rs.GET;
import javax.ws.rs.Path;
import javax.ws.rs.Produces;
import javax.ws.rs.core.MediaType;
// 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> ";
}
}
这个类通过@GET注解的方式注册资源,通过@Produces注解定义返回的类型,这里我们分别返回了文本、xml、html,也通过@Path定义了可以的URL;
1.3、修改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" version="2.5">
<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>de.demo.jersey.first</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>
参数“com.sun.jersey.config.property.package”定义了包的jersey将查找Web服务类。此属性必须指向你的资源类
1.4、运行rest service
启动tomcat,在浏览器中输入:http://localhost:8080/JerseyDemo/rest/hello,会显示Hello Jersey。
2、创建客户端
package de.demo.jersey.first.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/JerseyDemo").build();
}
}
效果如下图:
以上就是基本的操作,要想获得更多的信息请访问网址:http://www.vogella.com/articles/REST/article.html#first