URL未连接到我的spring启动应用程序

时间:2018-01-18 19:45:44

标签: java spring spring-mvc spring-boot

设置一个新的弹簧启动应用程序,总是让我觉得我必须正确地抓住我的嘴才能让它第一次运行。

我的角色应用程序正在发帖,我知道它正在击中我的控制器,因为当我做了一次获取时,我收到一条错误,说不允许获取/登录。

这是我在我的控制器中的方法,我试图从我的Angular应用程序调用。

@RequestMapping(value = "/login", method = RequestMethod.POST)
@ResponseBody
public String login(@RequestParam(NetworkKeyNames.KEY_PAYLOAD) String payload) throws IOException {
    log.error("Payload=" + payload);
    TypeReference<Map<String, String>> typeRef = new TypeReference<Map<String, String>>() {};
    Map<String, String> payloadMap = objectMapper.readValue(payload, typeRef);
    String username = payloadMap.get(NetworkKeyNames.KEY_USERNAME);
    String password = payloadMap.get(NetworkKeyNames.KEY_PASSWORD);

    String result = securityService.login(username, password);
    return result;
}

当我点击它时,这是控制台中的调试信息:

2018-01-18 14:33:24.865 DEBUG 27096 --- [qtp779343339-17] o.s.b.w.f.OrderedRequestContextFilter    : Bound request context to thread: Request(OPTIONS //localhost:8093/login)@6bc51cc8
2018-01-18 14:33:24.866 DEBUG 27096 --- [qtp779343339-17] o.s.web.servlet.DispatcherServlet        : DispatcherServlet with name 'dispatcherServlet' processing OPTIONS request for [/login]
2018-01-18 14:33:24.866 DEBUG 27096 --- [qtp779343339-17] o.s.b.a.e.mvc.EndpointHandlerMapping     : Looking up handler method for path /login
2018-01-18 14:33:24.869 DEBUG 27096 --- [qtp779343339-17] o.s.b.a.e.mvc.EndpointHandlerMapping     : Did not find handler method for [/login]
2018-01-18 14:33:24.869 DEBUG 27096 --- [qtp779343339-17] s.w.s.m.m.a.RequestMappingHandlerMapping : Looking up handler method for path /login
2018-01-18 14:33:24.869 DEBUG 27096 --- [qtp779343339-17] s.w.s.m.m.a.RequestMappingHandlerMapping : Returning handler method [public java.lang.String pizzainthecloud.pizzaplace.controller.PizzaPlaceController.login(java.lang.String) throws java.io.IOException]
2018-01-18 14:33:24.869 DEBUG 27096 --- [qtp779343339-17] o.s.b.f.s.DefaultListableBeanFactory     : Returning cached instance of singleton bean 'pizzaPlaceController'
2018-01-18 14:33:24.870 DEBUG 27096 --- [qtp779343339-17] o.j.s.OpenEntityManagerInViewInterceptor : Opening JPA EntityManager in OpenEntityManagerInViewInterceptor
2018-01-18 14:33:24.871 DEBUG 27096 --- [qtp779343339-17] o.s.web.servlet.DispatcherServlet        : Null ModelAndView returned to DispatcherServlet with name 'dispatcherServlet': assuming HandlerAdapter completed request handling
2018-01-18 14:33:24.871 DEBUG 27096 --- [qtp779343339-17] o.j.s.OpenEntityManagerInViewInterceptor : Closing JPA EntityManager in OpenEntityManagerInViewInterceptor
2018-01-18 14:33:24.871 DEBUG 27096 --- [qtp779343339-17] o.s.orm.jpa.EntityManagerFactoryUtils    : Closing JPA EntityManager
2018-01-18 14:33:24.871 DEBUG 27096 --- [qtp779343339-17] o.s.web.servlet.DispatcherServlet        : Successfully completed request
2018-01-18 14:33:24.872 DEBUG 27096 --- [qtp779343339-17] o.s.b.w.f.OrderedRequestContextFilter    : Cleared thread-bound request context: Request(OPTIONS //localhost:8093/login)@6bc51cc8

所以它看起来好像是映射到我的方法,但第一行中的日志永远不会被调用,我的断点永远不会被击中。

我错过了什么?

1 个答案:

答案 0 :(得分:1)

调用您的REST服务的客户端已启用CORS,因此它正在发送初始OPTIONS调用,而不是为您的控制器配置的POST调用,正如您在发布的日志语句的第一行中所看到的那样。

  

(OPTIONS // localhost:8093 / login)@ 6bc51cc8

您需要禁用CORS,或者向REST API添加CORS支持,或者将Apache放在应用程序前面,以转发UI和被调用服务的请求来自同一个来源(域名),以便CORS检查请求未被触发。