springboot开发xml接口
@PostMapping(value = "/testxml",produces = MediaType.APPLICATION_XML_VALUE,
consumes = MediaType.APPLICATION_XML_VALUE
如:produces和consumes的值等于接口类型(application/xml)
produces会将请求头的Content-Type或者Accept配置进行匹配,匹配不通过会返回错误415或者406
consumes默认会把配置的内容写到响应头的Content-Type中去
个人理解就是produces代表入参的时请求头中要带上Content-Type等于其配置值(application/xml)的一个声明,如果不声明会直接返回416或405,consumes是将响应标识成配置值(application/xml)
@RequestBody自动将请求流中的数据映射成string字符串文本类型,可用dm4j进行解析等
完整xml接口示例
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.*;
@RestController
@RequestMapping("/user")
public class LoginUserController {
@PostMapping(value = "/testxml",produces = MediaType.APPLICATION_XML_VALUE,
consumes = MediaType.APPLICATION_XML_VALUE)
@ResponseBody
public String getxml(@RequestBody String xmlparam){
return xmlparam;
}
}