SpringBoot REST put单元测试返回406-手动测试200

SpringBoot REST put单元测试返回406-手动测试200,rest,unit-testing,spring-boot,Rest,Unit Testing,Spring Boot,我尝试在我的应用程序中集成Unittests,但在测试PUT(JSON)RESTAPI时失败了 测试代码: @RunWith(SpringRunner.class) @SpringBootTest @AutoConfigureMockMvc public class EventOrderRestTest { @Autowired private MockMvc mvc; @Autowired ObjectMapper objectMapper; pr

我尝试在我的应用程序中集成Unittests,但在测试PUT(JSON)RESTAPI时失败了

测试代码:

@RunWith(SpringRunner.class)
@SpringBootTest
@AutoConfigureMockMvc
public class EventOrderRestTest {

    @Autowired
    private MockMvc mvc;

    @Autowired
    ObjectMapper objectMapper;

    private Integer id;


    @Test
    public void a_saveNewEventOrder() throws Exception {

        EventOrder o = new EventOrder();
        o.setPlz(54321);

        this.id = Integer.parseInt(
                mvc.perform(put("/order")
                        .contentType(MediaType.APPLICATION_JSON)
                        .content(objectMapper.writeValueAsString(o))
                        .accept(MediaType.TEXT_PLAIN))
                    .andExpect(status().isOk())
                    .andReturn().getResponse().getContentAsString()
        );

    }
我的应用程序中没有其他测试配置

因此,当运行API并手动调用它时,它会返回代码200和新的ObjectId

当测试在body中使用相同的值运行时,它返回代码406

怎么了?我错过了什么?

406 HTTP状态-

也许这就是问题的原因

.accept(MediaType.TEXT_PLAIN))
406 HTTP状态-

也许这就是问题的原因

.accept(MediaType.TEXT_PLAIN))

如果要获取纯文本,则负责put/order端点的控制器中的方法应返回字符串。该方法应如下所示:

@PutMapping(path="/order")
public @ResponseBody String putOrder() { 
   ...
   return "result";
}

如果要获取纯文本,则负责put/order端点的控制器中的方法应返回字符串。该方法应如下所示:

@PutMapping(path="/order")
public @ResponseBody String putOrder() { 
   ...
   return "result";
}

确实。。。谢谢我希望这是一个基本的设置。它确实。。。谢谢我希望这是一个基本的设置。