当我转到.../bank/1
时,我会看到一个帐户信息,就像预期的那样。 很好,很好。
[当我转到.../bank/1/description
时,我看到说明(good),但是我也看到了帐户信息([not好)。
我习惯于Spring的GetMapping
,如果多个路径匹配,事情就会中断-但是即使那样,在我的代码中AFAIK,仅一个也应该匹配吗?
AccountActions
?Bank.java
Path("/bank")
public class Bank {
@Context
UriInfo uriInfo;
@Context
Request request;
// ... Other irrelevant constructors, methods, and attributes
@GET
@Path("{acct}")
public AccountAction getAccount(@PathParam("acct") String id) {
LOGGER.log(Level.INFO, "- URL: " + uriInfo.getPath());
return new AccountAction(uriInfo, request, id, accounts);
}
@GET
@Path("{acct}/description")
public AccountAction getDescription(@PathParam("acct") String id) {
LOGGER.log(Level.INFO, "- URL: " + uriInfo.getPath());
return new AccountAction(uriInfo, request, id, accounts);
}
}
AccountAction.java
public class AccountAction {
@Context
UriInfo uriInfo;
@Context
Request request;
// ... Other irrelevant constructors, methods, and attributes
public AccountAction(UriInfo uriInfo, Request request, String id, AccountStore accounts) {
this.uriInfo = uriInfo;
this.request = request;
this.id = new Integer(id);
this.accounts = accounts;
}
@GET
@Path("/{id:\\d+}/description")
@Produces(MediaType.TEXT_PLAIN)
public String getDescription() {
LOGGER.log(Level.INFO, "- URL: " + uriInfo.getPath());
Account a = accounts.find(id);
if (a == null) {
throw new RuntimeException("No such account: " + id);
}
return a.getDescription();
}
@GET
@Path("/{id:\\d+$}")
@Produces(MediaType.APPLICATION_XML)
public Account getAccount() {
LOGGER.log(Level.INFO, "- URL: " + uriInfo.getPath());
Account a = accounts.find(id);
if (a == null) {
throw new RuntimeException("No such account: " + id);
}
return a;
}
}
日志输出:
17-Jan-2020 11:54:57.526 INFO [http-nio-8080-exec-1] edu...Bank.getDescription - URL: bank/1/description
17-Jan-2020 11:54:58.139 INFO [http-nio-8080-exec-1] edu...AccountAction.getAccount - URL: bank/1/description
17-Jan-2020 11:54:58.140 INFO [http-nio-8080-exec-1] edu...AccountAction.getDescription - URL: bank/1/description
替换此代码
@ Path(“ / {id:\ d + $}”)
作者
@ Path(“ / {id:[0-9] *}”)]