欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

springMVC远程接口开发

程序员文章站 2022-05-28 09:49:54
...


import java.io.IOException;
import java.io.PrintWriter;
import java.math.BigDecimal;
import java.util.List;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

import com.alibaba.fastjson.JSONObject;


/**
 * 安卓端订单管理
 * 
 * @author CNZZ
 *
 */
@Controller
@RequestMapping("/AndroidOrderManage")
public class AndroidOrderManageController {
	private static Logger log = LoggerFactory
			.getLogger(AndroidOrderManageController.class);

	@Resource
	AndroidOrderManageService androidOrderManageService;


	/**
	 * 安卓端订单创建
	 * 
	 * @param request
	 * @param response
	 * @return
	 * @throws Exception
	 */
	@RequestMapping("/createOrder")
	public void createOrder(HttpServletRequest request,
			HttpServletResponse response) throws Exception {
		JSONObject jsonObject = new JSONObject();

		try {
			// 1.入参打印
			String requestJSONStr = request.getParameter("requestJSONStr");
			log.info("安卓端订单创建,requestJSONStr={}", requestJSONStr);

			// 2.入参非空校验
			if (CmUtil.isEmpty(requestJSONStr)) {
				jsonObject.put("code", "-1");
				jsonObject.put("result", "");
				jsonObject.put("messages", "请求参数为空!");
				log.error("安卓端订单创建,请求参数为空,requestJSONStr={}", requestJSONStr);
				doResponse(response, jsonObject.toJSONString());
				return;
			}

			// 3.业务方法
			String orderNum = doCreateOrder(response, jsonObject,
					requestJSONStr);

			if (CmUtil.isEmpty(orderNum)) {
				return;
			}

			// 7.响应
			JSONObject result = new JSONObject();
			result.put("requestJSONStr", requestJSONStr);
			result.put("orderNum", orderNum);

			jsonObject.put("code", "000000");
			jsonObject.put("result", result);
			jsonObject.put("messages", "交易成功");

			doResponse(response, jsonObject.toJSONString());
		} catch (Exception e) {
			jsonObject.put("code", "-1");
			jsonObject.put("result", "");
			jsonObject.put("messages", "系统异常!");
			log.error("安卓端订单创建,系统异常,e={}", e);
			doResponse(response, jsonObject.toJSONString());
		}

	}

	/**
	 * 响应
	 * 
	 * @param response
	 * @param outputStr
	 */
	private void doResponse(HttpServletResponse response, String outputStr) {
		response.setContentType("application/json");
		PrintWriter out = null;
		try {
			out = response.getWriter();
			out.write(outputStr);
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} finally {
			out.flush();
			out.close();
		}
	}
}

 

 

其中部分方法和包导入删除过了,为了让流程更加清晰。

相关标签: 远程接口