GatewayController.java 2.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758
  1. package com.yihu.hos.controllers;
  2. import com.yihu.hos.core.constants.ExceptionConstant;
  3. import com.yihu.hos.core.exception.ESBException;
  4. import com.yihu.hos.models.GatewayRequestResult;
  5. import com.yihu.hos.models.GatewayResponseResult;
  6. import com.yihu.hos.services.GatewayService;
  7. import net.sf.json.JSONObject;
  8. import org.apache.commons.beanutils.BeanUtils;
  9. import org.springframework.beans.factory.annotation.Autowired;
  10. import org.springframework.web.bind.annotation.RequestMapping;
  11. import org.springframework.web.bind.annotation.RequestMethod;
  12. import org.springframework.web.bind.annotation.RestController;
  13. import javax.servlet.http.HttpServletRequest;
  14. import javax.servlet.http.HttpServletResponse;
  15. import java.io.Writer;
  16. @RestController
  17. @RequestMapping("/esb")
  18. public class GatewayController {
  19. private final GatewayService gatewayService;
  20. @Autowired
  21. public GatewayController(GatewayService gatewayService) {
  22. this.gatewayService = gatewayService;
  23. }
  24. @RequestMapping(value = "/gateway", method = RequestMethod.POST)
  25. public void transfer(HttpServletRequest request, HttpServletResponse response) throws Exception {
  26. request.setCharacterEncoding("UTF-8");
  27. response.setContentType("application/json;charset=UTF-8");
  28. Writer writer = response.getWriter();
  29. response.setCharacterEncoding("UTF-8");
  30. String returnString;
  31. String resultData;
  32. GatewayRequestResult gatewayRequestResult = new GatewayRequestResult();
  33. try {
  34. BeanUtils.populate(gatewayRequestResult, request.getParameterMap());
  35. //---end
  36. gatewayService.paramsIsNotNull(gatewayRequestResult.getApi());
  37. resultData = gatewayService.getResultData(gatewayRequestResult);
  38. returnString = resultData;
  39. } catch (Exception e) {
  40. e.printStackTrace();
  41. if (e instanceof ESBException) {
  42. ESBException esbException = (ESBException) e;
  43. returnString = JSONObject.fromObject(GatewayResponseResult.getError(esbException.getExceptionCode(), esbException.getExceptionMessage())).toString();
  44. } else {
  45. returnString = JSONObject.fromObject(GatewayResponseResult.getError(ExceptionConstant.EHREXCEPTION_SYSTEMEXCEPTION, ExceptionConstant.EHREXCEPTION_SYSTEMEXCEPTION_MESSAGE)).toString();
  46. }
  47. }
  48. writer.write(returnString);
  49. writer.flush();
  50. writer.close();
  51. }
  52. }