给定Spring Rest应用程序中406不可接受的原因是什么?

时间:2014-08-15 07:01:37

标签: json spring-mvc

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();
        }
    }
}

我正在使用https://addons.mozilla.org/en-US/firefox/addon/restclient/

测试我的REST Api

使用客户端我试图这样做:

方法:POST,

URL : http://localhost:8080/app/login

身体: {     “userName”:“harmathu”,     “passWord”:“blahblah” }

标题: 接受:“application / json”, 内容类型:“aplication / json”

这是输出:

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

即使我从Controller中删除了@ResponseBody,我也会遇到同样的错误。 我对Error 406的了解意味着休息正在发送其他客户不期望或不理解的东西。

同样,这也会产生同样的错误:

    @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();
        }
    }

1 个答案:

答案 0 :(得分:0)

问题在于基础设施。当我将项目移动到linux机器时,问题就解决了。