通过maven下载jar包
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-databind</artifactId>
<version>2.9.1</version>
</dependency>
spring配置文件配置bean标签,使用json传送字符数据,使用form传递文件流数据。
<bean id="mappingJacksonHttpMessageConverter" class="org.springframework.http.converter.json.MappingJackson2HttpMessageConverter">
<property name="supportedMediaTypes">
<list>
<value>application/json;charset=UTF-8</value>
<value>multipart/form-data;charset=UTF-8</value>
<!-- <value>text/html;charset=UTF-8</value> -->
</list>
</property>
<property name="objectMapper">
<bean class="com.bhne.utils.JsonObjectMapper"/>
</property>
</bean>
controller层使用responseBody注解以及POST关键字。定义方法参数时,如果是单字段传递可以使用RequestParam,如果想实现json的实体类映射,使用RequestBody。
@Controller
@RequestMapping("/Sigin")
public class SiginController extends BaseController {
@Autowired
public SiginService siginService;
@RequestMapping(value="/QuerySigins",method =RequestMethod.POST,produces = {"application/json;charset=UTF-8"})
@ResponseBody
public void QuerySigins(@RequestParam(value="page", defaultValue = "1",required = true) int page, @RequestParam(value = "row", defaultValue = "10",required = true) int row, HttpServletRequest request, HttpServletResponse response) throws IOException{
siginService.GetSigins(response, page, row);
}
@RequestMapping(value="/AddSigin",method =RequestMethod.POST,produces = {"application/json;charset=UTF-8"})
@ResponseBody
public void AddSigin(@RequestBody yyks_jk_signin jk_signin,HttpServletRequest request, HttpServletResponse response) throws IOException{
siginService.AddSigin(response, jk_signin);
}
}
涉及文件流的接口参数中,实体类映射不需要RequestBody注解,可以根据具体需求自己选择。
@RequestMapping(value="/RegisterUser",method =RequestMethod.POST,produces = {"multipart/form-data;charset=UTF-8"})
@ResponseBody
public void AddUser(yyks_sys_user user_,@RequestParam("file1") MultipartFile file,HttpServletRequest request, HttpServletResponse response) throws IOException{
String fileName = file.getOriginalFilename();
user_.setFILE_PATH("/files/face/" +fileName);
user_.setIS_DEL("0");
SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");//设置日期格式
user_.setCREATE_TIME(df.format(new Date()));// new Date()为获取当前系统时间
user_.setPASSWORD("null");
user_.setROLE("92a0824b0e9311e2b43200ff5535edd7");
boolean rs = userService.AddUser(user_);
JSONObject jsonObject = new JSONObject();
if(rs) {
PropertiesConfiguration config = null;
try {
config = new PropertiesConfiguration("config.properties");
} catch (ConfigurationException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}//构造对象
//默认的编码格式是ISO-8859-1,所以才在读取文件之前先设置了编码格式
config.setEncoding("UTF-8");
// config.getStringArray(""); //根据关键字获取String列表
// propertiesFile = config.getString("path");
String path = config.getString("path")+"/face";
// String path = request.getServletContext().getRealPath("/upload/face");
System.out.println(path+"/" +fileName);
//获取指定文件或文件夹在工程中真实路径,getRequest()这个方法是返回一个HttpServletRequest,封装这个方法为了处理编码问题
FileOutputStream fos = FileUtils.openOutputStream(new File(path+"/" +fileName));//打开FileOutStrean流
IOUtils.copy(file.getInputStream(),fos);//将MultipartFile file转成二进制流并输入到FileOutStream
fos.close();
jsonObject.put("code", "1");
jsonObject.put("msg", "请求成功");
}else {
jsonObject.put("code", "3");
jsonObject.put("msg", "数据库操作失败");
}
response.getWriter().write(jsonObject.toString());
}
开发接口服务端程序时,要预留测试接口,最好使用GET请求方式一并写入服务端程序中,方便在程序部署后,通过浏览器检查项目是否正常运行。
功能比较简单的程序,尽量使用springboot框架,springboot对于这个功能配置更加简单。