0

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();
        }
    }
a better oliver
  • 26,330
  • 2
  • 58
  • 66
Harsh M
  • 625
  • 2
  • 11
  • 25
  • possible duplicate of [Spring-MVC 406 Not Acceptable instead of JSON Response](http://stackoverflow.com/questions/7473498/spring-mvc-406-not-acceptable-instead-of-json-response) – skaffman Aug 15 '14 at 07:22
  • Hi, I saw the question, but in don't think both are the same... I don't have any Jackson here – Harsh M Aug 15 '14 at 11:02
  • How do you expect the conversion from the String payload to the `Users` type to happen if no Jackson is involved? – Oliver Drotbohm Aug 15 '14 at 16:18
  • I mean Jackson is involved, but I am not configuring anything, I am using @AutoConfiguration. The case is one class users and a class sessions with userid as foreign key to users. One Repository for user and one for sessions class and this controller. I have no xml configuration, Comment if full code I need to upload here. – Harsh M Aug 15 '14 at 18:23
  • @OliverGierke, Hi, In this code, I am using autoconfiguration, I have not configured Jackson manually. Still I am getting this error. If you please look through this. Thanks again for your time in advance. – Harsh M Aug 17 '14 at 14:22
  • Did you add Jackson as a dependency? – alfredaday Aug 17 '14 at 14:58

1 Answers1

0

The problem was with infrastructure. The problem was solved when I moved my project to linux machine.

Harsh M
  • 625
  • 2
  • 11
  • 25