RestApi.java 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181
  1. package com.java110.api.rest;
  2. import com.java110.api.smo.IApiServiceSMO;
  3. import com.java110.common.constant.CommonConstant;
  4. import com.java110.core.base.controller.BaseController;
  5. import io.swagger.annotations.Api;
  6. import io.swagger.annotations.ApiImplicitParam;
  7. import io.swagger.annotations.ApiOperation;
  8. import org.springframework.beans.factory.annotation.Autowired;
  9. import org.springframework.http.HttpStatus;
  10. import org.springframework.http.ResponseEntity;
  11. import org.springframework.web.bind.annotation.*;
  12. import javax.servlet.http.HttpServletRequest;
  13. import java.util.HashMap;
  14. import java.util.Map;
  15. /**
  16. *
  17. * rest api
  18. * Created by wuxw on 2018/10/16.
  19. */
  20. @RestController
  21. @RequestMapping(path = "/api")
  22. @Api(value = "对外统一提供服务接口服务")
  23. public class RestApi extends BaseController {
  24. @Autowired
  25. private IApiServiceSMO apiServiceSMOImpl;
  26. /**
  27. * 健康检查 服务
  28. * @return
  29. */
  30. @RequestMapping(path = "/health",method = RequestMethod.GET)
  31. @ApiOperation(value="服务健康检查", notes="test: 返回 2XX 表示服务正常")
  32. public String health(){
  33. return "";
  34. }
  35. /**
  36. * 资源请求 post方式
  37. * @param service 请求接口方式
  38. * @param postInfo post内容
  39. * @param request 请求对象 查询头信息 url等信息
  40. * @return http status 200 成功 其他失败
  41. */
  42. @RequestMapping(path = "/{service:.+}",method = RequestMethod.POST )
  43. @ApiOperation(value="资源post请求", notes="test: 返回 2XX 表示服务正常")
  44. @ApiImplicitParam(paramType="query", name = "service", value = "用户编号", required = true, dataType = "String")
  45. public ResponseEntity<String> servicePost(@PathVariable String service,
  46. @RequestBody String postInfo,
  47. HttpServletRequest request){
  48. ResponseEntity<String> responseEntity = null;
  49. try {
  50. Map<String, String> headers = new HashMap<String, String>();
  51. this.getRequestInfo(request, headers);
  52. headers.put(CommonConstant.HTTP_SERVICE,service);
  53. headers.put(CommonConstant.HTTP_METHOD,CommonConstant.HTTP_METHOD_POST);
  54. responseEntity = apiServiceSMOImpl.service(postInfo,headers);
  55. }catch (Throwable e){
  56. logger.error("请求post 方法["+service+"]失败:"+postInfo,e);
  57. return new ResponseEntity<String>("请求发生异常,"+e.getMessage(),HttpStatus.INTERNAL_SERVER_ERROR);
  58. }
  59. return responseEntity;
  60. }
  61. /**
  62. * 资源请求 get方式
  63. * @param service 请求接口方式
  64. * @param request 请求对象 查询头信息 url等信息
  65. * @return http status 200 成功 其他失败
  66. */
  67. @RequestMapping(path = "/{service:.+}",method = RequestMethod.GET )
  68. @ApiOperation(value="资源get请求", notes="test: 返回 2XX 表示服务正常")
  69. @ApiImplicitParam(paramType="query", name = "service", value = "用户编号", required = true, dataType = "String")
  70. public ResponseEntity<String> serviceGet(@PathVariable String service,
  71. HttpServletRequest request){
  72. ResponseEntity<String> responseEntity = null;
  73. try {
  74. Map<String, String> headers = new HashMap<String, String>();
  75. this.getRequestInfo(request, headers);
  76. headers.put(CommonConstant.HTTP_SERVICE,service);
  77. headers.put(CommonConstant.HTTP_METHOD,CommonConstant.HTTP_METHOD_GET);
  78. responseEntity = apiServiceSMOImpl.service("",headers);
  79. }catch (Throwable e){
  80. logger.error("请求get 方法["+service+"]失败:",e);
  81. return new ResponseEntity<String>("请求发生异常,"+e.getMessage(),HttpStatus.INTERNAL_SERVER_ERROR);
  82. }
  83. return responseEntity;
  84. }
  85. /**
  86. * 资源请求 put方式
  87. * @param service 请求接口方式
  88. * @param postInfo 修改内容
  89. * @param request 请求对象 查询头信息 url等信息
  90. * @return http status 200 成功 其他失败
  91. */
  92. @RequestMapping(path = "/{service:.+}",method = RequestMethod.PUT )
  93. @ApiOperation(value="资源put请求", notes="test: 返回 2XX 表示服务正常")
  94. @ApiImplicitParam(paramType="query", name = "service", value = "用户编号", required = true, dataType = "String")
  95. public ResponseEntity<String> servicePut(@PathVariable String service,
  96. @RequestBody String postInfo,
  97. HttpServletRequest request){
  98. ResponseEntity<String> responseEntity = null;
  99. try {
  100. Map<String, String> headers = new HashMap<String, String>();
  101. this.getRequestInfo(request, headers);
  102. headers.put(CommonConstant.HTTP_SERVICE,service);
  103. headers.put(CommonConstant.HTTP_METHOD,CommonConstant.HTTP_METHOD_PUT);
  104. responseEntity = apiServiceSMOImpl.service(postInfo,headers);
  105. }catch (Throwable e){
  106. logger.error("请求put 方法["+service+"]失败:",e);
  107. return new ResponseEntity<String>("请求发生异常,"+e.getMessage(),HttpStatus.INTERNAL_SERVER_ERROR);
  108. }
  109. return responseEntity;
  110. }
  111. /**
  112. * 资源请求 delete方式
  113. * @param service 请求接口方式
  114. * @param request 请求对象 查询头信息 url等信息
  115. * @return http status 200 成功 其他失败
  116. */
  117. @RequestMapping(path = "/{service:.+}",method = RequestMethod.DELETE )
  118. @ApiOperation(value="资源delete请求", notes="test: 返回 2XX 表示服务正常")
  119. @ApiImplicitParam(paramType="query", name = "service", value = "用户编号", required = true, dataType = "String")
  120. public ResponseEntity<String> serviceDelete(@PathVariable String service,
  121. HttpServletRequest request){
  122. ResponseEntity<String> responseEntity = null;
  123. try {
  124. Map<String, String> headers = new HashMap<String, String>();
  125. this.getRequestInfo(request, headers);
  126. headers.put(CommonConstant.HTTP_SERVICE,service);
  127. headers.put(CommonConstant.HTTP_METHOD,CommonConstant.HTTP_METHOD_DELETE);
  128. responseEntity = apiServiceSMOImpl.service("",headers);
  129. }catch (Throwable e){
  130. logger.error("请求delete 方法["+service+"]失败:",e);
  131. return new ResponseEntity<String>("请求发生异常,"+e.getMessage(),HttpStatus.INTERNAL_SERVER_ERROR);
  132. }
  133. return responseEntity;
  134. }
  135. /**
  136. * 获取请求信息
  137. * @param request
  138. * @param headers
  139. * @throws RuntimeException
  140. */
  141. private void getRequestInfo(HttpServletRequest request,Map headers) throws Exception{
  142. try{
  143. super.initHeadParam(request,headers);
  144. super.initUrlParam(request,headers);
  145. }catch (Exception e){
  146. logger.error("加载头信息失败",e);
  147. throw e;
  148. }
  149. }
  150. public IApiServiceSMO getApiServiceSMOImpl() {
  151. return apiServiceSMOImpl;
  152. }
  153. public void setApiServiceSMOImpl(IApiServiceSMO apiServiceSMOImpl) {
  154. this.apiServiceSMOImpl = apiServiceSMOImpl;
  155. }
  156. }