Spring Boot Restful POST被认为是GET问题

时间:2016-12-01 10:25:30

标签: rest post spring-boot postman

我在服务器端使用spring boot restful。我正在使用Tomcat 8.0进行开发

AppConfig.java

@SpringBootApplication
@ComponentScan({"com.my.project, package com.my.project.db"})
public class Application extends SpringBootServletInitializer {

private static Logger log = Logger.getLogger(Application.class);
private static Class<Application> applicationClass = Application.class;

public static void main(String[] args) {
    SpringApplication.run(applicationClass, args);     
    log.info("Application Start");
}

@Override
protected SpringApplicationBuilder configure(SpringApplicationBuilder application) {
    return application.sources(applicationClass);
}

@Override
public void onStartup(ServletContext servletContext) throws ServletException {
    super.onStartup(servletContext);
    servletContext.getServletRegistration(DispatcherServletAutoConfiguration.DEFAULT_DISPATCHER_SERVLET_BEAN_NAME)
            .setInitParameter("dispatchOptionsRequest", "true");
}

Login.java

@CrossOrigin("*") 
@RestController
public class LoginController {

@Autowired
private UserService userService;

private static Logger log = Logger.getLogger(LoginController.class);

@RequestMapping(value = "/login", method = RequestMethod.POST)
public ResponseEntity<LoginInfo> doLogin(@RequestBody LoginInfo loginInfo) {

    HttpStatus status = HttpStatus.OK;

    if (loginInfo.getLogin() == null || loginInfo.getPassword() == null) {
        // error;
        status = HttpStatus.BAD_REQUEST;
        log.error("login or password is empty.");
    }

    return RestAPIUtils.responseEntity(loginInfo, "/login", status);    
}

我正在使用Postman进行测试。 对于Input,我选择“POST”方法,然后正确输入url 标题:{Content-Type:'application / json'},Body如下:

{
    login: "myname",
    password: "password"
}

然后,我收到如下错误:

{
  "timestamp": 1480585504124,
  "status": 405,
  "error": "Method Not Allowed",
  "exception": "org.springframework.web.HttpRequestMethodNotSupportedException",
  "message": "Request method 'GET' not supported",
  "path": "/myProject/login/"
}

我尝试使用Type来“PUT”和“DELETE”,并且错误确实改为'PUT'或'DELETE'。只有POST被识别为'GET'???

我甚至尝试将服务器端和邮差改为'PUT',我用Http Code 200得到了预期的结果,OK:

{
    login: "myname",
    password: "password",
    status: "success"
}

POST功能有什么问题,有人可以帮帮我吗? 非常感谢!

0 个答案:

没有答案