George Sexton
George Sexton

Reputation: 131

TestRestTemplate.postForEntity returns 400 with No Descriptive Message

I'm having some trouble using TestRestTemplate.postForEntity(). My code is:

@Test
public void testAdd() {
    Map<String,String> mParams=new HashMap<>();
    mParams.put("joke","This is my joke");
    mParams.put("description","Totally Tasteless Joke");
    mParams.put("date",Long.toString(System.currentTimeMillis()));
    ResponseEntity<Joke> reJoke = restTemplate.postForEntity(getURLBase()+"/add",
                                                             null,
                                                             Joke.class,
                                                             mParams
                                                             );

    Joke j = reJoke.getBody();

    System.out.println("Status="+reJoke.getStatusCode()+" j.getJoke()="+j.getJoke()+" id="+j.getId());
}

The returned Status value is 400. Nothing is printed on the console.

I have a testGet() that does work:

@Test
public void testGet() {
    System.out.println("testGet()");
     initData();

     ResponseEntity<Joke> reJoke=restTemplate.getForEntity(getURLBase()+"/1",Joke.class,new HashMap<String,String>());
     Joke j=reJoke.getBody();
     System.out.println("Status=" + reJoke.getStatusCode()+" j =" + j + (j == null ? "" : j.getJoke()));
}

I noticed in the Javadoc for TestRestTemplate it says:

If Apache Http Client 4.3.2 or better is available (recommended) it will be used as the client, and by default configured to ignore cookies and redirects.

I've added

    <dependency>
        <groupId>org.apache.httpcomponents</groupId>
        <artifactId>httpclient</artifactId>
        <version>4.5.5</version>
        <scope>test</scope>
    </dependency>

to pom.xml, but it doesn't seem to make any difference.

Can anyone tell me how to either solve the issue, or get more information than "400"? It's really appreciated.

Upvotes: 1

Views: 736

Answers (1)

George Sexton
George Sexton

Reputation: 131

After a little searching, I found that the mParams was actually URL parameters. I needed to send the form encoded parameters as the request object. The working code is:

@Test
public void testAdd() {
    MultiValueMap<String, String> mParams= new LinkedMultiValueMap<String, String>();
    mParams.add("joke", "This is my joke");
    mParams.add("description", "Totally Tasteless Joke");
    mParams.add("date", Long.toString(System.currentTimeMillis()));
    HttpHeaders headers = new HttpHeaders();
    headers.setContentType(MediaType.APPLICATION_FORM_URLENCODED);

    HttpEntity<MultiValueMap<String, String>> request = new HttpEntity<MultiValueMap<String, String>>(mParams, headers);

    ResponseEntity<Joke> reJoke = restTemplate.postForEntity(getURLBase()+"/add",
                                                             request,
                                                             Joke.class
                                                             );

    Joke j = reJoke.getBody(); 
    System.out.println("Status="+reJoke.getStatusCode()+" j.getJoke()="+j.getJoke()+" id="+j.getId()); 
}

Upvotes: 1

Related Questions