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);// null
String result;
result = request.getParameter("name");
System.out.println("name="+result); //houfeng
result+= "--------"+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);// houfeng
String 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);//houfeng
String 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,但是有警告。提示用FormParam
String 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来实现。
[java] view plain copy 在CODE上查看代码片派生到我的代码片
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);
//get
String result = resource.get(String.class);
System.out.println(result); //get param
u = 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

jersey获取各个参数的总结的更多相关文章

  1. vue-router2.0 组件之间传参及获取动态参数

    <li v-for=" el in hotLins" > <router-link :to="{path:'details',query: {id:el ...

  2. asp.net 正则获取url参数

    现在有一种场景:Url是数据库里面的,里面带有很多参数,如何获取具体参数的值呢? var uri = new Uri(pageUrl); var queryString = uri.Query; va ...

  3. 【转】javascript浏览器参数的操作,js获取浏览器参数

    原文地址:http://www.haorooms.com/post/js_url_canshu html5修改浏览器地址:http://www.cnblogs.com/JiangXiaoTian/ar ...

  4. 学习SpringMVC——如何获取请求参数

    @RequestParam,你一定见过:@PathVariable,你肯定也知道:@QueryParam,你怎么会不晓得?!还有你熟悉的他(@CookieValue)!她(@ModelAndView) ...

  5. 用JS获取地址栏参数的方法

    采用正则表达式获取地址栏参数: function GetQueryString(name) {      var reg = new RegExp("(^|&)"+ nam ...

  6. js 获取url参数的值

    //获取url参数函数function GetQueryString(name){    var reg = new RegExp("(^|&)"+ name +" ...

  7. js对特殊字符转义、时间格式化、获取URL参数

    /*特殊字符转义*/ function replace_html(str) { var str = str.toString().replace(/&/g, "&" ...

  8. 用JS获取地址栏参数的方法(超级简单)

    方法一:采用正则表达式获取地址栏参数:( 强烈推荐,既实用又方便!) function GetQueryString(name) {      var reg = new RegExp("( ...

  9. 特殊字符转义&时间格式化&获取URL参数

    /*特殊字符转义*/ function htmlspecialchars (str) { var str = str.toString().replace(/&/g, "&& ...

随机推荐

  1. How to Find Processlist Thread id in gdb !!!!!GDB 使用

    https://mysqlentomologist.blogspot.jp/2017/07/           Saturday, July 29, 2017 How to Find Process ...

  2. Remon Spekreijse CSerialPort串口类的修正版2014-01-10

    转自:http://m.blog.csdn.net/blog/itas109/18358297# 2014-1-16阅读691 评论0 如需转载请标明出处:http://blog.csdn.net/i ...

  3. 转: 用 Go 写一个轻量级的 ldap 测试工具

    前言 这是一个轮子. 作为一个在高校里混的 IT,LDAP 我们其实都蛮熟悉的,因为在高校中使用 LDAP 来做统一认证还蛮普遍的.对于 LDAP 的管理员而言,LDAP 的各种操作自然有产品对应的管 ...

  4. Oracle 10g AND Oracle 11g手工建库案例--Oracle 10g

    Oracle 10g AND Oracle 11g手工建库案例--Oracle 10g 系统环境: 操作系统: RedHat EL6 Oracle:  Oracle 10g and Oracle 11 ...

  5. 读取 XML 数据时,超出最大字符串内容长度配额 (8192)

    格式化程序尝试对消息反序列化时引发异常: 尝试对参数 http://www.thermo.com/informatics/xmlns/limswebservice 进行反序列化时出错: Process ...

  6. TFS WorkItem Permission Setting

    TFS非常强大,但是权限设置确实非常的恶心复杂,这貌似是一切NB又傲慢的软件的通病. 那么,在哪里设置 WorkItem 的权限呢? 第一步: 第二步: 第三步,下面你将一目了然. 第四步,Share ...

  7. 特殊汉字“𣸭”引发的对于字符集的思考;mysql字符集;sqlalchemy字符集设置;客户端字符集设置;

    字符集.字符序的概念与联系 在数据的存储上,MySQL提供了不同的字符集支持.而在数据的对比操作上,则提供了不同的字符序支持. MySQL提供了不同级别的设置,包括server级.database级. ...

  8. SVN使用小结

    SVN是Subversion的简称.是一个开放源码的版本号控制系统.在它的管理下,文件和文件夹能够超越时空的限制,权且当作一种奇妙的"时间机器"吧. 基本功能 版本号控制 作为一个 ...

  9. codeforces 560 C Gerald&#39;s Hexagon

    神精度--------这都能过.随便算就好了,根本不用操心 就是把六边形补全成三角形.然后去掉补的三个三角形,然后面积除以边长1的三角形的面积就可以.... #include<map> # ...

  10. 玩转OpenStack

    一.OpenStack包含那些内容 1.预备知识 首先会有虚拟化和云计算的“预备知识”,会介绍 KVM,IaaS 等技术. 2.OpenStack核心 包含OpenStack的架构和和各个核心组件. ...