TestRestTemplate postForEntity不发送请求者Spring Boot 1.4

时间:2016-08-23 17:11:12

标签: spring spring-boot

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

这是经过测试的终点:

    @RestController
public class ServerController {

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

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

等。 当我调试它时,在exampleEndpoint中,in参数始终为null。 我使用的是Spring Boot 1.4 Spring 4.3.2

我更改了名称并删除了所有公司的内容,但除此之外它就是这样,它的工作方式是它击中了端点,只是请求没有通过。

1 个答案:

答案 0 :(得分:1)

您需要使用RequestBody对方法exampleEndpoint中的参数进行注释,如下所示:

81 20