springboot 提交表单和图片,后台对象接收
2018-09-21 10:53:12 作者:MangoCool 来源:MangoCool
简述:springboot项目中,前端表单提交数据,其中包括图片,后台以对象一并接收,然后直接入库,就这么简单的需求。
直接上代码:
CompanyInfoController 控制器:
package net.olym.symailp.website.controller;
import net.olym.symailp.core.util.AjaxUtils;
import net.olym.symailp.core.util.CommonResponseMap;
import net.olym.symailp.website.model.CompanyModel;
import net.olym.symailp.website.service.CompanyInfoService;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
@RestController
@RequestMapping("/webmgr/companys")
public class CompanyInfoController {
private static final Log LOG = LogFactory.getLog(CompanyInfoController.class);
@Autowired
HttpServletRequest request;
@Autowired
HttpServletResponse response;
@Autowired
CompanyInfoService companyInfoService;
@RequestMapping(value="/info", method = RequestMethod.GET)
public void getCompanyInfo() {
CommonResponseMap dataMap = new CommonResponseMap();
try {
CommonResponseMap retMap = companyInfoService.getCompanyInfo();
dataMap.putAll(retMap);
} catch (Exception e) {
LOG.error(e.getMessage(), e);
dataMap.error();
} finally {
AjaxUtils.sendAjaxForObject(response, dataMap);
}
}
@RequestMapping(value="/{id}", method = RequestMethod.POST)
public void updateCompanyInfo(CompanyModel company) {
CommonResponseMap dataMap = new CommonResponseMap();
try {
CommonResponseMap retMap = companyInfoService.updateCompanyInfo(company);
dataMap.putAll(retMap);
} catch (Exception e) {
LOG.error(e.getMessage(), e);
dataMap.error();
} finally {
AjaxUtils.sendAjaxForObject(response, dataMap);
}
}
}
CompanyInfoService 接口:
package net.olym.symailp.website.service;
import net.olym.symailp.core.util.CommonResponseMap;
import net.olym.symailp.website.model.CompanyModel;
public interface CompanyInfoService {
CommonResponseMap getCompanyInfo() throws Exception;
CommonResponseMap updateCompanyInf