Почему поражены обе конечные точки Джерси? - PullRequest
0 голосов
/ 17 января 2020

Когда я go до .../bank/1, я вижу информацию об учетной записи, как и следовало ожидать. Отлично, хорошо, хорошо.

Когда я go до .../bank/1/description Я вижу описание ( хорошо ), но я также вижу информацию об учетной записи ( не хорошо ).

Я привык к Spring'у GetMapping, где вещи ломаются, если совпадают несколько путей - но даже тогда, AFAIK, в моем коде, только один должен совпадать в любом случае?

Почему оба AccountActions запускаются?


Банк. 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

1 Ответ

0 голосов
/ 17 января 2020

Заменить этот код

@ Path ("/ {id: \ d + $}")

на

@ Path ("/ {id: [0-9] *}")

...