gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-04 17:57:25 26 4
gpt4 key购买 nike

您好,我正在使用 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/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com