博客
关于我
JAX-RS之@formparam和@HeaderParam
阅读量:129 次
发布时间:2019-02-26

本文共 2362 字,大约阅读时间需要 7 分钟。

今天继续学习JAX-RS中的@formparam和@headerparam
1 @formparam
    其功能是可以将前端的HTML绑定,先看例子
 
Java代码
  1.   
  2. html>   
  3. <body>   
  4.     <h1>JAX-RS @FormQuery Testing</h1>   
  5.     
  6.     <form action="rest/user/add" method="post">   
  7.            
  8.             Name : <input type="text" name="name" />   
  9.            
  10.   
  11.            
  12.             Age : <input type="text" name="age" />   
  13.            
  14.   
  15.         <input type="submit" value="Add User" />   
  16.     </form>   
  17.     
  18. </body>   
  19. </html>  
html>	

JAX-RS @FormQuery Testing

Name :
Age :
   处理:
  
Java代码
  1. @Path("/user")   
  2. public class UserService {   
  3.     
  4.     @POST  
  5.     @Path("/add")   
  6.     public Response addUser(   
  7.         @FormParam("name") String name,   
  8.         @FormParam("age"int age) {   
  9.     
  10.         return Response.status(200)   
  11.             .entity("addUser is called, name : " + name + ", age : " + age)   
  12.             .build();   
  13.     
  14.     }  
@Path("/user")public class UserService { 	@POST	@Path("/add")	public Response addUser(		@FormParam("name") String name,		@FormParam("age") int age) { 		return Response.status(200)			.entity("addUser is called, name : " + name + ", age : " + age)			.build(); 	}
  对于前端的HTML浏览,比如:
   http://localhost:8080/RESTfulExample/UserForm.html
  会出现普通的HTML表单,当提交按钮时,会转到
http://localhost:8080/RESTfulExample/rest/user/add
这个URL,这样,REST就去匹配addUser方法了,所以就会把表单中提交的数据输出。
2 在JAX-RS中,有两种方法能取得HTTP REQUEST头,
  1)@headparam
  
Java代码
  1.   
  2. @Path("/users")   
  3. public class UserService {   
  4.     
  5.     @GET  
  6.     @Path("/get")   
  7.     public Response addUser(@HeaderParam("user-agent") String userAgent) {   
  8.     
  9.         return Response.status(200)   
  10.             .entity("addUser is called, userAgent : " + userAgent)   
  11.             .build();   
  12.     
  13.     }  
@Path("/users")public class UserService { 	@GET	@Path("/get")	public Response addUser(@HeaderParam("user-agent") String userAgent) { 		return Response.status(200)			.entity("addUser is called, userAgent : " + userAgent)			.build(); 	}
  当访问URL:
  /users/get
会输出HTTP 头信息,即:
  addUser is called, userAgent : Mozilla/5.0 (Windows NT 6.1; rv:5.0) Gecko/20100101 Firefox/5.0
   另外的方法是,
Java代码
  1. @Path("/users")   
  2. public class UserService {   
  3.     
  4.     @GET  
  5.     @Path("/get")   
  6.     public Response addUser(@Context HttpHeaders headers) {   
  7.     
  8.         String userAgent = headers.getRequestHeader("user-agent").get(0);   
  9.     
  10.         return Response.status(200)   
  11.             .entity("addUser is called, userAgent : " + userAgent)   
  12.             .build();   
  13.     
  14.     }  
@Path("/users")public class UserService { 	@GET	@Path("/get")	public Response addUser(@Context HttpHeaders headers) { 		String userAgent = headers.getRequestHeader("user-agent").get(0); 		return Response.status(200)			.entity("addUser is called, userAgent : " + userAgent)			.build(); 	}
   也可以同样达到目的 

转载地址:http://aacf.baihongyu.com/

你可能感兴趣的文章
Mysql学习总结(44)——Linux下如何实现mysql数据库每天自动备份定时备份
查看>>
Mysql学习总结(45)——Mysql视图和事务
查看>>
Mysql学习总结(46)——8种常被忽视的SQL错误用法
查看>>
Mysql学习总结(48)——MySql的日志与备份还原
查看>>
Mysql学习总结(49)——从开发规范、选型、拆分到减压
查看>>
Mysql学习总结(4)——MySql基础知识、存储引擎与常用数据类型
查看>>
Mysql学习总结(50)——Oracle,mysql和SQL Server的区别
查看>>
Mysql学习总结(51)——Linux主机Mysql数据库自动备份
查看>>
Mysql学习总结(52)——最全面的MySQL 索引详解
查看>>
Mysql学习总结(53)——使用MySql开发的Java开发者规范
查看>>
Mysql学习总结(54)——MySQL 集群常用的几种高可用架构方案
查看>>
Mysql学习总结(55)——MySQL 语句大全再温习
查看>>
Mysql学习总结(56)——MySQL用户管理和权限设置
查看>>
Mysql学习总结(57)——MySQL查询当天、本周、本月、上周、本周、上月、距离当前现在6个月数据
查看>>
Mysql学习总结(58)——深入理解Mysql的四种隔离级别
查看>>
Mysql学习总结(59)——数据库分库分表策略总结
查看>>
Mysql学习总结(5)——MySql常用函数大全讲解
查看>>
Mysql学习总结(60)——并发量大、数据量大的互联网业务数据库设计规范总结
查看>>
Mysql学习总结(61)——MySQL优化之DBA级优化整理汇总
查看>>
Mysql学习总结(62)——MySQL连接com.mysql.jdbc.exceptions.jdbc4.CommunicationsException: Communications link问题
查看>>