Restful后台系统搭建(三)
来源:互联网 发布:淘宝买家不确认收货 编辑:程序博客网 时间:2024/06/05 10:23
本章主要讲解服务的启动以及使用jmeter简单测试。
先展示本章所涉及的几个包+文件分布:
分别讲解每一个类的目的
FrameworkException:
每个框架都应该有自己的异常子类,以便传输或者转换一些异常,通过ErrorCode来定义自己的错误码,方便接口返回时展示。
FrameworkErrorCode:
错误码表。
Main:
程序入口。
ServiceStarter:
服务启动器的接口,子类需要实现其callback方法。
ServiceStarterRegister:
服务启动器的注册类,所有需要启动的服务将实现ServiceStarter接口,并在该类中注册。
JerseyServerStarter:
负责启动Jetty服务并注册Jersey。
TestService:
测试用的服务接口,提供对外访问的url入口。
下面是每个类的代码实现:
FrameworkException:
/** * 2017年4月29日 黑苹果 */package com.black.apple.framework.exception;import org.apache.commons.lang3.StringUtils;import com.google.common.base.Optional;/** * @author 黑苹果 2017年4月29日 */public class FrameworkException extends Exception{ public FrameworkErrorCode errorCode; /** * serialVersionUID */ private static final long serialVersionUID = -1533750106989537039L; public FrameworkException(FrameworkErrorCode errorCode) { super(); this.errorCode = errorCode; } public FrameworkException(FrameworkErrorCode errorCode, Throwable t) { super(t); this.errorCode = errorCode; } public String getErrorCode() { return Optional.fromNullable(errorCode).isPresent() ? errorCode.toString() : StringUtils.EMPTY; }}
FrameworkErrorCode:
/** * 2017年4月29日 黑苹果 */package com.black.apple.framework.exception;/** * @author 黑苹果 2017年4月29日 */public enum FrameworkErrorCode{ /** * 成功 */ OK(0), /** * Jersey服务启动失败 */ JERSEY_SERVICE_START_FAILED(1), /** * 未知异常 */ UNKNOW_EXCEPTION(9999); private int errorCode; /** * 错误码范围为:10000000~10009999 */ private static final int ERROR_CODE_PREFIX = 1000_0000; private FrameworkErrorCode(int errorCode) { this.errorCode = ERROR_CODE_PREFIX + errorCode; } public String toString() { return String.valueOf(errorCode); }}
Main:
/** * 2017年4月29日 黑苹果 */package com.black.apple.framework.main;import java.util.List;import org.slf4j.Logger;import org.slf4j.LoggerFactory;import com.black.apple.framework.exception.FrameworkException;import com.google.common.base.Optional;/** * @author 黑苹果 2017年4月29日 */public class Main{ private static final Logger LOGGER = LoggerFactory.getLogger(Main.class); /** * @param args */ public static void main(String[] args) { List<ServiceStarter> services = ServiceStarterRegister.getServices(); for (ServiceStarter serviceStarter : services) { serviceStart(serviceStarter); } } private static void serviceStart(ServiceStarter serviceStarter) { if (!Optional.fromNullable(serviceStarter).isPresent()) { return; } try { serviceStarter.callback(); } catch (FrameworkException e) { LOGGER.error("Error in service starter when do callback.", e); } catch (Exception e) { LOGGER.error("Error in service starter when do callback.", e); } }}
/** * 2017年4月29日 黑苹果 */package com.black.apple.framework.main;import com.black.apple.framework.exception.FrameworkException;/** * @author 黑苹果 2017年4月29日 */public interface ServiceStarter{ /** * 服务启动器回调方法 */ public void callback() throws FrameworkException;}
ServiceStarterRegister:
/** * 2017年4月29日 黑苹果 */package com.black.apple.framework.main;import java.util.ArrayList;import java.util.List;import com.black.apple.framework.jersey.rest.service.JerseyServerStarter;/** * @author 黑苹果 2017年4月29日 */public final class ServiceStarterRegister{ private static final ServiceStarterRegister INSTANCE = new ServiceStarterRegister(); private List<ServiceStarter> services = new ArrayList<ServiceStarter>(); private ServiceStarterRegister() { registerService(); } private void registerService() { // TODO 添加Service services.add(new JerseyServerStarter()); } private static ServiceStarterRegister getInstance() { return INSTANCE; } public static List<ServiceStarter> getServices() { return getInstance().services; }}
JerseyServerStarter:
/** * 2017年4月29日 黑苹果 */package com.black.apple.framework.jersey.rest.service;import org.eclipse.jetty.server.Server;import org.eclipse.jetty.servlet.ServletContextHandler;import org.eclipse.jetty.servlet.ServletHolder;import org.slf4j.Logger;import org.slf4j.LoggerFactory;import com.black.apple.framework.exception.FrameworkErrorCode;import com.black.apple.framework.exception.FrameworkException;import com.black.apple.framework.main.ServiceStarter;import com.sun.jersey.spi.container.servlet.ServletContainer;/** * @author 黑苹果 2017年4月29日 */public class JerseyServerStarter implements ServiceStarter{ private static final Logger LOGGER = LoggerFactory.getLogger(JerseyServerStarter.class); private static final int PORT = 8687; /* * (non-Javadoc) * * @see com.black.apple.framework.main.ServiceStarter#callback() */ @Override public void callback() throws FrameworkException { Server server = new Server(PORT); ServletHolder servlet = new ServletHolder(ServletContainer.class); servlet.setInitParameter("com.sun.jersey.config.property.resourceConfigClass", "com.sun.jersey.api.core.PackagesResourceConfig"); servlet.setInitParameter("com.sun.jersey.config.property.packages", "com.black.apple.framework.jersey.rest.service"); ServletContextHandler handler = new ServletContextHandler(ServletContextHandler.SESSIONS); handler.setContextPath("/"); handler.addServlet(servlet, "/*"); server.setHandler(handler); try { server.start(); } catch (Exception e) { LOGGER.error("Error in start jersey server.", e); throw new FrameworkException(FrameworkErrorCode.JERSEY_SERVICE_START_FAILED, e); } }}
TestService:
/** * 2017年4月29日 黑苹果 */package com.black.apple.framework.jersey.rest.service;import javax.ws.rs.POST;import javax.ws.rs.Path;import javax.ws.rs.Produces;import javax.ws.rs.core.MediaType;/** * @author 黑苹果 2017年4月29日 */@Path("framework")public class TestService{ @POST @Path("test") @Produces(MediaType.APPLICATION_JSON) public String test(String content) { return "test" + content; }}
0 0
- Restful后台系统搭建(三)
- Restful后台系统搭建(一)
- Restful后台系统搭建(二)
- Restful后台系统搭建(四)
- Restful后台系统搭建(五)【终章】提供源码
- 移动大脑-SpringMVc搭建RestFul后台服务(三)-RestFul接口编写(模拟用户注册登录)
- 移动大脑-SpringMVc搭建RestFul后台服务(一)-环境搭建
- RESTful API后台系统架构设计(Java)
- 移动大脑-SpringMVc搭建RestFul后台服务(六)-微信支付(Android)
- 移动大脑-SpringMVc搭建RestFul后台服务(二)-配置mysql数据库
- 移动大脑-SpringMVc搭建RestFul后台服务(四)-添加Token过滤器
- 移动大脑-SpringMVc搭建RestFul后台服务(五)-支付宝支付
- 移动大脑-SpringMVc搭建RestFul后台服务(七)-增量更新
- 本地搭建管理后台系统
- 使用Node.js搭建微信支付后台(三)
- 物联网系统的搭建(三)
- 迟到的总结(三)--springmvc的系统学习之数据的处理,乱码及restful
- 如何使用laravel搭建后台登录系统
- iOS 框架模式(简述 MVC,MVP,MVVM 和 VIPER)
- HtmlUnit在本地化测试中的使用
- Course Selection System-0-1背包
- Java入门学习,作业1
- Java多线程
- Restful后台系统搭建(三)
- Vue.js启航
- 51nod1001 数组中和等于K的数对
- MySQL与存储引擎
- Java Annotation 原理
- [省选] [扩展欧拉函数] [线段树] [BZOJ4869] [HLOI2017] 相逢是问候
- Ubuntu16.04下基于Docker的Caffe-GPU版本环境搭建总结
- 缓存在高并发场景下的常见问题
- 妙用php中的array_filter()获取数据