Jersey RESTful Web服务
Jersey是一个RESTFUL请求服务JAVA框架,与常规的JAVA编程使用的struts框架类似,它主要用于处理业务逻辑层。与Struts类似,它同样可以和hibernate,spring框架整合。
由于Struts2+hibernate+spring整合在市场的占有率太高,所以很少一部分人去关注Jersey。所以网上有关于Jersey的介绍很少。但是它确实是一个非常不错的框架。对于请求式服务,对于GET,DELETE请求,你甚至只需要给出一个URI即可完成操作。
举个简单的例子:如果你想获得服务器数据库中的所有数据;那么你可以在浏览器或者利用Ajax的GET方法,将路径设置好;例如:localhost:8080/Student(项目名称)/studentinfo(项目服务总体前缀)/student(处理student对象的签注)/getStudentInfo(最后前缀)。这样就可以获取所有学生信息。你可以选择GET获取的数据的返回类型:JSON,XML,TEXT_HTML(String)..获取之后,你可以通过JS将这些数据塞到html或者jsp页面上。
下面是详解:
web.xml的设置:
<!--定义Jersey的拦截器 -->
<servlet>
<servlet-name>JerseyServlet</servlet-name>
<servlet-class>
com.sun.jersey.spi.spring.container.servlet.SpringServlet
</servlet-class>
<init-param>
<param-name>com.sun.jersey.config.property.packages</param-name>
<!--服务类所在的文件夹 -->
<param-value>com.mirrors.action</param-value><!-- 之所以我定义为com.mirrors.action就是说明此包中类的作用类似于struts中action层类的作用--!>
</init-param>
</servlet>
<servlet-mapping>
<servlet-name>JerseyServlet</servlet-name>
<url-pattern>/new/*</url-pattern><!--项目服务总体前缀 -->
</servlet-mapping>
<welcome-file-list>
<welcome-file>index.jsp</welcome-file>
</welcome-file-list>
<servlet>
<servlet-name>JerseyServlet</servlet-name>
<servlet-class>
com.sun.jersey.spi.spring.container.servlet.SpringServlet
</servlet-class>
<init-param>
<param-name>com.sun.jersey.config.property.packages</param-name>
<!--服务类所在的文件夹 -->
<param-value>com.mirrors.action</param-value><!-- 之所以我定义为com.mirrors.action就是说明此包中类的作用类似于struts中action层类的作用--!>
</init-param>
</servlet>
<servlet-mapping>
<servlet-name>JerseyServlet</servlet-name>
<url-pattern>/new/*</url-pattern><!--项目服务总体前缀 -->
</servlet-mapping>
<welcome-file-list>
<welcome-file>index.jsp</welcome-file>
</welcome-file-list>
StudentAction.java一些代码:
@Component
@Path("/student")//处理student对象的签注
public class StudentAction
{
private StudentDao studentdao;
public void setStudentdaoStudentDao studentdao)
{
this.studentdao =studentdao;
}
@GET//获取方式
@Path("getStudentInfo")//最后前缀
@Path("/student")//处理student对象的签注
public class StudentAction
{
private StudentDao studentdao;
public void setStudentdaoStudentDao studentdao)
{
this.studentdao =studentdao;
}
@GET//获取方式
@Path("getStudentInfo")//最后前缀
@Produces({ MediaType.APPLICATION_JSON })//返回类型为一个Student对象的JSON数组
public List<Student> getTrade()
public List<Student> getTrade()
{
return studentdao.getStudent();
}
return studentdao.getStudent();
}
}
这样一个GET方式的处理就结束了,接下来就是前台提取方式,你可以通过JS控制JSON数组在页面的呈现方式。
Jersey共计有4中处理方式,即:@GET,@POST,@DELETE,@PUT。
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 记一次.NET内存居高不下排查解决与启示
· 探究高空视频全景AR技术的实现原理
· 理解Rust引用及其生命周期标识(上)
· 浏览器原生「磁吸」效果!Anchor Positioning 锚点定位神器解析
· 没有源码,如何修改代码逻辑?
· 全程不用写代码,我用AI程序员写了一个飞机大战
· DeepSeek 开源周回顾「GitHub 热点速览」
· 记一次.NET内存居高不下排查解决与启示
· MongoDB 8.0这个新功能碉堡了,比商业数据库还牛
· .NET10 - 预览版1新功能体验(一)