你好,我正在使用Spring Boot 1.3.4。在我的控制器中,我有以下请求映射:
@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;
}
如果URL是"api/v1/books/name/sherlock_homes",那么上述方法不起作用,但是如果URL是"api/v1/books/name/sherlockHomes",那么它可以正常使用。
可能是什么问题?为什么Spring不允许路径变量中有下划线?
感谢您的帮助。