0

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

1
  • Try @GetMapping("{id}") Commented Jul 26, 2019 at 16:10

1 Answer 1

4

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.

Sign up to request clarification or add additional context in comments.

5 Comments

@michalk, really - I see one more mistake. Try corrected version.
This is just unbelievable that I could miss that typo with passing component name as request path to @RestController. Happened like 2nd time to me. Yep it works.
I missed it too :-) So, you can mark my replay as answer in this case.
The problem is I am not OP :P
@michalk, oops, it's evening of Friday... sorry :-)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.