摘要:是的开源项目之一,是一个框架。趁今天有空,学习一下。看了两篇博客后,自己写了一个。如果不配置,也可以手动指定,见注释掉的部分。
RESTEasy是JBoss的开源项目之一,是一个RESTful Web Services框架。
趁今天有空,学习一下RESTEasy。看了两篇博客后,自己写了一个demo。
1. 新建一个maven的web项目。
依赖如下:
2. 接着创建两个服务,messageservice和userservice:4.0.0 com.fengyuan RESTEasyDemo war 0.0.1-SNAPSHOT RESTEasyDemo Maven Webapp http://maven.apache.org junit junit 4.4 test org.projectlombok lombok 1.14.4 org.jboss.resteasy resteasy-jaxrs 2.2.3.GA org.jboss.resteasy resteasy-jackson-provider 2.2.3.GA org.apache.maven.plugins maven-compiler-plugin 1.7 UTF-8
UserService.java:
package com.fengyuan.restapi; import java.util.ArrayList; import java.util.HashMap; import java.util.List; import java.util.Map; import java.util.Map.Entry; import javax.ws.rs.GET; import javax.ws.rs.Path; import javax.ws.rs.PathParam; import javax.ws.rs.Produces; import com.fengyuan.domain.User; @Path("userservice") // 服务路径 public class UserService { /** * 初始化三个用户数据,存入map中,key为用户id,value为用户对象 */ static MapuserMap = new HashMap<>(); static { User user1 = new User("Lee", 24, "138***"); userMap.put(1, user1); User user2 = new User("Cathy", 25, "188***"); userMap.put(2, user2); User user3 = new User("Aaron", 26, "186***"); userMap.put(3, user3); } /** * 获取指定id的用户 * * @param id * @return */ @GET @Path("user/{id}") // 具体服务的路径, id是入参 @Produces("application/json") // 返回的格式 public User getById(@PathParam("id") Integer id) { return (User) userMap.get(id); } /** * 以json格式返回所有用户 * * @return */ @GET @Path("users") @Produces("application/json") public List getUsers() { List userList = new ArrayList (); for (Entry user : userMap.entrySet()) { userList.add(user.getValue()); } return userList; } }
MessageService.java:
package com.fengyuan.restapi; import javax.ws.rs.GET; import javax.ws.rs.Path; import javax.ws.rs.PathParam; import javax.ws.rs.core.Response; @Path("/messageservice") public class MessageService { public MessageService(){} @GET @Path("/{param}") public Response printMessage(@PathParam("param") String msg) { String result = "Hello : " + msg; return Response.status(200).entity(result).build(); } }
User.java:
package com.fengyuan.domain; import lombok.AllArgsConstructor; import lombok.Data; public @Data @AllArgsConstructor class User { private String name; private int age; private String tel; }3. 配置web.xml:
Restful Web Application resteasy.scan true resteasy.servlet.mapping.prefix /rest org.jboss.resteasy.plugins.server.servlet.ResteasyBootstrap resteasy-servlet org.jboss.resteasy.plugins.server.servlet.HttpServletDispatcher resteasy-servlet /rest/*
配置resteasy.scan为true表示自动扫描服务。如果不配置,也可以手动指定resteasy.resources,见注释掉的部分。
4. 启动tomcat,在浏览器输入服务的url: 5. 除了可以在浏览器直接访问之外,也可以用代码调用:package com.fengyuan.test; import java.io.IOException; import java.io.InputStream; import java.net.HttpURLConnection; import java.net.URL; import junit.framework.TestCase; public class TestUserAPI extends TestCase { public static final String USER_API = "http://127.0.0.1:8080/resteasy-demo/rest/userservice/users"; public void testCreateUserAndGetUser() throws IOException { URL url = new URL(USER_API); HttpURLConnection connection = (HttpURLConnection) url.openConnection(); connection.setRequestMethod("GET"); connection.setRequestProperty("Content-Type", "application/x-www-form-urlencoded"); connection.setConnectTimeout(1000); byte[] bytes = new byte[1024]; //读取请求返回值 InputStream inStream=connection.getInputStream(); inStream.read(bytes, 0, inStream.available()); System.out.println(new String(bytes, "utf-8")); connection.disconnect(); } }
控制台输出:
[{"name":"Lee","tel":"138***","age":24},{"name":"Cathy","tel":"188***","age":25},{"name":"Aaron","tel":"186***","age":26}]
当然,这里只是简单了测试了GET类型的服务,还有POST等类型的服务等有空了再补充。
另外,我在写这个demo项目的时候,发现如果使用了2.1.多版本的resteasy-jackson-provider和resteasy-jaxrs包,并且在web.xml中配置了resteasy.scan为true的话,会出一些问题,这边提醒一下,让看这篇博文的同学不用再踩进这个坑~。
文章版权归作者所有,未经允许请勿转载,若此文章存在违规行为,您可以联系管理员删除。
转载请注明本文地址:https://www.ucloud.cn/yun/66213.html
摘要:如果是其它方式引发的错误,此处的解决方法不可用。最与该错误重要便是前两个。仔细检查发现,在接口中将参数转化为一个对象,但没有指出该对象就是错误代码根据查询条件查询登录注册访问统计数据解决办法接口中添加相应的注解。 1. object is not an instance of declaring class 错误日日志 2016-06-01 10:55:59,612 [RMI TCP...
摘要:此类问题,在配置中,做的比较好,所以,官方也不是很放心其注解,建议使用。错误的引起,是和引起的,原因给的是字符串,却拿一个数字比较,会做一些转化,结果在数字格式化时发生异常。 使用mybatis注解开发的时候,总会遇到很多问题,但是mybatis的注解并不是那么的美好,一些问题或者错误根本不能给出准确的定位,有时候,甚至会给出Unknow Source的错误,获取就是一个关闭标签没有关...
摘要:第二集非必选持续集成编译打包部署到线上环境集成前言在持续集成第一集中讲解了关于如果编译,打包,发布包到私服。在下面一集中,也就是第二集中,开始讲解如何持续集成可运行的服务包到测试和正式环境。 第二集 hudson+gradle+git+maven(非必选)持续集成编译,打包,部署到线上环境集成 前言 在持续集成第一集中,讲解了关于如果编译,打包,发布jar包到maven私服。在下面一集...
摘要:这个过程是浏览器会发一次请求,询问服务器是否允许代码如下完了以后,客户端就可以顺利请求服务器接口了。 此文讲解在RESTful API中跨域问题在项目中如何处理的! CORS 是 Cross Origin Resource Sharing 的缩写, 定义了浏览器和服务器间共享内容的新方式,通过它浏览器和服务器可以安全地进行跨域访问,它是 JSONP 的现代继任 者。服务器上的 COR...
摘要:当提供程序线程池耗尽时,不能发送到使用者端。一些错误修正动态配置不能删除,支持参数,监控统计问题等新功能支持手册线程池耗尽时自动堆栈转储。在注册表无法连接时被阻止。正常关机,在注册表取消注册和线程池关闭之间增加额外的等待时间。 dubbo分析showImg(https://segmentfault.com/img/bVbam2f?w=1726&h=686); dubbo为什么要对接sp...
阅读 1963·2021-11-25 09:43
阅读 1916·2021-11-24 09:39
阅读 1331·2021-11-22 12:02
阅读 2787·2021-11-17 09:33
阅读 3016·2021-11-15 11:38
阅读 2340·2021-10-13 09:40
阅读 806·2021-09-22 15:41
阅读 1537·2019-08-30 10:58