• RestEasy入门小程序


    REST(Representational State Transfer)是 Roy Fielding 提出的一个描述互联系统架构风格的名词。为什么称为 REST?Web 本质上由各种各样的资源组成,资源由 URI 唯一标识。浏览器(或者任何其它类似于浏览器的应用程序)将展示出该资源的一种表现方式,或者一种表现状态。如果用户在该页面中定向到指向其它资源的链接,则将访问该资源,并表现出它的状态。这意味着客户端应用程序随着每个资源表现状态的不同而发生状态转移,也即所谓 REST。

    凡符合REST原则的框架成为RESTful框架。

    为了方便起见,本文使用了RESTeasy作为REST的3PP,和JBOSS7集成。这样不用打很多3pp包了,JBOSS自带。

    1. 建立项目

    Maven命令创建基本项目:

    mvn archetype:generate -DgroupId=com.edi.test -DartifactId=resteasy-test -DarchetypeArtifactId=maven-archetype-quickstart -DinteractiveMode=false
    

    然后再手动创建webapp目录,最终目录结构如下: (请无视eclipse自己创建的.setting文件夹)
    转存失败重新上传取消

    然后打开根目录下的pom.xml,修改如下

    
    
      4.0.0
      
        com.edison.test
        edi-test
        1.0
      
      com.edi.test
      resteasy-test
      1.0-SNAPSHOT
      resteasy-test
      http://maven.apache.org
      war
      
        
          junit
          junit
          4.4
          test
        
    
    
    org.jboss.resteasy
    jaxrs-api
    2.3.0.GA
    provided
    
    
    
    org.jboss.resteasy
    resteasy-multipart-provider
    2.3.0.GA
    provided
    
        
    
    org.jboss.resteasy
    resteasy-jaxb-provider
    2.3.0.GA
    provided
    
    
                javax
                javaee-api
                6.0
        provided
            
      
      
      
            resteasy-test
            
                
                    org.apache.maven.plugins
                    maven-compiler-plugin
                    
                        1.6
                        1.6
                        UTF-8
                    
                
            
        
    


    为了部署到JBoss7,打成war包,然后几个3pp JBoss7都已经默认包含了,不需要打到包里去。
     

    2. 定义交互数据类型

    首先定义交换的XML格式如下:

    
    1
    test
    


    再利用工具生成该XML的XSD文件。可以用这个在线的:http://www.freeformatter.com/xsd-generator.html
    生成xsd如下:

    
    
      
      
        
          
          
        
      
    


    将该xsd保存至resteasy-test/src/main/resources目录,然后利用jdk的xjc命令利用JAXB根据xsd生成java类

    xjc test.xsd -p com.edi.test -d ../java

    -p 参数添加包名 然后在java目录下会生成对应java 文件如下: 转存失败重新上传取消

     

    3. 完成RESTful化

    UserType是XML数据对应的Java类,但是服务器端一般有自己的pojo来封装对象。所以我们在服务器端创建自己的封装类。

    package com.edi.test;
    
    public class MyUser {
    
        private int id;
        private String name;
        
        public int getId() {
            return id;
        }
        public void setId(int id) {
            this.id = id;
        }
        public String getName() {
            return name;
        }
        public void setName(String name) {
            this.name = name;
        }
        
        @Override
        public String toString() {
            return "MyUser [id=" + id + ", name=" + name + "]";
        }
    }
    


    然后实现WebService的处理类,这里我们直接用SLSB

    package com.edi.test;
    
    import java.net.URI;
    import java.util.Map;
    import java.util.concurrent.ConcurrentHashMap;
    
    import javax.ejb.Stateless;
    import javax.ws.rs.Consumes;
    import javax.ws.rs.GET;
    import javax.ws.rs.POST;
    import javax.ws.rs.Path;
    import javax.ws.rs.PathParam;
    import javax.ws.rs.Produces;
    import javax.ws.rs.WebApplicationException;
    import javax.ws.rs.core.Response;
    
    @Stateless
    @Path("/users")
    public class UserHandler {
    
        private static Map map = new ConcurrentHashMap();
        
        @POST
        @Path("/create")
        @Consumes({ "application/xml" })
        public Response addUser(UserType user)
        {
            MyUser myUser = new MyUser();
            myUser.setId(user.getId());
            myUser.setName(user.getName());
            System.out.println(myUser + " is created.");
            map.put(myUser.getId(), myUser);
            return Response.created(URI.create("/users/" + myUser.getId())).build();
        }
        
        @GET
        @Path("/{id}")
        @Produces("application/xml")
        public UserType getUser(@PathParam("id") int id)
        {
            MyUser user = map.get(Integer.valueOf(id));
            if(user==null)
                throw new WebApplicationException(Response.Status.NOT_FOUND);
            UserType u = new UserType();
            u.setId(user.getId());
            u.setName(user.getName());
            return u;
        }
        
    }
    


    @POST和@GET 是对于http中POST和GET方法,resteasy 会自动将@Path里面对应路径的请求转交给给方法处理,并且用JAXB将请求里带的xml解析后转成对应的Java类(前面xjc生成的)作为参数传进来。

    4. 部署

    这里部署时有三种方法: a) 继承application类加上@ApplicationPath("path")注解 创建一个空类继承java.ws.rs.Application,并且标上注解@ApplicationPath()  路径为你想要该Rest请求所listen的路径

    package com.edi.test;
    
    import javax.ws.rs.ApplicationPath;
    import javax.ws.rs.core.Application;
    
    @ApplicationPath("/")
    public class MyApplication extends Application {
    }
    


    b) 继承application类加上web.xml 同样创建一个空类继承Application

    package com.edi.test;
    
    import javax.ws.rs.ApplicationPath;
    import javax.ws.rs.core.Application;
    
    public class MyApplication extends Application {
    }
    


    创建resteasy-test/src/main/webapp/WEB-INF/web.xml ,里面指明listen地址替代@Applicationpath 注解

    
    
    
    
    Restful Web Service for NBI 
    
    resteasy.jndi.resources
    java:app/resteasy-test/UserHandler
    
    
    
    com.edi.test.MyApplication
    /*
    
    
    
            30
        
    
    
    


    java:app/resteasy-test/UserHandler 是我们的WebService类的Portable JNDIName

    c) 只直接用web.xml 创建resteasy-test/src/main/webapp/WEB-INF/web.xml 如下

    
    
    
    
    Restful Web Service for NBI 
    
    resteasy.jndi.resources
    java:app/resteasy-test/UserHandler
    
    
    
    javax.ws.rs.core.Application
    /*
    
    
    
            30
        
    
    
    


     

    5. 测试

    用maven编译打包后放入JBoss7,启动JBoss后,用UnitTest来测试

    package com.edi.test;
    
    import java.io.BufferedReader;
    import java.io.IOException;
    import java.io.InputStreamReader;
    import java.io.OutputStream;
    import java.net.HttpURLConnection;
    import java.net.URL;
    
    import org.junit.Assert;
    import org.junit.Test;
    
    public class TestUserHandler {
    
        public static final String TARGET_POST_URL = "http://127.0.0.1:8080/resteasy-test/users/create";
        public static final String TARGET_GET_URL = "http://127.0.0.1:8080/resteasy-test/users/1";
        
        @Test
        public void testCreate() throws IOException {
            URL url = new java.net.URL(TARGET_POST_URL);
            HttpURLConnection conn = (HttpURLConnection)url.openConnection();
            conn.setRequestMethod("POST");
            conn.setRequestProperty("Content-Type", "application/xml");
            conn.setDoOutput(true);
            conn.setInstanceFollowRedirects(false);
            conn.setConnectTimeout(1000);
            
            String userXML = "1test";
            OutputStream os = conn.getOutputStream();
            os.write(userXML.getBytes());
            os.flush();
            
            Assert.assertEquals(HttpURLConnection.HTTP_CREATED, conn.getResponseCode());
            conn.disconnect();
        }
        
        @Test
        public void testGet() throws IOException {
            testCreate();
            URL url = new java.net.URL(TARGET_GET_URL);
            HttpURLConnection conn = (HttpURLConnection)url.openConnection();
            
            BufferedReader reader = new BufferedReader(new InputStreamReader(conn.getInputStream()));
            System.out.println("Result:");
            String out = null;
            while((out=reader.readLine())!=null){
                System.out.println(out);
            }
            Assert.assertEquals(HttpURLConnection.HTTP_OK, conn.getResponseCode());
            conn.disconnect();
        }
    
    }
    


    好吧,这只是测试代码,别要求我写太认真……
    当然,你也可以把该测试类放到reseteasy-test/src/test/对应路径下,然后引入嵌入式容器来做测试,比如Jetty或openejb。这里就不多叙述了。

     

  • 相关阅读:
    刷题记录:牛客NC15052求最值
    mmdetection中的一些Python基础知识(super()/***args/**kwargs)
    RedisTemplate缓存List的操作
    百度搜索引擎SEO优化方法
    生产环境重大bug,update加上索引字段会走索引进行更新?还是走全表扫描
    微信小程序连接蓝牙
    Redis-05Redis应用场景
    【每周一测】Java阶段三阶段考试
    k8s学习-Secret(创建、使用、更新、删除等)
    聊一聊Twitter的雪花算法
  • 原文地址:https://blog.csdn.net/weixin_71792169/article/details/127600229