Jersey Rest服务类型

在Rest服务中,资源类是接收Rest请求并完成响应的核心类,而资源类由Rest服务的“提供者”来调度的,这一定义类似于自定义Servlet类,该类会奖请求分派给指定的Controller/Action类来处理,

根据JAX-RS2.0规范对REST服务场景的定义,将Rest服务分别分为4种类型,如下图

Jersey Rest服务类型

类型1:当服务中没有Application子类时,容器会查找Servlet的子类做为入口,如果Servlet的子类也不存在,则REST服务类型为类型1

类型2:当服务中没有Application子类,但存在Servlet的子类时,则REST服务类型为类型2

类型3:服务中定义了Application的子类,而且这个Application的子类使用@ApplicationPath注解,则REST服务类型为类型3

类型4:如果服务中定义了Application的子类,而且这个Application的子类没有使用@ApplicationPath注解,则REST服务类型为类型4

DEMO

REST服务类型1:相应的逻辑是在服务中同时不存在Application的子类和Servlet的子类,REST服务动态生成一个名为javax.ws.core.Application的Servlet实例,并自动探测匹配资源(注:在没有Application的子类时,在web.xml中定义Servlet是不可少的配置)

package com.example;

import javax.ws.rs.GET;
import javax.ws.rs.Path;
import javax.ws.rs.Produces;
import javax.ws.rs.core.MediaType; /**
* Root resource (exposed at "myresource" path)
*/
@Path("myresource")
public class MyResource { /**
* Method handling HTTP GET requests. The returned object will be sent
* to the client as "text/plain" media type.
*
* @return String that will be returned as a text/plain response.
*/
@GET
@Produces(MediaType.TEXT_PLAIN)
public String getIt() {
return "Got it!";
}
}

Servlet2 web.xml配置

<?xml version="1.0" encoding="UTF-8"?>
<!-- This web.xml file is not required when using Servlet 3.0 container,
see implementation details http://jersey.java.net/nonav/documentation/latest/jax-rs.html -->
<web-app version="2.5" xmlns="http://java.sun.com/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd">
<servlet>
<servlet-name>Jersey Web Application</servlet-name>
<servlet-class>org.glassfish.jersey.servlet.ServletContainer</servlet-class>
<init-param>
<param-name>jersey.config.server.provider.packages</param-name>
<param-value>com.example</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>Jersey Web Application</servlet-name>
<url-pattern>/webapi/*</url-pattern>
</servlet-mapping>
</web-app>

Servlet3 web.xml配置

<?xml version="1.0" encoding="UTF-8"?>
<web-app version="3.0" xmlns="http://java.sun.com/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd">
<servlet>
<servlet-name>javax.ws.rs.core.Application</servlet-name>
</servlet>
<servlet-mapping>
<servlet-name>javax.ws.rs.core.Application</servlet-name>
<url-pattern>/webapi/*</url-pattern>
</servlet-mapping>
</web-app>

index.jsp

<html>
<body>
<h2>Jersey RESTful Web Application!</h2>
<p><a href="webapi/myresource">Jersey resource</a>
<p>Visit <a href="http://jersey.java.net">Project Jersey website</a>
for more information on Jersey!
</body>
</html>

REST服务类型2:不存在Application的子类但存在Servlet的子类

package com.example;

import javax.servlet.annotation.WebInitParam;
import javax.servlet.annotation.WebServlet; import org.glassfish.jersey.servlet.ServletContainer; @WebServlet(
initParams = @WebInitParam(name = "jersey.config.server.provider.packages", value = "com.example"),
urlPatterns = "/webapi/*",
loadOnStartup = 1)
public class AirServlet extends ServletContainer {
private static final long serialVersionUID = 1L;
}

该类继承于org.glassfish.jersey.servlet.ServletContainer,这是Jersey2中Servlet的基类,继承于HttpServlet

REST服务类型3:相应的逻辑是存在Application的子类并且定义了@ApplicationPath注解

Servlet2

package com.example;

import javax.ws.rs.ApplicationPath;
import org.glassfish.jersey.server.ResourceConfig; @ApplicationPath("/webapi/*")
public class AirResourceConfig extends ResourceConfig {
public AirResourceConfig() {
packages("com.example");
}
}
AirResourceConfig 继承于Application的子类 ResourceConfig,此时需要web.xml但内容可以为空,呆需要web-app的基本定义

Servlet3
package com.example;

import java.util.HashSet;
import java.util.Set; import javax.ws.rs.ApplicationPath;
import javax.ws.rs.core.Application; @ApplicationPath("/webapi/*")
public class AirApplication extends Application {
@Override
public Set<Class<?>> getClasses() {
final Set<Class<?>> classes = new HashSet<Class<?>>();
classes.add(MyResource.class);
return classes;
}
}

AirApplication覆盖了getClasses方法,注册了资源类MyResource,这样服务器启动后,MyResource类提供的资源路径将被映射到内存

REST服务类型4:相应的逻辑是一有二无,有存在Application的子类,无Servlet子类,无ApplicationPath注解

Servlet2
package com.example;

import java.util.HashSet;
import java.util.Set; import javax.ws.rs.core.Application; public class AirApplication extends Application {
@Override
public Set<Class<?>> getClasses() {
final Set<Class<?>> classes = new HashSet<Class<?>>();
classes.add(MyResource.class);
return classes;
}
}

  

<?xml version="1.0" encoding="UTF-8"?>
<web-app version="2.5" xmlns="http://java.sun.com/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd">
<servlet>
<servlet-name>Jersey Web Application</servlet-name>
<servlet-class>org.glassfish.jersey.servlet.ServletContainer</servlet-class>
<init-param>
<param-name>javax.ws.rs.Application</param-name>
<param-value>com.example.AirApplication</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>Jersey Web Application</servlet-name>
<url-pattern>/webapi/*</url-pattern>
</servlet-mapping>
</web-app>

Servlet3

package com.example;

import java.util.HashSet;
import java.util.Set; import javax.ws.rs.ApplicationPath;
import javax.ws.rs.core.Application; @ApplicationPath("/webapi/*")
public class AirApplication extends Application {
@Override
public Set<Class<?>> getClasses() {
final Set<Class<?>> classes = new HashSet<Class<?>>();
classes.add(MyResource.class);
return classes;
}
}
上一篇:ABP Framework:移除 EF Core Migrations 项目,统一数据上下文


下一篇:在.NET Core 中收集数据的几种方式