Peter
Peter

Reputation: 1

TestRestTemplate postForEntity does not send the requestbody Spring Boot 1.4

@RunWith(SpringRunner.class)
@SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT, classes = ServerApplication.class)
public class ExampleTest {
    public static final String EXAMPLE = "/example";

//this is the TestRestTemplate used   
     @Autowired
    TestRestTemplate testRestTemplate;

//this is the test
   @Test
    public void testExample() {
        String s = "asd";
        Object response = testRestTemplate.postForEntity(EXAMPLE, s, String.class ,Collections.emptyMap());
    }
}

This is the tested endpoint:

    @RestController
public class ServerController {

    @ResponseBody
    @PostMapping("/example")
    public String exampleEndpoint (String in) {
        return in;
    }
}

@SpringBootApplication
@ComponentScan("com.company.*")
public class ServerApplication {

etc. When I debug it, at the exampleEndpoint the in parameter is always null. I'm using Spring Boot 1.4 Spring 4.3.2

I changed the names and removed all the company stuff, but otherwise this is how it is, it works in the sense that it hits the endpoint, it just that the request doesn't go through.

Upvotes: 0

Views: 2164

Answers (1)

Billy Korando
Billy Korando

Reputation: 235

You need to annotate the argument in your method exampleEndpoint with RequestBody like so:

 public String exampleEndpoint (@RequestBody String in) {
        return in;
    }

Upvotes: 1

Related Questions