[英]Mapping two @GetMapping to same @RestController into Spring Boot
我需要映射兩個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
采用
@RestController
@RequestMapping("/tickets")
...
@GetMapping
...
@GetMapping("{id}")
在您的代碼中1) @RestController("tickets")
意味着“創建名為“ tickets”的bean” 2)第二個URL( @GetMapping("/{id}")
)告訴'put id at root'( http:// localhost :8080 / ID )-因此控制器無法將“票證”轉換為long。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.