【问题标题】:Why passing Map<String,String> in postman as JSON works but passing the map in Java RestTemplate does not work?为什么在邮递员中传递 Map<String,String> 作为 JSON 工作,但在 Java RestTemplate 中传递地图不起作用?
【发布时间】:2022-01-06 20:23:10
【问题描述】:

这个项目是一个运行在 JBOSS 应用服务器 (GraphClient.war) 上的 WAR 应用程序,在部署它之后,我可以使用如下 URL 向它发出请求:

http://localhost:8080/GraphClient/helloworld

我调用这个控制器传递 Map,使用 postman 配置 Body > RAW > Json (application/json) 并传递地图,如下所示:

{
"hello1":"Jupiter",
"hello2":"Mercury",
"hello3":"Venus",
"hello4":"Mars",
"hello5":"Earth"
}

它可以工作,但是如果我从另一个控制器用 Java 发送相同的 Map,我会得到一个不允许的 405 方法。代码如下:

@RequestMapping(value="/callhelloworld", method=RequestMethod.GET)
public String  caller( )
{

    MultiValueMap<String, String> body = new LinkedMultiValueMap<String,String>();

    body.add("planet1","Jupiter");
    body.add("planet2","Mercury");
    body.add("planet3","Venus");
    body.add("planet4","Mars");
    body.add("planet5","Earth");

    HttpHeaders headers = new HttpHeaders();



   // headers.set(HttpHeaders.CONTENT_TYPE, MediaType.APPLICATION_FORM_URLENCODED_VALUE);
    HttpEntity<?> entity = new HttpEntity<>(body, headers);

    RestTemplate rt = new RestTemplate();

    HttpEntity<String> response = rt.exchange(
            "http://localhost:8080/GraphClient/helloworld",
            HttpMethod.POST,
            entity,
            String.class
    );

    return response.getBody();
}


@RequestMapping(value="/helloworld", method=RequestMethod.GET, consumes=MediaType.APPLICATION_JSON_VALUE)
public String  helper( @RequestBody HashMap<String,String> values )
{
    String acumPlanets = "PLANETS HERE = ";
    for (Map.Entry<String, String> item : values.entrySet()) {
        System.out.println("Key " + item.getKey() + " Value " + item.getValue() );
        acumPlanets += item.getValue();
    }
    return acumPlanets;
}

你能意识到我在使用 RestTemplate 时做错了什么吗?

谢谢,

【问题讨论】:

  • 地图作为请求参数而不是 json 格式发送。使用常规地图而不是 LinkedMultiValueMap。此外,您的控制器应该在方法签名中使用Map 而不是HashMap。最后在发送时设置正确的内容类型(json),目前你什么都不设置。

标签: java spring spring-boot jboss postman


【解决方案1】:

您已定义端点来接收 HTTP GET 请求:

@RequestMapping(value="/helloworld", method=RequestMethod.GET, consumes=MediaType.APPLICATION_JSON_VALUE)

但实际上使用 RestTemplate 您正在执行 HTTP POST 请求:

HttpEntity<String> response = rt.exchange(
        "http://localhost:8080/GraphClient/helloworld",
        HttpMethod.POST,
        entity,
        String.class
);

【讨论】:

    【解决方案2】:

    建立在@Isakots 和@M.Deinum 的两个答案之上。

    这里有两个问题需要解决:

    1. 您正在通过不利的 GET 请求发送 JSON 正文
    2. 您正在尝试使用 LinkedValueMap 发送一个类似于 {"key1":"value1"..} 的简单 json 对象。

    为了解决第一个问题。您应该将端点定义为 POST。 示例:

    @RequestMapping(value="/helloworld", method=RequestMethod.POST, consumes=MediaType.APPLICATION_JSON_VALUE)
    

    为了解决第二个问题,请将您的 LinkedValueMap 替换为 Map。示例:

        Map<String, String> body = new HashMap<>();
        body.put("planet1","Jupiter");
        body.put("planet2", "Mercury");
        body.put("planet3", "Venus");
        body.put("planet4", "Mars");
        body.put("planet5", "Earth");
    
        HttpEntity<?> entity = new HttpEntity<>(body);
    
        RestTemplate rt = new RestTemplate();
    
        HttpEntity<String> response = rt.exchange(
                "http://localhost:8080/GraphClient/helloworld",
                HttpMethod.POST,
                entity,
                String.class
        );
    

    这两项更改后,一切都应按预期工作。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2020-03-19
      • 2010-10-16
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2023-01-07
      • 1970-01-01
      • 2019-12-29
      相关资源
      最近更新 更多