0

我的问题是如果我使用@PathParam,我如何验证请求参数。

例如我有两个请求参数,name 和 id

path is localhost:/.../search/namevalue/idvalue

如果用户提交姓名或 ID 的空白,我应该发送一个回复,提及该姓名是必需的/ID 是必需的。

如果我使用@QueryParam,我可以进行验证,但如果我必须使用路径变量,我不知道该怎么做。

如果我只是测试使用http:/localhost:/.../search/namevalueorhttp:/localhost:/.../search/idvalue或者http:/localhost:/.../search/它抛出 servlet 异常。

下面是代码,如果我使用 QueryParams 验证工作得很好,请让我知道当我使用 pathparam 时的方法

 @Controller
 @Path("/customer")
 public class CustomerController extends BaseController implements Customer {

@Override
@GET
@Produces({ "application/json", "application/xml" })
@Path("/search/{name}/{id}/")
public Response searchCustomerDetails(
        @PathParam("name") String name,
        @PathParam("id") Integer id) {

    ResponseBuilder response = null;
    CustomerValidations validations = (CustomerValidations) getAppContext()
            .getBean(CustomerValidations.class);
    CustomerResponse customerResponse = new CustomerResponse();
    CustomerService customerService = (CustomerService) getAppContext()
            .getBean(CustomerService.class);

    try {
        validations.searchCustomerDetailsValidation(
                name, id,customerResponse);

        if (customerResponse.getErrors().size() == 0) {
            CustomerDetails details = customerService
                    .searchCustomerDetailsService(name, id);
            if (details == null) {
                response = Response.status(Response.Status.NO_CONTENT);

            } else {
                customerResponse.setCustomerDetails(details);
                response = Response.status(Response.Status.OK).entity(
                        customerResponse);
            }
        } else {

            response = Response.status(Response.Status.BAD_REQUEST).entity(
                    customerResponse);
        }
    }

    catch (Exception e) {
        LOGGER.error(e.getMessage());
        response = Response.status(Response.Status.INTERNAL_SERVER_ERROR);

    }

    return response.build();
} }


@Component
@Scope("prototype")
public class CustomerValidations {

public void searchCustomerDetailsValidation(
        String name, Integer id,
        CustomerResponse customerResponse) {


    if (id == null) {

        customerResponse.getErrors().add(
                new ValidationError("BAD_REQUEST",
                        ""invalid id));
    }

    if (name== null
            || (name!= null && name
                    .trim().length() == 0)) {

        customerResponse.getErrors().add(
                new ValidationError("BAD_REQUEST", "invalid id"));
    }
} }

@XmlRootElement
 public class CustomerResponse {

private CustomerDetails customerDetails;
private List<ValidationError> errors = new ArrayList<ValidationError>();

//setters and getters }



public class ValidationError {

private String status;
private String message;


public ValidationError() {

}

public ValidationError(String status, String message) {
    super();
    this.status = status;
    this.message = message;
}
//setters and getters }
4

1 回答 1

1

您收到异常是因为您没有映射到@Path("/search/{foo}/")or的方法@Path("/search/"),因此您应该得到默认的 404 响应,因为这些路径并未真正定义。

我不确定你为什么要验证这些“缺失”的请求路径 - 看起来这个端点旨在用作查询端点,所以我建议你使用@RequestParam/query 参数来更 RESTful 描述你的搜索'正在尝试。的路径search/{name}/{id}将建议永久存在于此 URL 的特定资源,但在这种情况下,您正在查询此控制器上的客户。

我建议你/search完全放弃路径,只需将查询参数映射到客户控制器的“根”,这样你就会得到类似的东西

@Controller
@Path("/customer")
public class CustomerController extends BaseController implements Customer {

    @GET
    @Produces({"application/json", "application/xml"})
    public Response searchCustomerDetails(
            @RequestParam("name") String name,
            @RequestParam("id") Integer id) {

            // Returns response with list of links to /customer/{id} (below)

    }


    @GET
    @Produces({"application/json", "application/xml"})
    @Path("/{id}")
    public Response getCustomerDetails(@PathVariable("id") String id) {

            // GET for specific Customer
    }
}
于 2014-07-07T08:22:13.897 回答