sách gpt4 ai đã đi

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

In lại 作者:行者123 更新时间:2023-12-04 17:57:25 27 4
mua khóa gpt4 Nike

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

@RequestMapping(value = "name/{name}",method = RequestMethod.GET)
public ResponseEntity 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

@SpringBootỨng dụng
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)
@Bộ điều khiển nghỉ ngơi
@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 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 response = ResponseEntity.ok().body(h);
return response;
}

如果 URL 是 "api/v1/books/name/sherlock_homes" 那么上面的方法不工作但是 URL 是 "api/v1/books/name/sherlockHomes" 然后它工作正常。

可能是什么问题?为什么 spring 不允许在路径变量中使用下划线?

您的帮助应该是可观的。

1 Câu trả lời

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

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

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

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

27 4 0
行者123
Hồ sơ cá nhân

Tôi là một lập trình viên xuất sắc, rất giỏi!

Nhận phiếu giảm giá Didi Taxi miễn phí
Mã giảm giá Didi Taxi
Giấy chứng nhận ICP Bắc Kinh số 000000
Hợp tác quảng cáo: 1813099741@qq.com 6ren.com