Home > Mobile >  Why passing Map<String,String> in postman as JSON works but passing the map in Java RestTempla
Why passing Map<String,String> in postman as JSON works but passing the map in Java RestTempla

Time:01-07

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:

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:

{
"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. 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?

Thanks,

CodePudding user response:

You defined your endpoint to receive HTTP GET requests:

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

But actually with RestTemplate you are performing a HTTP POST request:

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

CodePudding user response:

Building on top of both answers by @Isakots and @M.Deinum.

You have two issues to fix here:

  1. You're sending a JSON body through a GET request which is not favorable
  2. You're trying to send a simple json object similar to {"key1":"value1"..} using LinkedValueMap.

In order to resolve the first issue. You should define your endpoint as a 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. 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.

  •  Tags:  
  • Related