SpringBoot对XML格式的请求和响应

  • 2019-01-14
  • 0
  • 0

1.jackson-dataformat-xml依赖

<dependency>
    <groupId>com.fasterxml.jackson.dataformat</groupId>
    <artifactId>jackson-dataformat-xml</artifactId>
</dependency>

2.创建请求qo对象

@Data
@NoArgsConstructor
@AllArgsConstructor
@JacksonXmlRootElement(localName = "User")
public class User {
    @JacksonXmlProperty(localName = "name")
    private String name;
    @JacksonXmlProperty(localName = "age")
    private Integer age;
}

可以映射的Xml样例如下

<User>
    <name>aaaa</name>
    <age>10</age>
</User>

3.配置对应controller。produces和consumes属性为设置返回格式为xml,不配置默认为json

@Controller
public class UserController {
    @PostMapping(value = "/user", 
        consumes = MediaType.APPLICATION_XML_VALUE, 
        produces = MediaType.APPLICATION_XML_VALUE)
    @ResponseBody
    public User create(@RequestBody User user) {
        user.setName("didispace.com : " + user.getName());
        user.setAge(user.getAge() + 100);
        return user;
    }
}

4.发起postMan测试

评论

还没有任何评论,你来说两句吧