`

jersey 简单使用

 
阅读更多

之前对于Webservice已经看了很多,包括axis,axissession校验(保持会话),cxf,jsr311等等,今天看了jersey,简单的写了一个例子。分享给入门的朋友。


jersey是一种restful框架,使用它为我们提供接口。有三块重要的东西,jersey-server,jersey-core, 集成(与spring等等)

codeing之前,先看看需要什么东西,也就是jar

我用的是maven工程,配置了

<dependency>
<groupId>org.glassfish.jersey.core</groupId>
<artifactId>jersey-server</artifactId>
<version>2.4.1</version>
</dependency>

<dependency>
<groupId>org.glassfish.jersey.core</groupId>
<artifactId>jersey-client</artifactId>
<version>2.4.1</version>
</dependency>

<dependency>
<groupId>com.sun.xml.bind</groupId>
<artifactId>jaxb-impl</artifactId>
<version>2.1.13</version>
</dependency>

<dependency>
<groupId>com.sun.jersey</groupId>
<artifactId>jersey-json</artifactId>
<version>1.17.1</version>
</dependency>


<dependency>
<groupId>com.sun.jersey</groupId>
<artifactId>jersey-core</artifactId>
<version>1.17.1</version>
</dependency>




<dependency>
<groupId>javax.ws.rs</groupId>
<artifactId>javax.ws.rs-api</artifactId>
<version>2.0</version>
</dependency>

都是在jersey开发中需要使用的,我加的比较全面。


说明一下:工程名字jerseydemo,包名 cn.thinkjoy.jerseydemo.resources.

在web.xml中配置

<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>cn.thinkjoy.jerseydemo.resources</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>

然后在包下写类文件:

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

@Path("/hello")
public class HelloResource {
@GET
@Produces(MediaType.TEXT_HTML)
public String sayHello() {
return "<html>hello world ! <br/><font color='red'>111</font></html>";

}

}

相关解释:
    资源类(Resource Class):注意,资源类是一个简单的 Java 对象 (POJO),可以实现任何接口。这增加了许多好处,比如可重用性和简单。
    注释(Annotation):在 javax.ws.rs.* 中定义,是 JAX-RS (JSR 311) 规范的一部分。
    @Path:定义资源基 URI。由上下文根和主机名组成,资源标识符类似于 http://localhost:8080/Jersey/rest/hello。
    @GET:这意味着以下方法可以响应 HTTP GET 方法。
    @Produces:以纯文本方式定义响应内容 MIME 类型。

我用的jetty启动服务,代码如下:
import org.mortbay.jetty.Server;
import org.springside.modules.test.utils.JettyUtils;

public class StartServer {
public static final int PORT = 8080;
public static final String CONTEXT = "/jerseydemo";
public static final String BASE_URL = "http://localhost:8080/jerseydemo";

public static void main(String[] args) throws Exception {
Server server = JettyUtils.buildNormalServer(PORT, CONTEXT);
server.start();
System.out.println("Hit Enter in console to stop server");
try {
if (System.in.read() != 0) {
server.stop();
System.out.println("Server stopped");
}
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}

}
}


运行StartServer之后,在本地浏览器窗口 输入地址: http://localhost:8080/jerseydemo/rest/hello


可以看到返回的结果

结果就不给大家附上了。

这是一个很简单的入门级联系。


分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics