3

Hi I am using spring boot 1.3.4. In my controller i have the following request mapping,

@RequestMapping(value = "name/{name}",method = RequestMethod.GET)
public ResponseEntity<Book> getBooksByName(@Valid @PathVariable("name") String name) {
    final Book h = mongoService.findByBookName(name);

Application.yml

server:
port: 8170
contextPath: /book-repository
spring:
    application:
        name: book-repository
    devtools:
        restart:
            exclude: META-INF/maven/**
            additional-paths: src/main/resources/
bookRepository:
    mongodb:
        connectionStrings:
            bookRepository: mongodb://localhost:27017/contentdb
springfox.documentation.swagger.v2.path: /api/v1
management.add-application-context-header: false

BookRepositoryMain.java

@SpringBootApplication
public class BookRepositoryMain {

public static final Logger LOG = LoggerFactory.getLogger(BookRepositoryMain.class);

public static void main(String[] args) {
    LOG.info("Initialising ...");
    SpringApplication.run(BookRepositoryMain.class, args);
}

}

BookController.java

@Api(value = "books", description = "books endpoint", tags = {"books"}, produces = MediaType.APPLICATION_JSON_VALUE)
@RestController
@RequestMapping(value = PATH, produces = MediaType.APPLICATION_JSON_VALUE)
public class BooksController {
public static final Logger LOG = LoggerFactory.getLogger(BooksController.class);
public static final String PATH = "/books";

@RequestMapping(
        value = "name/{name}",
        method = RequestMethod.GET)
@ApiOperation(
        response = Book.class,
        notes = "",
        value = "Finds Book by its name.")
@ApiResponses({
    @ApiResponse(code = 404, response = ErrorMessage.class, message = "Book not found.")
})
public ResponseEntity<Book> getBookByName(@PathVariable("name") String uid) throws ContentNotFoundException {
    final Book h = deviceMongoService.findByBookName(name);
    if (h == null) {
        throw new BookNotFoundException(String.format("Could not find book with name %s", name));
    }
    final ResponseEntity<Book> response = ResponseEntity.ok().body(h);
    return response;
}

}

If the URL is "api/v1/books/name/sherlock_homes" then the above method is not working but the URL is "api/v1/books/name/sherlockHomes" then it is working properly.

What might be the issue? Why spring doesn't allow underscore in path variable?

Your help should be appreciable.

VelNaga
  • 3,593
  • 6
  • 48
  • 82
  • This works fine for me, please provide a [mcve]. – Sotirios Delimanolis Aug 29 '16 at 22:38
  • @SotiriosDelimanolis but this is not working for me.I would say this is the Minimal,Complete, and Verifiable example.We don't have JSFiddle like Java otherwise i would reproduce this issue.Kindly let me know what shall i do next. – VelNaga Aug 29 '16 at 22:43
  • This is definitely not a [mcve], you've only provided your handler method. Let's see your relevant configuration. Get rid of all the domain specific code. We don't care about `Book` for example. We only care about the routing behavior of Spring MVC. Edit your question to contain something I can plug and run (as well as a URL example). That should be easy with Spring Boot. – Sotirios Delimanolis Aug 29 '16 at 22:44
  • @SotiriosDelimanolis Let me add a application.yml file – VelNaga Aug 29 '16 at 22:52
  • @SotiriosDelimanolis please refer the application.yml apart from that i didn't have any configuration in my application.Let me know for any more informations needed to resolve this issue. – VelNaga Aug 29 '16 at 22:58
  • Let's see your `main` method and the enclosing class. Let's see your `@Controller` class. – Sotirios Delimanolis Aug 29 '16 at 23:02
  • @SotiriosDelimanolis I updated my question with main class & controller class. – VelNaga Aug 29 '16 at 23:18
  • Can you describe what _not working_ means? Do you get a 404? What do you expect to happen, what actually happens? – Sotirios Delimanolis Aug 30 '16 at 00:14
  • @SotiriosDelimanolis no error.control not comes to the method.in the console I can see no matches found – VelNaga Aug 30 '16 at 05:41

1 Answers1

1

You can use regex in @RequestMapping value. For example you may try:

@RequestMapping(value = "name/{name:.+}",method = RequestMethod.GET)

This is documented in spring docs and also found in this similar question

Community
  • 1
  • 1
Laksitha Ranasingha
  • 4,321
  • 1
  • 28
  • 33
  • @Lakshita Sorry it is not working.I already tried that example. – VelNaga Aug 29 '16 at 22:18
  • @SotiriosDelimanolis because I have done something similar in the past? what's your solution? – Laksitha Ranasingha Aug 29 '16 at 22:20
  • @SotiriosDelimanolis path variable with "_" underscore is not working.It is working fine with alpha numeric characters. – VelNaga Aug 29 '16 at 22:22
  • I commented what you asked for. Who said my comment is an answer?, The answer is already there. – Laksitha Ranasingha Aug 29 '16 at 22:24
  • Let me clarify. Your answer makes a suggestion without explaining why it would fix the issue OP is seeing. I want you to explain why the OP is having the issue in the first place, how using a regular expression in the path mapping can fix it, and how the links you've provided are related. At the moment, it looks like you're simply copying an answer from another post (just vote/flag to close as duplicate if that's the case), without any justification. – Sotirios Delimanolis Aug 29 '16 at 22:30
  • @SotiriosDelimanolis you have got a point. when I saw the question; I have remembered something similar that I have done in the past. I thought this is kind a straightforward issue and the question I linked is not an exact duplicate, that's why I haven't done much elaboration. – Laksitha Ranasingha Aug 29 '16 at 22:37
  • Strange, this solved my problem, so what now? Upvoting. – Audrius Meškauskas Jan 06 '17 at 14:05