Spring启动+云| Zuul 代理 | 404错误
Spring Boot + Cloud | Zuul Proxy | 404 Error
我正在使用 Spring Cloud 和 Zuul 代理作为我的 RESTful 服务的网关。
网关应用程序(Spring 在端口 8080 上启动 Web 应用程序 运行)相关代码:-
主要class:-
@SpringBootApplication
@EnableZuulProxy
public class WebfrontApplication extends WebMvcConfigurerAdapter {
/**
* @param args
*/
public static void main(String[] args) {
SpringApplication.run(WebfrontApplication.class, args);
}
}
Zuul 映射:-
zuul:
routes:
customer:
path: /customer/**
url: http://localhost:9000/
在上述 UI 网关应用程序启动期间,我可以在我的日志中看到代理映射已注册:-
o.s.c.n.zuul.web.ZuulHandlerMapping : Mapped URL path [/customer/**] onto handler of type [class org.springframework.cloud.netflix.zuul.web.ZuulController]
REST服务(A Spring Boot web app 运行 on port 9000)相关代码:-
@RestController
@RequestMapping(value = "/customer")
public class CustomerController {
@Autowired
private CustomerService customerService;
/**
* Get List of All customers.
*
* @return
*/
@RequestMapping(value = "/list", method = RequestMethod.GET)
public List<Customer> list(Principal principal) {
return customerService.list();
}
}
使用休息客户端(在我的例子中是 POSTMAN)我能够成功地从上面的端点获得响应(在处理授权令牌之后)。
我在 UI 应用程序中使用 AngularJS 从 REST 端点获取数据。
相关代码:-
angular.module('customerModule').factory('customerService',function($http) {
return {
customerList : function(){
// PROBLEM !!!!!
// THIS CALL GIVES A 404 ERROR
return $http.get('/customer/list');
}
};
});
以上调用返回 404 错误:-
这是我的 Chrome 调试器显示的内容:-
Remote Address:127.0.0.1:8080
Request URL:http://localhost:8080/customer/list
Request Method:GET
Status Code:404 Not Found
请求Headers:-
Accept:application/json, text/plain, */*
Accept-Encoding:gzip, deflate, sdch
Accept-Language:en-US,en;q=0.8,hi;q=0.6,ms;q=0.4
Cache-Control:no-cache
Connection:keep-alive
Cookie:SESSION=2e1ac330-fe41-4ff4-8efb-81eaec12c8d1
Host:localhost:8080
Pragma:no-cache
Referer:http://localhost:8080/
User-Agent:Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/41.0.2272.89 Safari/537.36
X-Auth-Token:2e1ac330-fe41-4ff4-8efb-81eaec12c8d1
响应Headers:-
Cache-Control:no-cache, no-store, max-age=0, must-revalidate
Content-Type:application/json; charset=UTF-8
Date:Fri, 20 Mar 2015 04:00:36 GMT
Date:Fri, 20 Mar 2015 04:00:36 GMT
Expires:0
Pragma:no-cache
Pragma:no-cache
Server:Jetty(9.2.9.v20150224)
Transfer-Encoding:chunked
X-Application-Context:bootstrap
X-Content-Type-Options:nosniff
X-Content-Type-Options:nosniff
X-Frame-Options:DENY
X-Frame-Options:DENY
X-XSS-Protection:1; mode=block
X-XSS-Protection:1; mode=block
这里有什么问题吗?映射不正确还是我遗漏了什么?
已解决
根据接受的答案,将 Zuul 映射更改为:-
zuul:
routes:
resource:
path: /customer/**
url: http://localhost:9000/
stripPrefix: false
资源服务器上没有“/list”的映射(所以是 404)。您需要在路由声明中设置 stripPrefix=false,或者将后端的请求映射更改为“/”。
如果不想设置stripPrefix
属性也可以尝试以下配置:
zuul:
routes:
customer:
path: /customer/**
url: http://localhost:9000/customer
我正在使用 Spring Cloud 和 Zuul 代理作为我的 RESTful 服务的网关。
网关应用程序(Spring 在端口 8080 上启动 Web 应用程序 运行)相关代码:-
主要class:-
@SpringBootApplication
@EnableZuulProxy
public class WebfrontApplication extends WebMvcConfigurerAdapter {
/**
* @param args
*/
public static void main(String[] args) {
SpringApplication.run(WebfrontApplication.class, args);
}
}
Zuul 映射:-
zuul:
routes:
customer:
path: /customer/**
url: http://localhost:9000/
在上述 UI 网关应用程序启动期间,我可以在我的日志中看到代理映射已注册:-
o.s.c.n.zuul.web.ZuulHandlerMapping : Mapped URL path [/customer/**] onto handler of type [class org.springframework.cloud.netflix.zuul.web.ZuulController]
REST服务(A Spring Boot web app 运行 on port 9000)相关代码:-
@RestController
@RequestMapping(value = "/customer")
public class CustomerController {
@Autowired
private CustomerService customerService;
/**
* Get List of All customers.
*
* @return
*/
@RequestMapping(value = "/list", method = RequestMethod.GET)
public List<Customer> list(Principal principal) {
return customerService.list();
}
}
使用休息客户端(在我的例子中是 POSTMAN)我能够成功地从上面的端点获得响应(在处理授权令牌之后)。
我在 UI 应用程序中使用 AngularJS 从 REST 端点获取数据。
相关代码:-
angular.module('customerModule').factory('customerService',function($http) {
return {
customerList : function(){
// PROBLEM !!!!!
// THIS CALL GIVES A 404 ERROR
return $http.get('/customer/list');
}
};
});
以上调用返回 404 错误:- 这是我的 Chrome 调试器显示的内容:-
Remote Address:127.0.0.1:8080
Request URL:http://localhost:8080/customer/list
Request Method:GET
Status Code:404 Not Found
请求Headers:-
Accept:application/json, text/plain, */*
Accept-Encoding:gzip, deflate, sdch
Accept-Language:en-US,en;q=0.8,hi;q=0.6,ms;q=0.4
Cache-Control:no-cache
Connection:keep-alive
Cookie:SESSION=2e1ac330-fe41-4ff4-8efb-81eaec12c8d1
Host:localhost:8080
Pragma:no-cache
Referer:http://localhost:8080/
User-Agent:Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/41.0.2272.89 Safari/537.36
X-Auth-Token:2e1ac330-fe41-4ff4-8efb-81eaec12c8d1
响应Headers:-
Cache-Control:no-cache, no-store, max-age=0, must-revalidate
Content-Type:application/json; charset=UTF-8
Date:Fri, 20 Mar 2015 04:00:36 GMT
Date:Fri, 20 Mar 2015 04:00:36 GMT
Expires:0
Pragma:no-cache
Pragma:no-cache
Server:Jetty(9.2.9.v20150224)
Transfer-Encoding:chunked
X-Application-Context:bootstrap
X-Content-Type-Options:nosniff
X-Content-Type-Options:nosniff
X-Frame-Options:DENY
X-Frame-Options:DENY
X-XSS-Protection:1; mode=block
X-XSS-Protection:1; mode=block
这里有什么问题吗?映射不正确还是我遗漏了什么?
已解决
根据接受的答案,将 Zuul 映射更改为:-
zuul:
routes:
resource:
path: /customer/**
url: http://localhost:9000/
stripPrefix: false
资源服务器上没有“/list”的映射(所以是 404)。您需要在路由声明中设置 stripPrefix=false,或者将后端的请求映射更改为“/”。
如果不想设置stripPrefix
属性也可以尝试以下配置:
zuul:
routes:
customer:
path: /customer/**
url: http://localhost:9000/customer