JAX-RS之queryparam、PathParam、DefaultValue、FormParam、Context、RestController等
这几天做东西接触了JAX-RS的东西,没有系统的从开始就学,只是单纯去复制粘贴的用,主要用到了几个Annotations变量,具体如下:
queryparam、PathParam、FormParam、Context、RestController。下面就分别解释下他们的用法:
1.@queryparam
Path("/users")
public class UserService {
@GET
@Path("/query")
public Response getUsers(
@QueryParam("from") int from,
@QueryParam("to") int to,
@QueryParam("orderBy") List<String> orderBy) {
return Response
.status(200)
.entity("getUsers is called, from : " + from + ", to : " + to
+ ", orderBy" + orderBy.toString()).build();
}
}
这里,在url中输入"/users/query/from=1&to=100&orderBy=name&orderBy=age",则输出一下结果:
getUsers is called, from : 1, to : 100, orderBy[name, age]
这里,也可以以动态方式获得,如下:
@Path("/users")
public class UserService {
@GET
@Path("/query")
public Response getUsers(@Context UriInfo info) {
String from = info.getQueryParameters().getFirst("from");
String to = info.getQueryParameters().getFirst("to");
List<String> orderBy = info.getQueryParameters().get("orderBy");
return Response
.status(200)
.entity("getUsers is called, from : " + from + ", to : " + to
+ ", orderBy" + orderBy.toString()).build();
}
URL:users/query?from=100&to=200&orderBy=age&orderBy=name
输出为:
getUsers is called, from : 100, to : 200, orderBy[age, name]
注意这里把orderby后的两个参数读入为LIST处理了.
2.@pathParam
与@queryParam类似,但是,queryparam在url中是用键值对来传递,而在pathParam中是只出现值而不出现参数,
url如是:"/users/2011/20/10",例如:
@GET
@Path("{year}/{month}/{day}")
public Response getUserHistory(
@PathParam("year") int year,
@PathParam("month") int month,
@PathParam("day") int day) { String date = year + "/" + month + "/" + day; return Response.status(200)
.entity("getUserHistory is called, year/month/day : " + date)
.build(); }
这里通过上面的url将输出:
getUserHistory is called, year/month/day : 2011/20/10
可以用多个param,并且若path中有相同参数名,则采用最接近的参数名所对应的值,如
@Path("/customers/{id}")
public class CustomerResource {
@Path("/address/{id}")
@Produces("text/plain")
@GET
public String getAddress(@PathParam("id") String addressId) {...}
}
如:customers/123/address/456 , 则 addressId 的值为456.
3.@DefaultValue
@Path("/users")
public class UserService {
@GET
@Path("/query")
public Response getUsers(
@DefaultValue("1000") @QueryParam("from") int from,
@DefaultValue("999")@QueryParam("to") int to,
@DefaultValue("name") @QueryParam("orderBy") List<String> orderBy) {
return Response
.status(200)
.entity("getUsers is called, from : " + from + ", to : " + to
+ ", orderBy" + orderBy.toString()).build();
}
输入url为:/users/query
输出为:
getUsers is called, from : 1000, to : 999, orderBy[name]
4.@FormParam
FormParam用于提取post请求中的form数据,具体举例如下:
<FORM action="http://example.com/customers" method="post">
<P>
First name: <INPUT type="text" name="firstname"><BR>
Last name: <INPUT type="text" name="lastname"><BR>
<INPUT type="submit" value="Send">
</P>
</FORM>
@Path("/customers")
public class CustomerResource {
@POST
public void createCustomer(@FormParam("firstname") String first,
@FormParam("lastname") String last) {
...
}
}
5.@context
当jax-rs服务基于servlet发布的时候 ,还可以通过@Context注入servlet中的ServletConfig , ServletContext ,HttpServletRequest , HttpServletResponse
然后REST就可以通过sessionid来保持住用户状态。举例如下:
@Path("UserContext")
public class UserContext {
@Context UriInfo uriInfo;
@Context HttpHeaders httpHeaders;
@Context SecurityContext sc;
@Context Request req;
@Context Response resp;
@Context HttpServletResponse response;
@Context HttpServletRequest request;
@GET
public String hi(@QueryParam("name") String yourName ){
if(yourName!=null)
request.getSession().setAttribute("name", yourName);
String username = (String) request.getSession().getAttribute("name");
if(username!=null){
System.out.println(request.getSession().getId() + ":" + username);
}
else{
System.out.println(request.getSession().getId() + "没有用户");
}
return null;
}
}
<!--在web.xml加入-->
<servlet>
<display-name>JAX-RS REST Servlet</display-name>
<servlet-name>JAX-RS REST Servlet</servlet-name>
<servlet-class>com.sun.jersey.spi.container.servlet.ServletContainer</servlet-class>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>JAX-RS REST Servlet</servlet-name>
<url-pattern>/services/*</url-pattern>
</servlet-mapping>
在url中输入:”/rest/services/UserContext“则输出:
A46756539D2E39CC2CFFCB3FE1C99E70没有用户
若在url中输入:http://localhost:8080/rest/services/UserContext?name=hello
则输出:
A46756539D2E39CC2CFFCB3FE1C99E70:hello
6.@RestController
它继承自@Controller注解,我们可以开发REST服务的时候不需要使用@Controller而专门的@RestController
定义如下:
@Target(value=TYPE)
@Retention(value=RUNTIME)
@Documented
@Controller
@ResponseBody
public @interface RestController
7.还有cookieparam和headerparam等,此处没怎么接触,就先不学习了。
JAX-RS之queryparam、PathParam、DefaultValue、FormParam、Context、RestController等的更多相关文章
- jax-rs中的一些参数标注简介(@PathParam,@QueryParam,@MatrixParam,@HeaderParam,@FormParam,@CookieParam)
先复习一下url的组成: scheme:[//[user:password@]host[:port]][/]path[?query][#fragment] jax-rs anotation @Path ...
- [REST Jersey] @QueryParam Demo
This demo sourced from the jersey tutor. https://jersey.java.net/documentation/latest/jaxrs-resource ...
- React 新 Context API 在前端状态管理的实践
本文转载至:今日头条技术博客 众所周知,React的单向数据流模式导致状态只能一级一级的由父组件传递到子组件,在大中型应用中较为繁琐不好管理,通常我们需要使用Redux来帮助我们进行管理,然而随着Re ...
- React 全新的 Context API
Context API 可以说是 React 中最有趣的一个特性了.一方面很多流行的框架(例如react-redux.mobx-react.react-router等)都在使用它:另一方面官方文档中却 ...
- Apache CXF 3.0: CDI 1.1 Support as Alternative to Spring--reference
With Apache CXF 3.0 just being released a couple of weeks ago, the project makes yet another importa ...
- REST 在 Java 中的使用
REST是一种混合的架构风格,它的由来以及它的架构元素在笔者的前一篇文章<REST 架构风格的由来 & 元素>中已经描述了.本篇主要描述一下J2EE对REST的支持. Java是在 ...
- Jersey MVC
Jersey是JAX-RS(JavaAPI for RESTful Service)标准的一个实现,用于开发RESTful Web Application.可以参考JAX-RS的介绍(http://w ...
- Spring boot中使用springfox来生成Swagger Specification小结
Rest接口对应Swagger Specification路径获取办法: 根据location的值获取api json描述文件 也许有同学会问,为什么搞的这么麻烦,api json描述文件不就是h ...
- RestEasy简介
RestEasy简介 RestEasy技术说明 简介 RESTEasy RESTEasy是JBoss的一个开源项目,提供各种框架帮助你构建RESTful Web Services和RESTful Ja ...
随机推荐
- Servlet容器初始化IOC容器
<!-- ServletContext参数,配置Ioc容器的xml文件名 --> <context-param> <param-name>contextConfig ...
- (补充一)CountDownLatch
引言: 在学习单例模式时候,用到了锁synchronized的概念,在多线程中又用到了CountDownLatch的概念 CountDownLatch是一个同步工具类,它允许一个或多个线程一直等待, ...
- php。。。
我可能不会是一个合格的程序员,因为不够专一,学的种类多,精通的却很少,现在我要做为一个php程序员,专注起航了...接下来半年全力以赴,做出成绩吧. 另外之前的狂刷要一千题也要开始每天更新了,最难的就 ...
- linux ioctl()函数
我这里说的ioctl函数是指驱动程序里的,因为我不知道还有没有别的场合用到了它,所以就规定了我们讨论的范围.写这篇文章是因为我前一阵子被ioctl给搞混了,这几天才弄明白它,于是在这里清理一下头脑. ...
- Python基础笔记系列一:基本工具与表达式
本系列教程供个人学习笔记使用,如果您要浏览可能需要其它编程语言基础(如C语言),why?因为我写得烂啊,只有我自己看得懂!! 工具基础(Windows系统下)传送门:Python基础笔记系列四:工具的 ...
- XML DOM解析 基础概念
DOM和SAX W3C制定了一套书写XML分析器的标准接口规范——DOM. 除此以外,XML_DEV邮件列表中的成员根据应用的需求也自发地定义了一套对XML文档进行操作的接口规范——SAX. 这两种接 ...
- vue2 遇到的问题汇集ing
1 .子路由 { path: '/order-list', //订单列表 name: "order-list", component(resolve) { require.ensu ...
- freemarker报 java.io.FileNotFoundException:及TemplateLoader使用
使用过freemarker的肯定其见过如下情况: java.io.FileNotFoundException: Template xxx.ftl not found. 模板找不到.可能你会认为我明明指 ...
- GAN作用——在我做安全的看来,就是做数据拟合、数据增强
from:https://www.zhihu.com/question/56171002/answer/155777359 GAN的作用,也就是为什么GAN会火了(有部分原因可能是因为Lecun的赞赏 ...
- linux下vim对于意外退出的文档的再次开启
转载的: 1.对于同一个文件如果上次已经打开,而未关闭的情况下,又打开该文件进行编辑时,会出现如下提醒: 这是由于已经打开但未闭关的文件,会在其目录下出现一个.swp的文件,由于是属于隐藏文件,可以用 ...