简体   繁体   English

为什么要通过 Map<string,string> 在 postman 作为 JSON 工作,但通过 map 在 ZD52387880E1EA22817A72D37592 工作?</string,string>

[英]Why passing Map<String,String> in postman as JSON works but passing the map in Java RestTemplate does not work?

This project is for a WAR application which run over JBOSS Application Server (GraphClient.war), after the deployment of it I can do requests to it using URLs like:该项目适用于在 JBOSS 应用程序服务器 (GraphClient.war) 上运行的 WAR 应用程序,在部署它之后,我可以使用以下 URL 对其进行请求:

http://localhost:8080/GraphClient/helloworld

I call this controller passing a Map<String,String>, using postman an configuring Body > RAW > Json (application/json) and passing the map like: I call this controller passing a Map<String,String>, using postman an configuring Body > RAW > Json (application/json) and passing the map like:

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

IT WORKS, but if I send the same Map<String,String> in Java from another controller I get a 405 method not allowed.它可以工作,但是如果我从另一个 controller 在 Java 中发送相同的 Map<String,String> ,我会得到不允许的 405 方法。 Here is the code:这是代码:

@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;
}

Can you realize what I'm doing wrong with the RestTemplate?你能意识到我在使用 RestTemplate 时做错了什么吗?

Thanks,谢谢,

You defined your endpoint to receive HTTP GET requests:您将端点定义为接收 HTTP GET请求:

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

But actually with RestTemplate you are performing a HTTP POST request:但实际上使用 RestTemplate 您正在执行 HTTP POST请求:

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

Building on top of both answers by @Isakots and @M.Deinum.建立在@Isakots 和@M.Deinum 的两个答案之上。

You have two issues to fix here:您有两个问题需要在这里解决:

  1. You're sending a JSON body through a GET request which is not favorable您正在通过不利的 GET 请求发送 JSON 正文
  2. You're trying to send a simple json object similar to {"key1":"value1"..} using LinkedValueMap.您正在尝试使用 LinkedValueMap 发送类似于 {"key1":"value1"..} 的简单 json object。

In order to resolve the first issue.为了解决第一个问题。 You should define your endpoint as a POST .您应该将端点定义为POST Example:例子:

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

In order to resolve the second issues replace your LinkedValueMap with a Map .为了解决第二个问题,将LinkedValueMap替换为Map Example:例子:

    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
    );

After these two changes everything should work as expected.在这两个更改之后,一切都应该按预期工作。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM