简体   繁体   中英

Spring MVC @Pathvariable won't work

I have an interface and an implementation class:

Interface

@RequestMapping(value = CUSTOMER_PATH + "{id}", method = RequestMethod.GET, produces = {MediaType.APPLICATION_JSON + CHARSET_UTF_8_ENCODING})
    Response getCustomerDetails(@PathVariable(CONTACT_ID) String id);

Impl

@Override
public Response getCustomerDetails(String id) {
    String methodname = "getCustomerDetails: ";
    LOG.info(methodname + "Get Customer Details");

    Response response;
    System.out.println("******************id: " + id);

    if (StringUtils.isEmpty(id)) {
        response = Response.status(Status.BAD_REQUEST).entity(MockData.customerDetailsInvalid).build();
        LOG.info(methodname + "Returning Customer's Details - Status: Invalid Request");
    } else {
        response = Response.status(Status.OK).entity(MockData.customerDetails).build();
        LOG.info(methodname + "Returning Customer's Details - Status: OK");

    }

    return response;
}

The path ..../MockDataProvider-war/services/mock/customers/ 7 returns “invalid request”. The value for id is printed out as null, even though it should be 7.

Anyone know why id is null?

原因是@PathVariable(CONTACT_ID)它应该是@PathVariable String id可以@PathVariable String id

Fixed it by changing the Interface from:

Response getCustomerDetails(@PathVariable(CONTACT_ID) String id);

to:

Response getCustomerDetails(String id);

and the Impl from:

public Response getCustomerDetails(String id) 

to

public Response getCustomerDetails(@PathVariable(CONTACT_ID) String id)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM