解析异常和返回结果

<mvc:default-servlet-handler />
	<!-- 解决静态页面加载问题 -->
	<!-- 解决静态页面加载问题 -->
	<!-- 启动Springmvc注解驱动 -->
	<!-- 返回json 方法一 需要导入 fastjson.jar包 -->  
	 <!--RequestMappingHandlerAdapter-->
	<mvc:annotation-driven>
		<mvc:message-converters register-defaults="true">
			<ref bean="fastJsonHttpMessageConverter" />
		</mvc:message-converters>
	</mvc:annotation-driven>
	<!-- 配置Fastjson支持 -->
	<bean id="fastJsonHttpMessageConverter" class="com.alibaba.fastjson.support.spring.FastJsonHttpMessageConverter">
		<property name="supportedMediaTypes">
			<list>
				<value>application/json;charset=UTF-8</value>
				<value>text/plain;charset=UTF-8</value>
				<value>text/html;charset=UTF-8</value>
			</list>
		</property>
		<!-- 一下可以不配 -->
		<property name="features">
			<list>
				<value>WriteMapNullValue</value>
				<value>QuoteFieldNames</value>
			</list>
		</property>
	</bean>


package com.dongly.fanshe;

import com.alibaba.fastjson.annotation.JSONType;

@JSONType(orders={"status","msg","result"})
public class ResultVo {

	private Integer status;
	private String msg;
	private Object result;

	public Integer getStatus() {
		return status;
	}

	public void setStatus(Integer status) {
		this.status = status;
	}

	public String getMsg() {
		return msg;
	}

	public void setMsg(String msg) {
		this.msg = msg;
	}

	public Object getResult() {
		return result;
	}

	public void setResult(Object result) {
		this.result = result;
	}

	public ResultVo() {
		super();
	}

	public ResultVo(Integer status, String msg, Object result) {
		super();
		this.status = status;
		this.msg = msg;
		this.result = result;
	}

	@Override
	public String toString() {
		return "ResultVo [status=" + status + ", msg=" + msg + ", result=" + result + "]";
	}

}
 
  
 
package com.dongly.fanshe;

import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;

import javax.servlet.http.HttpServletResponse;

import org.apache.commons.lang3.StringUtils;
import org.apache.commons.lang3.math.NumberUtils;

import com.alibaba.fastjson.JSON;

/**
 * 解析异常和返回结果信息
 * @author bashen
 */
public class ParseResult {
	public static ResultVo parseException(Exception e) {
		String runtimeExcetion = e.getMessage();
		ResultVo vo = new ResultVo();
		if (runtimeExcetion != null) {
			String[] excetion = runtimeExcetion.split(":");
			if (excetion.length == 2 && excetion[0] != null && excetion[0].startsWith("5")) {
				vo.setStatus(NumberUtils.toInt(excetion[0], 501));
				vo.setMsg(excetion[1]);
				return vo;
			}
		}
		vo.setStatus(501);
		vo.setMsg("服务器内部错误");
		return vo;
	}
	public static String parseResult(ResultVo rv, HttpServletResponse response) {
		if (null == rv.getStatus()) {
			rv.setStatus(500);
			if (response != null)
				response.setStatus(500);
		}
		if (response != null)
			response.setStatus(rv.getStatus());

		if (null == rv.getMsg() || "".equals(rv.getMsg())) {
			rv.setMsg("unknow message");
		}
		if (null == rv.getResult() || "".equals(rv.getResult())) {
			rv.setResult("{}");
		}
		return JSON.toJSONString(rv);
	}
}
package com.dongly.fanshe;

import java.lang.reflect.Method;
import java.util.Map;

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

import org.springframework.http.MediaType;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestHeader;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

@RequestMapping("/reg")
@Controller
public class ReflectTest {
	
	@ResponseBody
	@RequestMapping(value = "/{method:[a-zA-Z_]{3,10}}",consumes=MediaType.APPLICATION_JSON_VALUE,produces = MediaType.APPLICATION_JSON_VALUE)
	public String select(@PathVariable("method") String method,@RequestBody(required=false) Map<String,Object> body,@RequestHeader Map<String,Object> headers,HttpServletResponse response) {

		String result = null;
		ResultVo vo = null;
		try {
			
			String className = "com.dongly.fanshe" + ".User";
			Class<?> clazz = this.getClass().getClassLoader().loadClass(className);
			
			Method m = clazz.getDeclaredMethod(method, Map.class, Map.class);
			
			Object invoke = m.invoke(clazz.newInstance(), body,headers);
			vo = new ResultVo(200, "请求成功", invoke);
		} catch (Exception e) {
			vo = ParseResult.parseException(e);
		} finally {
			result = ParseResult.parseResult(vo, response);
		}
		return result;
	}

}

猜你喜欢

转载自xhnszdm.iteye.com/blog/2305878