spring-mvc - 带下划线的 Spring 引导路径变量不调用 Controller

标签 spring-mvc spring-boot

您好,我正在使用 spring boot 1.3.4。在我的 Controller 中,我有以下请求映射,

@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 不允许在路径变量中使用下划线?

您的帮助应该是可观的。

最佳答案

您可以在@RequestMapping 值中使用正则表达式。例如你可以尝试:

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

这记录在 spring docs 中也可以在这个 similar question 中找到

关于spring-mvc - 带下划线的 Spring 引导路径变量不调用 Controller ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39215722/

相关文章:

spring-mvc - SetLocale 无法在 jsp 站点中使用德语格式化货币

java - 如何从 Spring Controller 启动 CommandLineRunner

java - Spring 1.4.0 发布 hibernate 命名策略将在表名和列名中添加下划线(_)

spring - 无法连接到运行 Junit 测试的 Redis

spring - 使用 Spring Boot 进行内存身份验证

Spring Security @PreAuthorize 应用程序本身作为用户

java - 使用 Spring MVC Web 应用程序强制以 html 形式进行 UTF8 编码

spring - 多部分文件最大大小异常——spring boot 内嵌tomcat

eclipse - STS 3.4.0 Gradle 支持不起作用

tomcat - 不带嵌入式 Tomcat 的 Spring Boot 管理