jersey+grizzly学习笔记

jersey是Sun公司按照JAX-RS规范标准对RESTful架构风格的一种实现,关于RESTful的介绍可以参考RESTful架构,主要作用就是提供了一些标注将一个资源类、一个POJO Java类封装为Web资源。

grizzly是一个web容器,类似tomcat或者glassfish容器。其中grizzly专门解决编写成千上万用户访问服务器时候产生的问题,使用JAVA NIO作为基础。

其中jersey官方入门例子参考jersey官方例子。下面介绍jersey的注解使用。

jersey中有几种常用的接收参数的注解:
@PathParam 接收链接中参数,如"/xxx/{name}/",@PathParm("name")
@QueryParam 接收链接中的普通参数,如"/xxx?name=ttt",@QueryParam("name")
@FormParm 接收post提交中的表单参数
@FormDataParm 上传文件接收文件参数

参数编码的问题,参考这里点击打开链接

GET例子:

@Path("/sayHello")
    @GET
    // The Java method will produce content identified by the MIME Media
    // type "text/plain"
    @Produces("text/plain")
    public String getClichedMessage() {
        // Return some cliched textual content
        return "Hello World";
    }
<strong>get方式可以用@PathParam和@QueryParam两种方式接收参数,不能用@FormParam。</strong>
@GET  
	@Path("{username}")  
	@Produces(MediaType.TEXT_HTML)  
	public String getUserName(@PathParam("username") String userName) {  
	    return "hello "+userName;
	}
	
	@GET  
	@Path("/get1")  
	@Produces(MediaType.TEXT_HTML)  
	public String get1(@QueryParam("msg") String userName) {  
	    return "hello "+userName;
	}
post方式可以使用@FormParam和@FormDataParam两种方式接收参数
@POST     
    @Path("/post")
    @Consumes("application/x-www-form-urlencoded")
	@Produces(MediaType.TEXT_HTML+";charset=utf-8")
	//@Produces("text/plain;charset=utf-8")
    public String echo(@FormParam("msg") String msg){  
        return "are you say "+msg;  
    }
其中GET和POST均有可能出现中文乱码的情况,解决方法是指定产生数据的编码格式,一下两种方式均可:
@Produces(MediaType.TEXT_HTML+";charset=utf-8")
@Produces("text/plain;charset=utf-8")
下面是对常见的媒体格式类型以及其中区别的总结:
常见的媒体格式类型如下:
text/html : HTML格式
text/plain :纯文本格式      
text/xml :  XML格式
image/gif :gif图片格式    
image/jpeg :jpg图片格式 
image/png:png图片格式

以application开头的媒体格式类型:
application/xhtml+xml :XHTML格式
application/xml     : XML数据格式
application/atom+xml  :Atom XML聚合格式    
application/json    : JSON数据格式
application/pdf       :pdf格式  
application/msword  : Word文档格式
application/octet-stream : 二进制流数据(如常见的文件下载)
application/x-www-form-urlencoded : <form encType=””>中默认的encType,form表单数据被编码为key/value格式发送到服务器(表单默认的提交数据的格式)

其中
1、text/html是html格式的正文 
2、text/plain是无格式正文
3、text/xml忽略xml头所指定编码格式而默认采用us-ascii编码
4、application/xml会根据xml头指定的编码格式来编码:



  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值