-
tomcat发布基于jersey的WebService(二)
普通类 -
- 支持
- 批判
- 提问
- 解释
- 补充
- 删除
-
-
tomcat发布基于jersey的WebService(二)
测试了几个get和post方式的webservice请求,并模拟客户端测 试调用情况。在google中找到了这么一篇相关的文章:
-
REST: CRUD with JAX-RS (Jersey)
结果做到一半出了问题,服务端总是获取不到客户端请求的数据流内容!调试了一个下午未果,却在第二天如梦方醒般的找到了原因。教训1:学习下http协 议;教训2:老外的东西不一定是对的。
周末的 测试主要还是基于jersey的webservice服务。对get和post两种方式发送请求的几种情况都做了简单的示例(上传文件的情况未测试,待续)。
service端:
view plaincopy to clipboardprint?
•••••••••10••••••••20••••••••30••••••••40••••••••50••••••••60••••••••70••••••••80••••••••90••••••••100•••••••110•••••••120•••••••130•••••••140•••••••150
1. @Path("/hello")
2. public class HelloService {
3. @GET
4. @Produces("text/plain")
5. public String helloWorld(){
6. return "hello world";
7. }
8. /*
9. * post param test
10. */
11. @POST
12. @Path("echo")
13. @Consumes("application/x-www-form-urlencoded")
14. public String echo(@FormParam("msg") String msg){
15. return "are you say "+msg;
16. }
17. /*
18. * get param test
19. */
20. @GET
21. @Path("sex")
22. @Produces("text/plain")
23. public String getSex(@PathParam("name") String name){
24. return "male";
25. }
26.
27. /*
28. * get {} request
29. * http://houfeng:8080/jerseyWebServiceTest/services/hello/age/houfeng
30. */
31. @GET
32. @Path("age/{name}")
33. @Produces("text/plain")
34. public String getAge(@PathParam("name") String name){
35. return "18";
36. }
37.
38.
39. /*
40. * get {} request
41. * http://houfeng:8080/jerseyWebServiceTest/services/hello/223232323
42. */
43. @GET
44. @Path ("{id}")
45. @Produces ("application/xml")
46. public StreamingOutput retrieveCustomer(@PathParam ("id") String customerId) {
47. String customerDetails = "hou,feng,232";
48. final String[] details = customerDetails.split(",");
49. return new StreamingOutput() {
50. public void write(OutputStream outputStream) {
51. PrintWriter out = new PrintWriter(outputStream);
52. out.println("<?xml version=\"1.0\" encoding=\"UTF-8\"?>");
53. out.println("<customer>");
54. out.println("<firstname>" + details[0] + "</firstname>");
55. out.println("<lastname>" + details[1] + "</lastname>");
56. out.println("<zipcode>" + details[2] + "</zipcode>");
57. out.println("</customer>");
58. out.close();
59. }
60. };
61. }
62.
63.
64. // get vs post
65.
66. @GET
67. @Path("test_get")
68. @Produces("text/plain")
69. public String getTest1(@PathParam("name") String name, @Context HttpServletRequest request){
70. System.out.println("name:"+name);// null
71. String result;
72. result = request.getParameter("name");
73. System.out.println("name="+result); //houfeng
74. result+= "--------"+request.getContextPath();
75. return result;
76. }
77.
78. /*
79. * get 方式 正确的获取参数方 法 @QueryParam 或者 用 request; url里有参数的用PathParam
80. */
81. @GET
82. @Path("test_get2")
83. @Produces("text/plain")
84. public String getTest11(@QueryParam("name") String name, @Context HttpServletRequest request){
85. System.out.println("name:"+name);// houfeng
86. String result;
87. result = request.getParameter("name");
88. System.out.println("name="+result); //houfeng
89. result+= "--------"+request.getContextPath();
90. return result;
91. }
92.
93.
94. @POST
95. @Path("test_post1")
96. @Consumes("application/x-www-form-urlencoded")
97. @Produces("text/plain")
98. public String getTest2(@FormParam("name") String name){
99. System.out.println(name);//houfeng
100. String result=name;
101. return result;
102. }
103.
104. @POST
105. @Path("test_post2")
106. @Consumes("application/x-www-form-urlencoded")
107. @Produces("text/plain")
108. public String getTest22(@QueryParam("name") String name){
109. System.out.println("name:"+name);//houfeng,但是有警告。提示用FormParam
110. String result = name;
111. return result;
112. }
113.
114.
115. @POST
116. @Path("test_post3")
117. @Produces("text/plain")
118. public String getTest2222(String entity, @Context HttpServletRequest request){
119. System.out.println("entity:"+entity);//hello 传入方 式:resource.entity("hello").post(String.class);
120. String result;
121. result= "--------"+request.getContextPath();
122. return result;
123. }
124.
125. @POST
126. @Path("test_post4")
127. //@Consumes("application /xml"),这样就会出错;@Consumes("application/x-www-form-urlencoded") 可以。
128. @Produces("text/plain")
129. public String getTest22222(InputStream is, @Context HttpServletRequest request) throws Exception{
130. byte[] buf = new byte[is.available()];
131. is.read(buf);
132. System.out.println("buf:"+new String(buf));
133. String result;
134. result= "--------"+request.getContextPath();
135. return result;
136. }
客户端可以采用两种方式测试。
1,采用jersey实现的测试api:jersey-twitter-client-1.0-SNAPSHOT-jar-with-dependencies.jar
2,采用apache httpclient 模拟客户端的各种请求。
上面提到的参考e文中是采用的第二种方式。在这里我使用jersey测试api来实现。
view plaincopy to clipboardprint?
1. public void testHelloService() throws URISyntaxException {
2. Client client = Client.create();
3. URI u = new URI("http://houfeng:8080/jerseyWebServiceTest/services/hello");
4. System.out.println(u);
5. WebResource resource = client.resource(u);
6. //get
7. String result = resource.get(String.class);
8. System.out.println(result);
9.
10. //get param
11. u = new URI("http://houfeng:8080/jerseyWebServiceTest/services/hello/sex");
12. System.out.println(u);
13. resource = client.resource(u);
14. MultivaluedMapImpl params = new MultivaluedMapImpl();
15. params.add("name", "houfeng");
16. result = resource.queryParams(params).get(String.class);
17. System.out.println(result);
18.
19. u =new URI("http://houfeng:8080/jerseyWebServiceTest/services/hello/test_get");
20. System.out.println(u);
21. resource = client.resource(u);
22. params = new MultivaluedMapImpl();
23. params.add("name", "houfeng");
24. result = resource.queryParams(params).get(String.class);
25. System.out.println(result);
26.
27. u =new URI("http://houfeng:8080/jerseyWebServiceTest/services/hello/test_get2");
28. System.out.println(u);
29. resource = client.resource(u);
30. params = new MultivaluedMapImpl();
31. params.add("name", "houfeng");
32. result = resource.queryParams(params).get(String.class);
33. System.out.println(result);
34.
35.
36. u =new URI("http://houfeng:8080/jerseyWebServiceTest/services/hello/test_post1");
37. System.out.println(u);
38. resource = client.resource(u);
39. params = new MultivaluedMapImpl();
40. params.add("name", "houfeng");
41. result = resource.type(MediaType.APPLICATION_FORM_URLENCODED).post(String.class,params);
42. System.out.println(result);
43.
44. u =new URI("http://houfeng:8080/jerseyWebServiceTest/services/hello/test_post2");
45. System.out.println(u);
46. resource = client.resource(u);
47. params = new MultivaluedMapImpl();
48. params.add("name", "houfeng");
49. result = resource.queryParams(params).type(MediaType.APPLICATION_FORM_URLENCODED).post(String.class);
50. System.out.println(result);
51.
52. u =new URI("http://houfeng:8080/jerseyWebServiceTest/services/hello/test_post3");
53. System.out.println(u);
54. resource = client.resource(u);
55. result = resource.entity("hello").post(String.class);
56. System.out.println(result);
57.
58. u =new URI("http://houfeng:8080/jerseyWebServiceTest/services/hello/test_post4");
59. System.out.println(u);
60. resource = client.resource(u);
61. String buf = "inputstream content.";
62. ByteArrayInputStream bais = new ByteArrayInputStream(buf.getBytes());
63. result = resource.entity(bais).post(String.class);
64. System.out.println(result);
65. }
过程中遇到的问题就是提交流的时候,错误的参考了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稍后再学习)。
view plaincopy to clipboardprint?
1. 严 重: StandardWrapper.Throwable
2. com.sun.jersey.spi.service.ServiceConfigurationError: jersey-server-components: A dependent class, org/jvnet/mimepull/MIMEParsingException, of the class com.sun.jersey.multipart.impl.MultiPartReader implementing the provider class java.lang.Object is not found. The provider implementation is ignored.
3. at com.sun.jersey.spi.service.ServiceFinder.fail(ServiceFinder.java:388)
4. at com.sun.jersey.spi.service.ServiceFinder.access$200(ServiceFinder.java:144)
5. at com.sun.jersey.spi.service.ServiceFinder$LazyClassIterator.next(ServiceFinder.java:595)
6. at com.sun.jersey.spi.service.ServiceFinder$LazyClassIterator.next(ServiceFinder.java:571)
7. at com.sun.jersey.spi.service.ServiceFinder.toClassArray(ServiceFinder.java:374)
8. at com.sun.jersey.server.impl.application.WebApplicationImpl.initiate(WebApplicationImpl.java:437)
9. at com.sun.jersey.server.impl.application.WebApplicationImpl.initiate(WebApplicationImpl.java:420)
10. at com.sun.jersey.spi.container.servlet.ServletContainer.initiate(ServletContainer.java:377)
11. at com.sun.jersey.spi.container.servlet.ServletContainer$InternalWebComponent.initiate(ServletContainer.java:242)
12. at com.sun.jersey.spi.container.servlet.WebComponent.load(WebComponent.java:466)
13. at com.sun.jersey.spi.container.servlet.WebComponent.init(WebComponent.java:182)
14. at com.sun.jersey.spi.container.servlet.ServletContainer.init(ServletContainer.java:281)
15. at com.sun.jersey.spi.container.servlet.ServletContainer.init(ServletContainer.java:442)
16. at javax.servlet.GenericServlet.init(GenericServlet.java:212)
17. at org.apache.catalina.core.StandardWrapper.loadServlet(StandardWrapper.java:1161)
18. at org.apache.catalina.core.StandardWrapper.load(StandardWrapper.java:981)
19. at org.apache.catalina.core.StandardContext.loadOnStartup(StandardContext.java:4042)
20. at org.apache.catalina.core.StandardContext.start(StandardContext.java:4348)
21. at org.apache.catalina.core.ContainerBase.addChildInternal(ContainerBase.java:791)
22. at org.apache.catalina.core.ContainerBase.addChild(ContainerBase.java:771)
23. at org.apache.catalina.core.StandardHost.addChild(StandardHost.java:525)
24. at org.apache.catalina.startup.HostConfig.deployDirectory(HostConfig.java:920)
25. at org.apache.catalina.startup.HostConfig.deployDirectories(HostConfig.java:883)
26. at org.apache.catalina.startup.HostConfig.deployApps(HostConfig.java:492)
27. at org.apache.catalina.startup.HostConfig.start(HostConfig.java:1138)
28. at org.apache.catalina.startup.HostConfig.lifecycleEvent(HostConfig.java:311)
29. at org.apache.catalina.util.LifecycleSupport.fireLifecycleEvent(LifecycleSupport.java:117)
30. at org.apache.catalina.core.ContainerBase.start(ContainerBase.java:1053)
31. at org.apache.catalina.core.StandardHost.start(StandardHost.java:719)
32. at org.apache.catalina.core.ContainerBase.start(ContainerBase.java:1045)
33. at org.apache.catalina.core.StandardEngine.start(StandardEngine.java:443)
34. at org.apache.catalina.core.StandardService.start(StandardService.java:516)
35. at org.apache.catalina.core.StandardServer.start(StandardServer.java:710)
36. at org.apache.catalina.startup.Catalina.start(Catalina.java:566)
37. at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
38. at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
39. at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
40. at java.lang.reflect.Method.invoke(Unknown Source)
41. at org.apache.catalina.startup.Bootstrap.start(Bootstrap.java:288)
42. at org.apache.catalina.startup.Bootstrap.main(Bootstrap.java:413)
43. 2009-11-16 19:19:19 org.apache.catalina.core.StandardContext loadOnStartup
44. 严 重: Servlet /jerseyWebServiceTest threw load() exception
45. com.sun.jersey.spi.service.ServiceConfigurationError: jersey-server-components: A dependent class, org/jvnet/mimepull/MIMEParsingException, of the class com.sun.jersey.multipart.impl.MultiPartReader implementing the provider class java.lang.Object is not found. The provider implementation is ignored.
46. at com.sun.jersey.spi.service.ServiceFinder.fail(ServiceFinder.java:388)
47. at com.sun.jersey.spi.service.ServiceFinder.access$200(ServiceFinder.java:144)
48. at com.sun.jersey.spi.service.ServiceFinder$LazyClassIterator.next(ServiceFinder.java:595)
49. at com.sun.jersey.spi.service.ServiceFinder$LazyClassIterator.next(ServiceFinder.java:571)
50. at com.sun.jersey.spi.service.ServiceFinder.toClassArray(ServiceFinder.java:374)
51. at com.sun.jersey.server.impl.application.WebApplicationImpl.initiate(WebApplicationImpl.java:437)
52. at com.sun.jersey.server.impl.application.WebApplicationImpl.initiate(WebApplicationImpl.java:420)
53. at com.sun.jersey.spi.container.servlet.ServletContainer.initiate(ServletContainer.java:377)
54. at com.sun.jersey.spi.container.servlet.ServletContainer$InternalWebComponent.initiate(ServletContainer.java:242)
55. at com.sun.jersey.spi.container.servlet.WebComponent.load(WebComponent.java:466)
56. at com.sun.jersey.spi.container.servlet.WebComponent.init(WebComponent.java:182)
57. at com.sun.jersey.spi.container.servlet.ServletContainer.init(ServletContainer.java:281)
58. at com.sun.jersey.spi.container.servlet.ServletContainer.init(ServletContainer.java:442)
59. at javax.servlet.GenericServlet.init(GenericServlet.java:212)
60. at org.apache.catalina.core.StandardWrapper.loadServlet(StandardWrapper.java:1161)
61. at org.apache.catalina.core.StandardWrapper.load(StandardWrapper.java:981)
62. at org.apache.catalina.core.StandardContext.loadOnStartup(StandardContext.java:4042)
63. at org.apache.catalina.core.StandardContext.start(StandardContext.java:4348)
64. at org.apache.catalina.core.ContainerBase.addChildInternal(ContainerBase.java:791)
65. at org.apache.catalina.core.ContainerBase.addChild(ContainerBase.java:771)
66. at org.apache.catalina.core.StandardHost.addChild(StandardHost.java:525)
67. at org.apache.catalina.startup.HostConfig.deployDirectory(HostConfig.java:920)
68. at org.apache.catalina.startup.HostConfig.deployDirectories(HostConfig.java:883)
69. at org.apache.catalina.startup.HostConfig.deployApps(HostConfig.java:492)
70. at org.apache.catalina.startup.HostConfig.start(HostConfig.java:1138)
71. at org.apache.catalina.startup.HostConfig.lifecycleEvent(HostConfig.java:311)
72. at org.apache.catalina.util.LifecycleSupport.fireLifecycleEvent(LifecycleSupport.java:117)
73. at org.apache.catalina.core.ContainerBase.start(ContainerBase.java:1053)
74. at org.apache.catalina.core.StandardHost.start(StandardHost.java:719)
75. at org.apache.catalina.core.ContainerBase.start(ContainerBase.java:1045)
76. at org.apache.catalina.core.StandardEngine.start(StandardEngine.java:443)
77. at org.apache.catalina.core.StandardService.start(StandardService.java:516)
78. at org.apache.catalina.core.StandardServer.start(StandardServer.java:710)
79. at org.apache.catalina.startup.Catalina.start(Catalina.java:566)
80. at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
81. at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
82. at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
83. at java.lang.reflect.Method.invoke(Unknown Source)
84. at org.apache.catalina.startup.Bootstrap.start(Bootstrap.java:288)
85. at org.apache.catalina.startup.Bootstrap.main(Bootstrap.java:413)
很奇怪的原因,之前的jersey测试都是基于myeclipse自带的tomcat进行部署调试的,未发现任何异常。这次我把jersey应用到以前做的project中,然后部署在我安装的tomcat6.0中,结果部署除了这么个异常!google也找不到任何解答。将jersey测试项目也 部署到tomcat6.0中也出现这样的异常。但是部署在myeclipse自带的tomcat中就不出现这样的异常,深度不解~
就这样折腾了一个下午的时间~
第二天晚上又打开来调试,问题依旧,没办法登陆jersey官网看看,在guide中发现这样一句:
Non-maven developers require:
• jersey-server.jar ,
• jersey-core.jar ,
• jsr311-api.jar ,
• asm.jar
于是将jersey包精简到以上几个,去掉了几个client测试相关的jar和spring支持jar,还有一个jersey- multipart-1.1.2-ea.jar(问题就是个jar)。启动tomcat6,异常消失了。^_^
反复改变部署的jars,直接的方法就是把jersey-multipart-1.1.2-ea.jar去掉。其他的jar目前倒是没有产生影 响。 -
-
- 标签:
- result
- path
- string
- houfeng
- post
- public
- tomcat
- jersey
- http
- webservice
- system.out.println
- hello
-
加入的知识群:
学习元评论 (0条)
聪明如你,不妨在这 发表你的看法与心得 ~