解析异常和返回结果

<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;
	}

}

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值