0
@POST
@Path("/login")
@Consumes(MediaType.APPLICATION_JSON)
@Produces(MediaType.APPLICATION_JSON)
public Response login(Member member) {
    Response.ResponseBuilder builder = null;
    try {
        LogMsg.info("Login User " + member.getUsername() + " Password : "+ member.getPassword());
        member.setPassword(member.getPassword());
        repository.validateLogin(member);
         builder = Response.ok();
    } catch (ConstraintViolationException ce) {
        ErrorMessages errorMessage = new ErrorMessages(ce.getMessage(), 409, "username is wrong");  
        LogMsg.error("ConstraintViolationException  " + ce.getMessage());
        builder = Response.status(409)
                        .entity(errorMessage);  
    } catch (ValidationException e) {
        Map<String, String> responseObj = new HashMap<String, String>();
        responseObj.put("username", "Invalid Username");
        ErrorMessages errorMessage = new ErrorMessages(e.getMessage(), 404, "password is wrong");   
        LogMsg.error("ValidationException  " + e.getMessage());
        builder = Response.status(404)
                        .entity(errorMessage);                          
    } catch (Exception e) {
        Map<String, String> responseObj = new HashMap<String, String>();
        responseObj.put("error", e.getMessage());
        LogMsg.error(e.getMessage());
        builder = Response.status(Response.Status.BAD_REQUEST).entity(
                responseObj);
    }
    return builder.build();
}

Note:If user name and password are correct i am sending the 200k to client. 1)if username and password is wrong we are raising the exception that exception code i need to send to client. can any body help me in this.

Thanks, Madan

Madan Mohan
  • 47
  • 1
  • 9
  • Do you have some error,when you run the code? – Abdelhak Nov 28 '15 at 08:03
  • no errors.In client i am debugging i found at the time of raising the exception the login service is not calling.If username and password is correct the service is 200 ok i am getting . – Madan Mohan Nov 28 '15 at 08:05

1 Answers1

0

yes You can send Response with Exception Code. You code is ok Just you have to return the response from catch block. i have tried to modify your code.

@POST
@Path("/login")
@Consumes(MediaType.APPLICATION_JSON)
@Produces(MediaType.APPLICATION_JSON)
public Response login(Member member) {
    Response.ResponseBuilder builder = null;
    try {
        LogMsg.info("Login User " + member.getUsername() + " Password : "+ member.getPassword());
        member.setPassword(member.getPassword());
        repository.validateLogin(member);
         builder = Response.ok();
    } catch (ConstraintViolationException ce) {
        ErrorMessages errorMessage = new ErrorMessages(ce.getMessage(), 409, "username is wrong");  
        LogMsg.error("ConstraintViolationException  " + ce.getMessage());

        return Response.status(409).entity(errorMessage).type(MediaType.APPLICATION_JSON).build();                      
    } catch (ValidationException e) {
        Map<String, String> responseObj = new HashMap<String, String>();
        responseObj.put("username", "Invalid Username");
        ErrorMessages errorMessage = new ErrorMessages(e.getMessage(), 404, "password is wrong");   
        LogMsg.error("ValidationException  " + e.getMessage());

        return Response.status(404).entity(errorMessage).type(MediaType.APPLICATION_JSON).build();                                              
    } catch (Exception e) {
        Map<String, String> responseObj = new HashMap<String, String>();
        responseObj.put("error", e.getMessage());
        LogMsg.error(e.getMessage());

        return Response.status(400).entity(errorMessage).type(MediaType.APPLICATION_JSON).build();                                              
    }
    return builder.build();
}