spring-projects / spring-data-examples

Spring Data Example Projects
Apache License 2.0
5.2k stars 3.4k forks source link

Add FeignClient example #597

Closed khauser closed 3 years ago

khauser commented 3 years ago

With

@AutoConfigureMockMvc
@SpringBootTest(webEnvironment = WebEnvironment.RANDOM_PORT)
public class BookingTest {
    private static final Long EXPECTED_BOOKING_ID = 1L;
    private static final String EXPECTED_BOOKING_NAME_2 = "NewTestBooking";

    @Autowired
    private MockMvc mvc;

    private String patchPayload;
    @Test
    public void test_patch() throws Exception {
        givenPatchItem();

        mvc.perform(MockMvcRequestBuilders.patch(BookingController.REQUEST_MAPPING + "/" + EXPECTED_BOOKING_ID)
                .contentType(JSON_PATCH_JSON).content(patchPayload)).andDo(print()).andExpect(status().isNoContent());

        mvc.perform(MockMvcRequestBuilders.get(BookingController.REQUEST_MAPPING + "/" + EXPECTED_BOOKING_ID)
                .accept(MediaType.APPLICATION_JSON)).andDo(print())
                .andExpect(jsonPath("$.name").value(EXPECTED_BOOKING_NAME_2));
    }

    @SneakyThrows
    private void givenPatchItem() {
        HashMap<String, Object> patchItem = new HashMap<>();
        patchItem.put("op", "replace");
        patchItem.put("path", "/name");
        patchItem.put("value", EXPECTED_BOOKING_NAME_2);
        patchPayload = new ObjectMapper().writeValueAsString(singletonList(patchItem));
    }

I'm able to test Spring data rest jsonpatch, which is greate.

But how a FeignClient on this would look like?

Trying with:

    @PatchMapping(path = BOOKING_ID_PATH, consumes = MediaType.APPLICATION_JSON_VALUE)
    ResponseEntity<Void> jsonPatch(@PathVariable(value = ID) Long id, @RequestBody String patchPayload);

brings:

WARN 1 --- [nio-8585-exec-7] .w.s.m.s.DefaultHandlerExceptionResolver : Resolved [org.springframework.web.HttpRequestMethodNotSupportedException: Request method 'PATCH' not supported]
khauser commented 3 years ago
@PatchMapping(path = BOOKING_ID_PATH, consumes = "application/json-patch+json")
    ResponseEntity<Void> jsonPatch(@PathVariable(value = ID) Long id, @RequestBody String patchPayload);

works with e.g. https://github.com/khauser/JsonPatchShowcase