SpringMvc 静态内部类 封装请求数据
最近写一个小电商项目的时候碰到一个整单/单件(退货)以及单件换货共用的一个接口
本来打算直接传json字符串作为参数的
(List)JSONArray.toCollection(JSONArray.fromObject(json)
后边发现利用框架进行简单粗暴的集合更方便
于是参数直接写成:
@RequestBody List<TClaimOnline> theList //TClaimOnline为封装商品参数的单件商品对象
又发现对象中用来区别退货和换货的是对象的一个属性,于是提取出来应该是最好的
于是乎在架构师的指导下
写了一个静态内部类
@RestController @RequestMapping(APIUrls.API_CUSTOMERSERVICE_URI) public class ReturnExchangeController { @Autowired private ReturnExchangeService returnExchangeService; @Autowired private ShopStockService shopStockService; @Autowired private TokenManager tokenManager; public static class ItemReturn { private String retExchYn; private List<TClaimOnline> theList; public String getRetExchYn() { return retExchYn; } public void setRetExchYn(String retExchYn) { this.retExchYn = retExchYn; } public List<TClaimOnline> getTheList() { return theList; } public void setTheList(List<TClaimOnline> theList) { this.theList = theList; } } ......
方法及参数写成:
/** * 退换货申请 * * @param request * @param response * @return * @throws Exception */ @RequestMapping(value = APIUrls.API_FCUSTOMERSERVICE_ITEM_RETURN_APPLICATION_URI, method = RequestMethod.POST) public ResponseEntity<?> itemReturnApplication(@RequestBody ItemReturn itemReturn, HttpServletRequest request, HttpServletResponse response) throws Exception {
很好的解决了提取共性封装数据的问题
在集合便利赋值的时候 还使用了点装B的 Java8新写法 :
itemReturn.getTheList().forEach(t -> {
t.setRetExchYn(itemReturn.getRetExchYn());
t.setCustNo(custNo);
t.setcCode(cCode);
});
感觉高大上了很多