博客
关于我
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启动失败解决过程
查看>>
MySQL启动失败:Can't start server: Bind on TCP/IP port
查看>>
mysql启动报错
查看>>
mysql启动报错The server quit without updating PID file几种解决办法
查看>>
MySQL命令行登陆,远程登陆MySQL
查看>>
mysql命令:set sql_log_bin=on/off
查看>>
mySQL和Hive的区别
查看>>
MySQL和Java数据类型对应
查看>>
mysql和oorcale日期区间查询【含左右区间问题】
查看>>
MYSQL和ORACLE的一些操作区别
查看>>
mysql和redis之间互相备份
查看>>
MySQL和SQL入门
查看>>
mysql在centos下用命令批量导入报错_Variable ‘character_set_client‘ can‘t be set to the value of ‘---linux工作笔记042
查看>>
Mysql在Linux运行时新增配置文件提示:World-wrirable config file ‘/etc/mysql/conf.d/my.cnf‘ is ignored 权限过高导致
查看>>
Mysql在Windows上离线安装与配置
查看>>
MySQL在渗透测试中的应用
查看>>
Mysql在离线安装时启动失败:mysql服务无法启动,服务没有报告任何错误
查看>>
Mysql在离线安装时提示:error: Found option without preceding group in config file
查看>>
MySQL基于SSL的主从复制
查看>>
Mysql基本操作
查看>>