springboot post xml

TestController.java



1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
package com.done.posttest.controller;
 
 
import com.done.posttest.model.Student;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpEntity;
import org.springframework.http.HttpHeaders;
import org.springframework.http.MediaType;
import org.springframework.util.LinkedMultiValueMap;
import org.springframework.util.MultiValueMap;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.client.RestTemplate;
 
import java.util.HashMap;
import java.util.Map;
 
@Api(description = "test")
@RestController
public class TestController {
 
 
    @PostMapping("received")
    @ApiOperation(value = "received", httpMethod = "POST", consumes =  MediaType.APPLICATION_XML_VALUE , produces = MediaType.APPLICATION_XML_VALUE)
    public Map<Object,Object> received(@RequestBody Student student){
        Map<Object,Object> map = new HashMap<>();
 
 
        System.out.println(student);
 
        map.put("code",200);
        map.put("message","接收成功");
        return map;
    }
 
    @PostMapping("send")
    public Map<Object,Object> send(){
        Map<Object,Object> map = new HashMap<>();
 
 
        RestTemplate restTemplate = new RestTemplate();
 
        String url = "http://localhost:8700/received";
        HttpHeaders requestHeader = new HttpHeaders();
        requestHeader.setContentType(MediaType.APPLICATION_XML);
        StringBuffer xmlString = new StringBuffer();
        xmlString.append("<student>")
                .append("<id>2</id>")
                .append("<name>jim</name>")
                .append("</student>");
        HttpEntity<String> requestEntity = new HttpEntity<>(xmlString.toString(), requestHeader);
 
        String forObject = restTemplate.postForObject(url, requestEntity, String.class);
 
        map.put("code",200);
        map.put("message","发送成功");
        map.put("data",forObject);
        return map;
    }
 
}

 

 

Student.java
复制代码
package com.done.posttest.model;


import com.fasterxml.jackson.dataformat.xml.annotation.JacksonXmlProperty;
import com.fasterxml.jackson.dataformat.xml.annotation.JacksonXmlRootElement;
import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiModelProperty;

import javax.xml.bind.annotation.*;
import java.io.Serializable;


@JacksonXmlRootElement(localName ="student")
public class Student implements Serializable {

    @JacksonXmlProperty
    private String id;

    @JacksonXmlProperty
    private String name;

    public String getId() {
        return id;
    }

    public void setId(String id) {
        this.id = id;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }
}
复制代码

 

posted @   cppwen  阅读(482)  评论(0编辑  收藏  举报
努力加载评论中...
点击右上角即可分享
微信分享提示