I am new to Dropwizard and am having a little trouble with the conversion from POJO to Json. I am using jQuery at the frontend where I am calling the ajax function with the type POST. I am trying to validate a login against data in a MySQL database. I know the database is connected and the correct user can be located in the database based on the credentials entered on the HTML login page.
Here is the backend code:
@POST
@UnitOfWork
@Path("/login")
@Produces("application/json")
public User login(String credentials) {
try {
ObjectMapper mapper = new ObjectMapper();
JsonNode root = mapper.readValue(credentials, JsonNode.class);
String username = root.get("Username").toString().replaceAll("\"", "");
String password = root.get("Password").toString().replaceAll("\"", "");
User user = userDAO.findByUsername(username);
//Check password matches and respond
boolean passwordMatched = user.isPasswordMatched(password);
//Return correct response
if (passwordMatched) {
return user;
}
}
catch (Exception e) {
System.out.println(e.getMessage());
}
return null;
}
Then at the front end I simply post the username and password to the url:
$(document).ready(function() {
$("#login").click(function() {
var username = $("#username").val();
var password = $("#password").val();
$.ajax({
url:'http://localhost:8080/User/login',
type: 'POST',
data: JSON.stringify({"Username" : username, "Password" : password}),
complete: function(data, status) {
alert("Status: " + status + "Data: " + JSON.stringify(data));
}
});
The console output from the server is HTTP code 200 and when I set a breakpoint in the login function I can see the correct database entry being found and being returned from function. However, the alert that is triggered on complete from the jQuery code prints an error.
The data returned from my server is valid JSon according to JsonLint. So it looks like ajax is not receiving the data which is strange because it sends the original login details successfully. Can anyone help with this?