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.