forked from DevFW-CICD/spring-petclinic
Merge pull request #326 from aidenchiavatti:json-fix
* pr/326: Polish "Fix content negotiation for /vets" Fix content negotiation for /vets
This commit is contained in:
commit
44060315cc
3 changed files with 5 additions and 17 deletions
|
@ -1,5 +1,5 @@
|
||||||
/*
|
/*
|
||||||
* Copyright 2002-2013 the original author or authors.
|
* Copyright 2002-2018 the original author or authors.
|
||||||
*
|
*
|
||||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||||
* you may not use this file except in compliance with the License.
|
* you may not use this file except in compliance with the License.
|
||||||
|
@ -48,7 +48,7 @@ class VetController {
|
||||||
return "vets/vetList";
|
return "vets/vetList";
|
||||||
}
|
}
|
||||||
|
|
||||||
@GetMapping({ "/vets.json", "/vets.xml" })
|
@GetMapping({ "/vets" })
|
||||||
public @ResponseBody Vets showResourcesVetList() {
|
public @ResponseBody Vets showResourcesVetList() {
|
||||||
// Here we are returning an object of type 'Vets' rather than a collection of Vet
|
// Here we are returning an object of type 'Vets' rather than a collection of Vet
|
||||||
// objects so it is simpler for JSon/Object mapping
|
// objects so it is simpler for JSon/Object mapping
|
||||||
|
|
|
@ -23,13 +23,5 @@
|
||||||
</tr>
|
</tr>
|
||||||
</tbody>
|
</tbody>
|
||||||
</table>
|
</table>
|
||||||
|
|
||||||
<table class="table-buttons">
|
|
||||||
<tr>
|
|
||||||
<td><a th:href="@{/vets.xml}">View as XML</a></td>
|
|
||||||
<td><a th:href="@{/vets.json}">View as JSON</a></td>
|
|
||||||
</tr>
|
|
||||||
</table>
|
|
||||||
|
|
||||||
</body>
|
</body>
|
||||||
</html>
|
</html>
|
||||||
|
|
|
@ -17,10 +17,6 @@ import org.springframework.beans.factory.annotation.Autowired;
|
||||||
import org.springframework.boot.test.autoconfigure.web.servlet.WebMvcTest;
|
import org.springframework.boot.test.autoconfigure.web.servlet.WebMvcTest;
|
||||||
import org.springframework.boot.test.mock.mockito.MockBean;
|
import org.springframework.boot.test.mock.mockito.MockBean;
|
||||||
import org.springframework.http.MediaType;
|
import org.springframework.http.MediaType;
|
||||||
import org.springframework.samples.petclinic.vet.Specialty;
|
|
||||||
import org.springframework.samples.petclinic.vet.Vet;
|
|
||||||
import org.springframework.samples.petclinic.vet.VetController;
|
|
||||||
import org.springframework.samples.petclinic.vet.VetRepository;
|
|
||||||
import org.springframework.test.context.junit4.SpringRunner;
|
import org.springframework.test.context.junit4.SpringRunner;
|
||||||
import org.springframework.test.web.servlet.MockMvc;
|
import org.springframework.test.web.servlet.MockMvc;
|
||||||
import org.springframework.test.web.servlet.ResultActions;
|
import org.springframework.test.web.servlet.ResultActions;
|
||||||
|
@ -65,15 +61,15 @@ public class VetControllerTests {
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void testShowResourcesVetList() throws Exception {
|
public void testShowResourcesVetList() throws Exception {
|
||||||
ResultActions actions = mockMvc.perform(get("/vets.json").accept(MediaType.APPLICATION_JSON))
|
ResultActions actions = mockMvc.perform(get("/vets")
|
||||||
.andExpect(status().isOk());
|
.accept(MediaType.APPLICATION_JSON)).andExpect(status().isOk());
|
||||||
actions.andExpect(content().contentType("application/json;charset=UTF-8"))
|
actions.andExpect(content().contentType("application/json;charset=UTF-8"))
|
||||||
.andExpect(jsonPath("$.vetList[0].id").value(1));
|
.andExpect(jsonPath("$.vetList[0].id").value(1));
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void testShowVetListXml() throws Exception {
|
public void testShowVetListXml() throws Exception {
|
||||||
mockMvc.perform(get("/vets.xml").accept(MediaType.APPLICATION_XML))
|
mockMvc.perform(get("/vets").accept(MediaType.APPLICATION_XML))
|
||||||
.andExpect(status().isOk())
|
.andExpect(status().isOk())
|
||||||
.andExpect(content().contentType(MediaType.APPLICATION_XML_VALUE))
|
.andExpect(content().contentType(MediaType.APPLICATION_XML_VALUE))
|
||||||
.andExpect(content().node(hasXPath("/vets/vetList[id=1]/id")));
|
.andExpect(content().node(hasXPath("/vets/vetList[id=1]/id")));
|
||||||
|
|
Loading…
Reference in a new issue