微服务框架Jersey:快速入门
本文介紹如何利用Jersey和嵌入式的Jetty容器構(gòu)建微服務(wù)應(yīng)用,包括基本框架搭建、服務(wù)啟動(dòng)和服務(wù)訪問等。
環(huán)境及版本
開發(fā)環(huán)境:Windows 7 、IntelJ IDEA、maven、jdk
Jersey:2.19
JDK: 1.8
基本框架搭建
搭建Jersey微服務(wù)應(yīng)用,基本框架構(gòu)建主要步驟:
- 創(chuàng)建maven工程,組織依賴庫和編譯插件
- 編寫服務(wù)器啟動(dòng)類
- 注冊REST資源
- 日志配置
整體框架結(jié)構(gòu)如圖所示:
maven配置
新建mavan工程,引入Jersey、Jetty、log4j等依賴庫,并為工程指定編譯器為jdk 1.8。
<?xml version="1.0" encoding="UTF-8"?> <project><modelVersion>4.0.0</modelVersion><groupId>rest</groupId><artifactId>rest</artifactId><version>1.0.0</version><properties><jetty.version>9.3.8.v20160314</jetty.version><jersey2.version>2.19</jersey2.version></properties><dependencies><!--jersey--><dependency><groupId>org.glassfish.jersey.containers</groupId><artifactId>jersey-container-servlet</artifactId><version>${jersey2.version}</version></dependency><!--jetty容器--><dependency><groupId>org.eclipse.jetty</groupId><artifactId>jetty-servlet</artifactId><version>${jetty.version}</version></dependency><!--日志--><dependency><groupId>org.slf4j</groupId><artifactId>slf4j-log4j12</artifactId><version>1.7.25</version></dependency></dependencies><build><plugins><plugin><groupId>org.apache.maven.plugins</groupId><artifactId>maven-compiler-plugin</artifactId><version>2.3.2</version><configuration><source>1.8</source><target>1.8</target></configuration></plugin></plugins></build> </project>服務(wù)器入口
編寫服務(wù)啟動(dòng)類
package org.bigdata;import org.eclipse.jetty.server.Server; import org.eclipse.jetty.server.handler.HandlerList; import org.eclipse.jetty.servlet.ServletContextHandler; import org.eclipse.jetty.servlet.ServletHolder; import org.glassfish.jersey.servlet.ServletContainer;public class JettyServer {public static void main(String[] args) throws Exception {int port = 8081;Server server = new Server(port);ServletHolder sh = new ServletHolder(ServletContainer.class);//監(jiān)聽的資源sh.setInitParameter("jersey.config.server.provider.packages","org.bigdata.res");ServletContextHandler apiContext = new ServletContextHandler(ServletContextHandler.SESSIONS);apiContext.addServlet(sh, "/*");apiContext.setContextPath("/");HandlerList handlerList = new HandlerList();handlerList.addHandler(apiContext);server.setHandler(handlerList);server.start();}}編寫資源類
編寫一個(gè)簡單的資源類,用于響應(yīng)用戶的/hello請求。編寫完成后,一定要在服務(wù)器啟動(dòng)類中注冊資源。sh.setInitParameter("jersey.config.server.provider.packages","org.bigdata.res");
package org.bigdata.res;import javax.jws.WebService; import javax.servlet.http.HttpServletRequest; import javax.ws.rs.GET; import javax.ws.rs.Path; import javax.ws.rs.core.Context;@Path("") @WebService public class WelcomeRes {@Path("hello")@GETpublic String sayHello(@Context HttpServletRequest request) {return "hello, jetty!";} }日志配置
Jersey框架的日志要在框架中顯示引入,如果沒有顯示引入log4j框架,服務(wù)器啟動(dòng)時(shí)會(huì)提示如下異常。
log4j:WARN No appenders could be found for logger (dao.hsqlmanager). log4j:WARN Please initialize the log4j system properly. log4j:WARN See http://logging.apache.org/log4j/1.2/faq.html#noconfig for more info.引入log4j框架,并指定日志輸出文件log4j.properties。
log4j.rootLogger=INFO,A1log4j.appender.A1=org.apache.log4j.ConsoleAppender log4j.appender.A1.layout=org.apache.log4j.PatternLayout log4j.appender.A1.layout.ConversionPattern=%d{yyyy-MM-dd HH:mm:ss} %p %c{2} %m%n log4j.appender.A1.Threshold=INFO服務(wù)啟動(dòng)
運(yùn)行JettyServer類,啟動(dòng)服務(wù)器。如圖所示:
通過瀏覽器輸入http://localhost:8081/hello請求服務(wù),查看返回結(jié)果。
總結(jié)
以上是生活随笔為你收集整理的微服务框架Jersey:快速入门的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 为你总结了N个真实线上故障,从容应对面试
- 下一篇: 几款web富文本编辑器汇总整理