设为首页 收藏本站
查看: 566|回复: 0

[经验分享] tomcat发布基于jersey的WebService(二)

[复制链接]

尚未签到

发表于 2015-11-14 12:23:15 | 显示全部楼层 |阅读模式
  周末继续学习jersey,测试了几个get和post方式的webservice请求,并模拟客户端测试调用情况。在google中找到了这么一篇相关的文章:
  REST: CRUD with JAX-RS (Jersey) 。  结果做到一半出了问题,服务端总是获取不到客户端请求的数据流内容!调试了一个下午未果,却在第二天如梦方醒般的找到了原因。教训1:学习下http协议;教训2:老外的东西不一定是对的。
  
  周末的测试主要还是基于jersey的webservice服务。对get和post两种方式发送请求的几种情况都做了简单的示例(上传文件的情况未测试,待续)。
  
  service端:
  @Path("/hello")public class HelloService {@GET@Produces("text/plain")public String helloWorld(){return "hello world";}/** post param  test*/@POST   @Path("echo")@Consumes("application/x-www-form-urlencoded")  public String echo(@FormParam("msg") String msg){return "are you say "+msg;}/** get param test*/@GET@Path("sex")@Produces("text/plain")public String getSex(@PathParam("name") String name){return "male";}/** get {} request * http://houfeng:8080/jerseyWebServiceTest/services/hello/age/houfeng*/@GET@Path("age/{name}")@Produces("text/plain")public String getAge(@PathParam("name") String name){return "18";}/** get {} request* http://houfeng:8080/jerseyWebServiceTest/services/hello/223232323*/@GET@Path ("{id}")@Produces ("application/xml")public StreamingOutput retrieveCustomer(@PathParam ("id") String customerId) {String customerDetails = "hou,feng,232"; final String[] details = customerDetails.split(","); return new StreamingOutput() {  public void write(OutputStream outputStream) {  PrintWriter out = new PrintWriter(outputStream);out.println("<?xml version=/"1.0/" encoding=/"UTF-8/"?>");out.println("<customer>");out.println("<firstname>" + details[0] + "</firstname>");out.println("<lastname>" + details[1] + "</lastname>");out.println("<zipcode>" + details[2] + "</zipcode>");out.println("</customer>");out.close();} }; }// get  vs  post @GET@Path("test_get")@Produces("text/plain")public String getTest1(@PathParam("name") String name, @Context HttpServletRequest request){System.out.println("name:"+name);// nullString result;result = request.getParameter("name");System.out.println("name="+result); //houfengresult+= "--------"+request.getContextPath(); return result;}/** get 方式 正确的获取参数方法 @QueryParam 或者 用 request; url里有参数的用PathParam*/@GET@Path("test_get2")@Produces("text/plain")public String getTest11(@QueryParam("name") String name, @Context HttpServletRequest request){System.out.println("name:"+name);// houfengString result;result = request.getParameter("name");System.out.println("name="+result); //houfeng  result+= "--------"+request.getContextPath(); return result;}@POST@Path("test_post1")@Consumes("application/x-www-form-urlencoded") @Produces("text/plain")public String getTest2(@FormParam("name") String name){ System.out.println(name);//houfengString result=name;  return result;}@POST@Path("test_post2")@Consumes("application/x-www-form-urlencoded") @Produces("text/plain")public String getTest22(@QueryParam("name") String name){System.out.println("name:"+name);//houfeng,但是有警告。提示用FormParamString result = name; return result;}@POST@Path("test_post3") @Produces("text/plain")public String getTest2222(String entity, @Context HttpServletRequest request){System.out.println("entity:"+entity);//hello 传入方式:resource.entity("hello").post(String.class);String result; result= "--------"+request.getContextPath(); return result;}@POST@Path("test_post4")//@Consumes("application/xml"),这样就会出错;@Consumes("application/x-www-form-urlencoded") 可以。@Produces("text/plain")public String getTest22222(InputStream is, @Context HttpServletRequest request) throws Exception{byte[] buf = new byte[is.available()];is.read(buf);System.out.println("buf:"+new String(buf));String result; result= "--------"+request.getContextPath(); return result;}
  
  客户端可以采用两种方式测试。
  1,采用jersey实现的测试api:jersey-twitter-client-1.0-SNAPSHOT-jar-with-dependencies.jar

  2,采用apache httpclient 模拟客户端的各种请求。
  上面提到的参考e文中是采用的第二种方式。在这里我使用jersey测试api来实现。
  public  void testHelloService() throws URISyntaxException {Client client = Client.create();URI u = new URI("http://houfeng:8080/jerseyWebServiceTest/services/hello");System.out.println(u);WebResource resource = client.resource(u);//getString result = resource.get(String.class);System.out.println(result);//get paramu = new URI("http://houfeng:8080/jerseyWebServiceTest/services/hello/sex");System.out.println(u);resource = client.resource(u);MultivaluedMapImpl params = new MultivaluedMapImpl();params.add("name", "houfeng");result = resource.queryParams(params).get(String.class);System.out.println(result);u =new URI("http://houfeng:8080/jerseyWebServiceTest/services/hello/test_get");System.out.println(u);resource = client.resource(u);params = new MultivaluedMapImpl();params.add("name", "houfeng");result = resource.queryParams(params).get(String.class);System.out.println(result);u =new URI("http://houfeng:8080/jerseyWebServiceTest/services/hello/test_get2");System.out.println(u);resource = client.resource(u);params = new MultivaluedMapImpl();params.add("name", "houfeng");result = resource.queryParams(params).get(String.class);System.out.println(result);u =new URI("http://houfeng:8080/jerseyWebServiceTest/services/hello/test_post1");System.out.println(u);resource = client.resource(u);params = new MultivaluedMapImpl();params.add("name", "houfeng");result = resource.type(MediaType.APPLICATION_FORM_URLENCODED).post(String.class,params);System.out.println(result);u =new URI("http://houfeng:8080/jerseyWebServiceTest/services/hello/test_post2");System.out.println(u);resource = client.resource(u);params = new MultivaluedMapImpl();params.add("name", "houfeng");result = resource.queryParams(params).type(MediaType.APPLICATION_FORM_URLENCODED).post(String.class);System.out.println(result);u =new URI("http://houfeng:8080/jerseyWebServiceTest/services/hello/test_post3");System.out.println(u);resource = client.resource(u); result = resource.entity("hello").post(String.class);System.out.println(result);u =new URI("http://houfeng:8080/jerseyWebServiceTest/services/hello/test_post4");System.out.println(u);resource = client.resource(u); String buf = "inputstream content.";ByteArrayInputStream bais = new ByteArrayInputStream(buf.getBytes());result = resource.entity(bais).post(String.class);System.out.println(result);}
  
  过程中遇到的问题就是提交流的时候,错误的参考了e文中 “@Consumes("application/xml") ”的请求类型! 结果导致service 端 接受请求的方法参数InputStream 得不到内容。换作@Context HttpServeltRequest request 参数也无济于事。于是在网上搜索,在一个国外论坛中有人提到相似的问题“上传文件得不到流里的内容,但是jetty里可以,tomcat里不可以。?”。好像没有太大参考,但我也试了下,还是失败。。。
  今天修改提交类型注解为:@Consumes("application/x-www-form-urlencoded") ,测试通过!终于才恍然大悟:application/xml是客户端接受的内容类型。哎,是应该学习下http协议的相关知识,这样的问题耽误了大半天的时间!
  另外,对于jax-ws中几个注解,简单总结下:
       QueryParam--url ? 后面表示的参数  .  get post 通用.
       PathParam---url中的一部分,例如用{}表示的url中的一部分。get post 通用。
       FormParam---post提交的form表单参数。     用于 post     
    ( 其他几个param稍后再学习)。

  

             版权声明:本文为博主原创文章,未经博主允许不得转载。

运维网声明 1、欢迎大家加入本站运维交流群:群②:261659950 群⑤:202807635 群⑦870801961 群⑧679858003
2、本站所有主题由该帖子作者发表,该帖子作者与运维网享有帖子相关版权
3、所有作品的著作权均归原作者享有,请您和我们一样尊重他人的著作权等合法权益。如果您对作品感到满意,请购买正版
4、禁止制作、复制、发布和传播具有反动、淫秽、色情、暴力、凶杀等内容的信息,一经发现立即删除。若您因此触犯法律,一切后果自负,我们对此不承担任何责任
5、所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其内容的准确性、可靠性、正当性、安全性、合法性等负责,亦不承担任何法律责任
6、所有作品仅供您个人学习、研究或欣赏,不得用于商业或者其他用途,否则,一切后果均由您自己承担,我们对此不承担任何法律责任
7、如涉及侵犯版权等问题,请您及时通知我们,我们将立即采取措施予以解决
8、联系人Email:admin@iyunv.com 网址:www.yunweiku.com

所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其承担任何法律责任,如涉及侵犯版权等问题,请您及时通知我们,我们将立即处理,联系人Email:kefu@iyunv.com,QQ:1061981298 本贴地址:https://www.yunweiku.com/thread-139133-1-1.html 上篇帖子: tomcat文件夹与文件解析 下篇帖子: 关于Tomcat 6的热部署和热加载
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

扫码加入运维网微信交流群X

扫码加入运维网微信交流群

扫描二维码加入运维网微信交流群,最新一手资源尽在官方微信交流群!快快加入我们吧...

扫描微信二维码查看详情

客服E-mail:kefu@iyunv.com 客服QQ:1061981298


QQ群⑦:运维网交流群⑦ QQ群⑧:运维网交流群⑧ k8s群:运维网kubernetes交流群


提醒:禁止发布任何违反国家法律、法规的言论与图片等内容;本站内容均来自个人观点与网络等信息,非本站认同之观点.


本站大部分资源是网友从网上搜集分享而来,其版权均归原作者及其网站所有,我们尊重他人的合法权益,如有内容侵犯您的合法权益,请及时与我们联系进行核实删除!



合作伙伴: 青云cloud

快速回复 返回顶部 返回列表