webservice(resteasy demo)

1.建立web工程MyResteasy

2.导入jar包

0.servlet-api-2.5.jar
1.guice-2.0.jar
2.resteasy-guice-2.1.0.GA.jar
3.jaxrs-api-2.1.0.GA.jar
4.resteasy-jaxrs-2.1.0.GA.jar
5.javassist-3.8.0.GA.jar
6.scannotation-1.0.2.jar

客户端额外加上:
7.commons-codec-1.2.jar
8.commons-httpclient-3.1.jar
9.commons-logging-1.0.4.jar
(测试的时候客户端,服务端jar包都全部导入了)

3.配置web.xml

 <context-param>
     <param-name>resteasy.scan(resteasy.resources)</param-name>
     <param-value>true(*.*.*java)</param-value>
  </context-param>

  <listener>
       <listener-class>
           org.jboss.resteasy.plugins.server.servlet.ResteasyBootstrap
       </listener-class>
   </listener>

    <servlet>
       <servlet-name>Resteasy</servlet-name>
       <servlet-class>
            org.jboss.resteasy.plugins.server.servlet.HttpServletDispatcher
       </servlet-class>
   </servlet>
   <servlet-mapping>
       <servlet-name>Resteasy</servlet-name>
       <url-pattern>/resteasy/*</url-pattern>
   </servlet-mapping>

解释:

   <context-param>
     <param-name>resteasy.scan(resteasy.resources)</param-name>
     <param-value>true(*.*.*java)</param-value>
  </context-param>

(1) param-name写resteasy.scan
param-value写true
这是自动的意思。当匹配到resteasy的路径的时候,会自动找适合的资源类。
(2) (括号里的)param-name写resteasy.resources
param-value写具体的服务端的资源类(如后面的com.zhshch.test.Hello)
当匹配到resteasy的路径的时候,会去找指定的资源类

4.编写服务端资源类
接口:
@Path(“resteasy/resource”)
public interface IHello {

@Path("/hello")
@GET
public String hello();

@Path("/hello/{name}")
@GET
public String helloName(@PathParam("name") String name);

@Path("/obj")
@POST
public String json(String json);

}

解释:
(1)
web.xml配置了

 <servlet-mapping>
       <servlet-name>Resteasy</servlet-name>
       <url-pattern>/resteasy/*</url-pattern>
   </servlet-mapping>

类上面定义了路径 @Path(“resteasy/resource”)
(一定要写resteasy/,因为要和web.xml匹配,如果直接resource的话,就会找不到)
当url输入http://localhost:8080/MyResteasy/resteasy/resource的时候,就会找这个资源

(2)

@Path("/hello")
@GET
public String hello();

http://localhost:8080/MyResteasy/resteasy/resource/hello,会找这个方法
(@GET注解的,可以直接在浏览器查看,@POST标注的,不可以在浏览器查看)

@Path("/hello/{name}")
@GET
public String helloName(@PathParam("name") String name);
{name}这样就作为了参数,@PathParam("name") String name定义使用这个参数

如果URL=http://localhost:8080/MyResteasy/resteasy/resource/hello/zxc
相当于helloName(zxc);把zxc传给了这个方法

@Path("/obj")
@POST
public String json(String json);

对于传递对象,集合比较复杂的,resteasy有相应的配置,这里是个demo,就不深入研究。
这里对于任何对象,都json化,变成json来传递.
这里是post方法

实现类:

public class Hello implements IHello {

public String hello() {
    return "hello" ;
}

public String helloName(String name) {
    String n = null;
    try {
        n = URLDecoder.decode(name,"utf-8");
    } catch (UnsupportedEncodingException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
    return  name ;
}


public String json(String json) {
    String n = null;
    try {
        n = URLDecoder.decode(json,"utf-8");
    } catch (UnsupportedEncodingException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
    return  json ;
}   

}

5.
做好前面几步:
2)导入jar包
3)配置xml
4)配置服务资源类
然后运行web工程,然后在浏览器查看其中的第一个注解的
接口:
@Path(“/hello”)
@GET
public String hello();
实现:
public String hello() {
return “hello” ;
}
一般这个方法就是浏览器测试webservice是否配置成功.
浏览器测试:
http://localhost:8080/MyResteasy/resteasy/resource/hello
如下图
这里写图片描述
webservice配置成功

6.客户端测试

public class TestClient {

public static void main(String[] args) throws Exception{
    testPost();

}
public static void testGet() throws Exception{
    //定义资源访问路径
    String url = "http://localhost:8080/MyResteasy/resteasy/resource/hello/zhshch" ;
    //2.连接目标
    ClientRequest client = new  ClientRequest(url )  ;
    //3.getTarget只能访问注解的@GET方法,postTarget访问@POST方法
    //String.class是返回的类型
    String result = client.getTarget(String.class) ;
    System.out.println(result);
}
public static void testPost() throws Exception{
    String url = "http://localhost:8080/MyResteasy/resteasy/resource/obj" ;
    //对于webservice的调用,因为传输数据涉及到中文,所以发的是要转码,那边接的时候要解码
    String para = "周星驰" ;
    para = URLEncoder.encode(para,"utf-8") ;

    ClientRequest client = new ClientRequest( url )  ;
    //post参数要用下面这种传输
    client.body("text/plain", para) ;
    String result = client.postTarget(String.class) ;
    //同理,传过来的是转码的数据,这边接的时候也要解码
    result = URLDecoder.decode(result,"utf-8") ;
    System.out.println(result);
}

}
对于对象,集合,想测试的可以自己json化试一下。
demo

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值