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