[英]Mapping two @GetMapping to same @RestController into Spring Boot
I need to mapping two GET methods look like: 我需要映射两个GET方法,如下所示:
GET /tickets - Retrieves a list of tickets
GET /tickets/12 - Retrieves a specific ticket
But when I mapped this, the Spring got confused! 但是当我绘制此图时,Spring感到困惑!
When I hit http://localhost:8080/tickets in the Chrome, the result on server is: 当我在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"]
When I hit http://localhost:8080/tickets/12 in the Chrome, the result on server is: 当我在Chrome中点击http:// localhost:8080 / tickets / 12时,服务器上的结果是:
QueryTranslatorFactoryInitiator : HHH000397: Using ASTQueryTranslatorFactory
My Spring controller is: 我的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));
}
}
My unit test is: 我的单元测试是:
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")));
}
}
I'm using Java 11 and Spring Boot 2.1.6 我正在使用Java 11和Spring Boot 2.1.6
Use 采用
@RestController
@RequestMapping("/tickets")
...
@GetMapping
...
@GetMapping("{id}")
In your code 1) @RestController("tickets")
means 'create bean named "tickets"' 2) second URL ( @GetMapping("/{id}")
) tells 'put ID at root' ( http://localhost:8080/ID ) - so controller cannot convert 'tickets' to long. 在您的代码中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.