简体   繁体   中英

Mapping two @GetMapping to same @RestController into Spring Boot

I need to mapping two GET methods look like:

GET /tickets - Retrieves a list of tickets
GET /tickets/12 - Retrieves a specific ticket

But when I mapped this, the Spring got confused!

When I hit http://localhost:8080/tickets in the Chrome, the result on server is:

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:

QueryTranslatorFactoryInitiator  : HHH000397: Using ASTQueryTranslatorFactory

My Spring controller is:

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

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM