java-web-common
Controller
中一律返回Java object
,组件将自动转换数据格式,满足Json接口格式try-catch
.Spring只有针对页面请求的SimpleMappingExceptionResolver异常解决方案,并没有针对使用@ResponseBody的方法异常处理<context:component-scan base-package="ren.wenchao.web.common, your-controller-packages" />
<mvn:annotation-driven/>
@JsonResponseBody
注解,默认是V2版本。可以参考TestController.java
文件示例1
@RequestMapping(value = "/testJsonResponceBodyV2ReturnMap")
@JsonResponseBody(version = JsonResponseBody.Version.V2)
public Map testJsonResponceBodyV2ReturnMap() {
HashMap<Object, Object> hashMap = Maps.newHashMap();
hashMap.put("key", "value");
return hashMap;
}
返回:
{
"code":0,
"msg":"",
"data":{
"key":"value"
}
}
示例2
@RequestMapping(value = "/testJsonResponceBodyV1WithException")
@JsonResponseBody(version = JsonResponseBody.Version.V1)
public List<String> testJsonResponceBodyV1WithException() {
throw new RuntimeException("some exception");
}
返回:
{
"ret":false,
"msg":"some exception"
}
更多示例请参考:TestController.java
Payload
,有可能你最终需要的csv的列和业务model Payload
的属性不完全一致,SomeCSVPayload
(名字随意)来实现BaseCSVPayload
接口,并制定csv的列头等信息@CSVResponceBody(fileName = "output", payloadClass = SomeCSVPayload.class)
,如下: @RequestMapping(value = "/testCSVResponceBody")
@CSVResponceBody(fileName = "output", payloadClass = SomeCSVPayload.class)
public SomeCSVPayload testCSVResponceBody() {
return new SomeCSVPayload();
}
示例使用请查看TestController.java中的testCSVResponceBody
方法
暂时没打算支持Excel下载,因为Excel需要区分版本,不同版本的限制也有些不同,而CSV比较通用,因此目前只支持csv