import com.hpb.bc.service.ApiFacadeService;
import com.hpb.bc.util.RequestHandleUtil;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import javax.servlet.http.HttpServletRequest;
import java.util.Map;
@RestController
@RequestMapping("/api")
public class ApiController extends BaseController {
@Autowired
ApiFacadeService apiFacadeService;
private static final Logger log = LoggerFactory.getLogger(ApiController.class);
@GetMapping(produces = MediaType.APPLICATION_JSON_VALUE, consumes = MediaType.APPLICATION_JSON_VALUE)
public ResponseEntity<Object> doExcute(HttpServletRequest request){
Map<String, String> reqParam = RequestHandleUtil.getReqParam(request);
Object object = null;
try {
object = apiFacadeService.doExcute(reqParam);
} catch (Exception e) {
log.error("【*********** ApiController exception,error msg {}********** 】",e.getMessage(),e);
}
return new ResponseEntity<>(object, HttpStatus.CREATED);
}
}
import org.apache.commons.lang3.StringUtils;
import javax.servlet.http.HttpServletRequest;
import java.io.*;
import java.net.URLDecoder;
import java.util.HashMap;
import java.util.LinkedHashMap;
import java.util.Map;
public class RequestHandleUtil {
public static final String METHOD_POST = "POST";
public static final String METHOD_GET = "GET";
public static final String CONTENT_TYPE_JSON = "application/json";
public static Map<String, String> getReqParam(HttpServletRequest req){
String method = req.getMethod();
Map<String, String> reqMap = new HashMap<String, String>();
if(METHOD_GET.equals(method)){
reqMap = doGet(req);
}else if(METHOD_POST.equals(method)){
reqMap = doPost(req);
}else{
return null;
}
return reqMap;
}
private static Map<String, String> doGet(HttpServletRequest req) {
String param = req.getQueryString();
return paramsToMap(param);
}
private static Map<String, String> doPost(HttpServletRequest req){
String contentType = req.getContentType();
try {
if (CONTENT_TYPE_JSON.equals(contentType)) {
StringBuffer sb = new StringBuffer();
InputStream is = req.getInputStream();
InputStreamReader isr = new InputStreamReader(is);
BufferedReader br = new BufferedReader(isr);
String s = "";
while ((s = br.readLine()) != null) {
sb.append(s);
}
String str = sb.toString();
return paramsToMap(str);
} else {
}
} catch (IOException e) {
e.printStackTrace();
}
return null;
}
public static Map<String, String> paramsToMap(String params) {
Map<String, String> map = new LinkedHashMap<>();
if (StringUtils.isNotBlank(params)) {
String[] array = params.split("&");
for (String pair : array) {
if ("=".equals(pair.trim())) {
continue;
}
String[] entity = pair.split("=");
if (entity.length == 1) {
map.put(decode(entity[0]), null);
} else {
map.put(decode(entity[0]), decode(entity[1]));
}
}
}
return map;
}
public static String decode(String content) {
try {
return URLDecoder.decode(content, "UTF-8");
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
}
return "";
}
}