LoginController.java
package com.harmathuwebLogin;
import java.util.GregorianCalendar;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpEntity;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpStatus;
import org.springframework.http.MediaType;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.ResponseStatus;
@Controller("/app")
public class LoginController implements LoginService{
@Autowired
private SessionRepository sessionRepository;
@Autowired
UsersRepository userRepository;
@Override
@RequestMapping(value = "/app/login", method = RequestMethod.POST, consumes = "application/json", produces = MediaType.APPLICATION_JSON_VALUE)
@ResponseStatus (value = HttpStatus.ACCEPTED)
@ResponseBody
public HttpEntity<String> login(@RequestBody Users user) {
Users user_res = userRepository.findByUserNameAndPassWord(
user.getUserName(), user.getPassWord());
if (user_res != null) {
Sessions new_session = new Sessions(user_res,
GregorianCalendar.getInstance(),
GregorianCalendar.getInstance());
sessionRepository.save(new_session);
String body = "{\"sessionId\" : \"" + new_session.getSessionId()
+ "\", \"firstName\" : \"" + user_res.getFirstName()
+ "\", \"lastName\" : \"" + user_res.getLastName() + "\"}";
HttpHeaders headers = new HttpHeaders();
headers.setContentType(MediaType.APPLICATION_JSON);
return new HttpEntity<String>(body, headers);
} else {
throw new HttpUnauthorizedException();
}
}
}
I am testing my REST Api with https://addons.mozilla.org/en-US/firefox/addon/restclient/
Using client I am trying to do like this :
Method : POST,
URL : http://localhost:8080/app/login
Body : { "userName": "harmathu", "passWord": "blahblah" }
Headers : Accept : "application/json", Content-Type : "aplication/json"
This is the output :
Status Code: 406 Not Acceptable
Date: Fri, 15 Aug 2014 06:51:20 GMT
Server: Apache-Coyote/1.1
Transfer-Encoding: chunked
access-control-allow-headers: x-requested-with
access-control-allow-methods: POST, GET, OPTIONS, DELETE
access-control-allow-origin: *
access-control-max-age: 3600
Even If I remove @ResponseBody from Controller, I get the same error. What I know about Error 406 means rest is sending something which rest client does not expect or understand.
Apparantely this also gives the same error :
@Override
@RequestMapping(value = "/app/login", method = RequestMethod.POST, consumes = "application/json", produces = "application/json")
@ResponseStatus(value = HttpStatus.ACCEPTED)
@ResponseBody
public String login(@RequestBody Users user) {
Users user_res = userRepository.findByUserNameAndPassWord(
user.getUserName(), user.getPassWord());
if (user_res != null) {
Sessions new_session = new Sessions(user_res,
GregorianCalendar.getInstance(),
GregorianCalendar.getInstance());
sessionRepository.save(new_session);
String body = "{\"sessionId\" : \"" + new_session.getSessionId()
+ "\",\"firstName\" : \"" + user_res.getFirstName()
+ "\",\"lastName\" : \"" + user_res.getLastName() + "\"}";
return body;
} else {
throw new HttpUnauthorizedException();
}
}