RestTemplate post请求传递map 还是MultiValueMap

昨天与同事联调http接口,准备用RestTemplate 进行请求,调用post请求,,但是参数死活传不进去:

测试对比:

先贴出自己的接口方法如下

public class User {


    private String loginname;
//    @Size(min=6 ,max= 20 ,message = "{items.password.size}")
    private String password;
//    @Size(min=3,max = 10,message = "{items.name.size}")
    private String username;

//    @NotNull
    private Integer age;

    public User() {

    }

    public String getLoginname() {
        return loginname;
    }

    public void setLoginname(String loginname) {
        this.loginname = loginname;
    }

    public String getPassword() {
        return password;
    }

    public void setPassword(String password) {
        this.password = password;
    }

    public String getUsername() {
        return username;
    }

    public void setUsername(String username) {
        this.username = username;
    }

    @Override
    public String toString() {
        return "User{" +
                "loginname='" + loginname + '\'' +
                ", password='" + password + '\'' +
                ", username='" + username + '\'' +
                '}';
    }
}
    controller 中的get post测试接口

@RequestMapping(value = "/userone",method = RequestMethod.GET)
    @Validation
    public @ResponseBody String logUserOne(
            @Validated User user) {

        return user.toString();

    }

    @PostMapping(value = "/userone1")
    @ResponseBody
    public  String logUserOne1(@RequestBody User user) {

        return user.toString();

    }

 

下面为postman请求:结果正常

RestTemplate post请求传递map 还是MultiValueMap_第1张图片

 

准备在代码中进行测试:遇到如下错误

写如下测试方法:

1.直接传map,运行结果可以出来
        result = restTemplate.postForObject(url, map, String.class);
        System.out.println("map:"+result);

2.此种传法报错
org.springframework.web.client.HttpClientErrorException: 415 Unsupported Media Type

        result = restTemplate.postForObject(url, paramMapt, String.class);
        System.out.println("paramMapt:"+result);

3.此种传法也报错
org.springframework.web.client.HttpClientErrorException: 400 Bad Request

        result = restTemplate.postForObject(url, formEntity, String.class);

经过查看 spring相关的jar包版本为4.3.7
以前看到其他同事写的都是使用的第三种方法,发现运行的相当正常,不知现在自己使用怎么这么费劲,希望有知道的朋友给与答疑解惑

 @Test
    public void getbdc23(){

        RestTemplate restTemplate = new RestTemplate();
        String result =null;
        Map map = new HashMap<>();
        map.put("loginname", "spw2444");
        map.put("password", "passpasss");
        map.put("username", "username");

        HttpHeaders headers = new HttpHeaders();
        headers.setContentType( MediaType.APPLICATION_JSON );

//        headers.setAccept(Arrays.asList(MediaType.APPLICATION_JSON));
        headers.add("Accept","application/json");
        MultiValueMap paramMapt = new LinkedMultiValueMap() {
            {
                this.add("loginname", "loginname");
                this.add("password", "passpasss");
                this.add("username", "username");
            }
        };

        HttpEntity> formEntity = new HttpEntity>(paramMapt, headers);
        String url = "http://localhost:8083/validated/userone1";

//        ResponseEntity result = restTemplate.postForEntity(url, formEntity, String.class,map);

//        System.out.println(result.getBody());
        User user = new User();
        user.setLoginname("longinna");
        user.setPassword("pass");
        user.setUsername("name");
        result = restTemplate.postForObject(url, map, String.class);
        System.out.println("map:"+result);
        result = restTemplate.postForObject(url, paramMapt, String.class);
        System.out.println("paramMapt:"+result);
        result = restTemplate.postForObject(url, formEntity, String.class);
        System.out.println("formEntity:"+result);
    }

 

 

 

你可能感兴趣的:(RestTemplate post请求传递map 还是MultiValueMap)