我需要映射两个GET方法,如下所示:
GET /tickets - Retrieves a list of tickets
GET /tickets/12 - Retrieves a specific ticket
但是当我绘制此图时,Spring感到困惑!
当我在Chrome中点击http://localhost:8080/tickets时,服务器上的结果是:
DefaultHandlerExceptionResolver : Resolved [org.springframework.web.method.annotation.MethodArgumentTypeMismatchException: Failed to convert value of type 'java.lang.String' to required type 'java.lang.Long'; nested exception is java.lang.NumberFormatException: For input string: "tickets"]
当我在Chrome中点击http://localhost:8080/tickets/12时,服务器上的结果是:
QueryTranslatorFactoryInitiator : HHH000397: Using ASTQueryTranslatorFactory
我的Spring控制器是:
package wendelsilverio.api.ticket;
import java.util.List;
import java.util.Optional;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RestController;
@RestController("tickets")
public class TicketController {
@Autowired
private TicketRepository repository;
@GetMapping
public List<TicketEntity> getTickets() {
return repository.findAll();
}
@GetMapping("/{id}")
public Optional<TicketEntity> getTicket(@PathVariable("id") Long id) {
return repository.findById(Long.valueOf(id));
}
}
我的单元测试是:
package wendelsilverio.api.ticket;
import static org.hamcrest.CoreMatchers.is;
import static org.mockito.BDDMockito.given;
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.jsonPath;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status;
import java.util.Arrays;
import java.util.Optional;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.autoconfigure.web.servlet.AutoConfigureMockMvc;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.boot.test.mock.mockito.MockBean;
import org.springframework.http.MediaType;
import org.springframework.test.context.junit4.SpringRunner;
import org.springframework.test.web.servlet.MockMvc;
@SpringBootTest
@AutoConfigureMockMvc
@RunWith(SpringRunner.class)
public class TicketControllerRestfulTest {
@Autowired
private MockMvc mockMvc;
@MockBean
private TicketController mockTicketController;
@Test
public void getTickets() throws Exception {
given(mockTicketController.getTickets())
.willReturn(Arrays.asList(new TicketEntity(1L, "First ticket"), new TicketEntity(2L, "Second ticket")));
mockMvc.perform(get("tickets").contentType(MediaType.APPLICATION_JSON)).andExpect(status().isOk())
.andExpect(jsonPath("$[0].content", is("First ticket")))
.andExpect(jsonPath("$[1].content", is("Second ticket")));
}
@Test
public void getTicket12() throws Exception {
Optional<TicketEntity> twelveTicket = Optional.of(new TicketEntity(12L, "Twelve ticket"));
given(mockTicketController.getTicket(12L)).willReturn(twelveTicket);
mockMvc.perform(get("tickets/12").contentType(MediaType.APPLICATION_JSON)).andExpect(status().isOk())
.andExpect(jsonPath("$.id", is(12L))).andExpect(jsonPath("$.content", is("Twelve ticket")));
}
}
我正在使用Java 11和Spring Boot 2.1.6
答案 0 :(得分:3)
使用
{'a': 15, 'b': 22, 'c': 31, 'd': 42}
在您的代码中
1)@RestController
@RequestMapping("/tickets")
...
@GetMapping
...
@GetMapping("{id}")
的意思是“创建名为“票证”的bean”
2)第二个URL(@RestController("tickets")
)告诉'root的put ID'(http://localhost:8080/ID)-因此控制器无法将'ticket'转换为long。