title: How to validate path variables in REST
body: <p>My Question is How can I validate request parameters if I use @PathParam.</p>
  
  <p>For instance I have two request parameters, name and id</p>
  
  <pre><code>path is localhost:/.../search/namevalue/idvalue
  </code></pre>
  
  <p>if a user submits blanks for name or id I should send a response mentioning that name is required/ id is required.</p>
  
  <p>I could do the validations if I use @QueryParam, but I'm not sure how to do it if I have to use pathvariables.</p>
  
  <p>If I just test using <code>http:/localhost:/.../search/namevalue</code> or<code>http:/localhost:/.../search/idvalue</code> or <code>http:/localhost:/.../search/</code> it's throwing servlet exception.</p>
  
  <p>Below is the code, if i use QueryParams validations work just fine, Please let me know the approach when i use pathparam</p>
  
  <pre><code> @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 &amp;&amp; name
                      .trim().length() == 0)) {
  
          customerResponse.getErrors().add(
                  new ValidationError("BAD_REQUEST", "invalid id"));
      }
  } }
  
  @XmlRootElement
   public class CustomerResponse {
  
  private CustomerDetails customerDetails;
  private List&lt;ValidationError&gt; errors = new ArrayList&lt;ValidationError&gt;();
  
  //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 }
  </code></pre>