11

I am using Spring Cloud and Zuul proxy as gateway to my RESTful service.

Gateway Application (Spring Boot web app running on port 8080) relevant code:-

Main class:-

@SpringBootApplication
@EnableZuulProxy
public class WebfrontApplication extends WebMvcConfigurerAdapter {

    /**
     * @param args
     */
    public static void main(String[] args) {
        SpringApplication.run(WebfrontApplication.class, args);
    }
}

Zuul Mappings:-

zuul:
  routes:
    customer:
      path: /customer/**
      url: http://localhost:9000/

During startup of above UI Gateway application, I can see in my logs that mappings for proxies are registered:-

o.s.c.n.zuul.web.ZuulHandlerMapping      : Mapped URL path [/customer/**] onto handler of type [class org.springframework.cloud.netflix.zuul.web.ZuulController]

The REST service (A Spring Boot web app running on port 9000) relevant code:-

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

Using a rest client (POSTMAN in my case) I am able to get response from above endpoint successfully (after taking care of auth token).

I am using AngularJS for in UI application to get data from the REST endpoint.

Relevant code:-

angular.module('customerModule').factory('customerService',function($http) {

    return {
        customerList : function(){
            // PROBLEM !!!!!
            // THIS CALL GIVES A 404 ERROR
            return $http.get('/customer/list');
        }
    };
});

The above call is giving back a 404 error:- This is what my Chrome debugger shows:-

Remote Address:127.0.0.1:8080
Request URL:http://localhost:8080/customer/list
Request Method:GET
Status Code:404 Not Found

Request 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

Response 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

What's is wrong here ? Are mappings incorrect or am I missing something else?

SOLVED

As per the accepted answer, changing the Zuul mapping worked when changed to:-

zuul:
 routes:
  resource:
   path: /customer/**
   url: http://localhost:9000/
   stripPrefix: false
Nicholas K
  • 15,148
  • 7
  • 31
  • 57
Kumar Sambhav
  • 7,503
  • 15
  • 63
  • 86
  • I am following Dave Syer's blog from here: http://spring.io/blog/2015/01/12/spring-and-angular-js-a-secure-single-page-application – Kumar Sambhav Mar 20 '15 at 04:32

2 Answers2

14

There is no mapping for "/list" on the resource server (so it's a 404). You either need to set stripPrefix=false on your route declaration, or change the request mapping on the backend to "/".

Dave Syer
  • 56,583
  • 10
  • 155
  • 143
  • I'm having a similar problem. Only in my case, it gives me a 405(Not Allowed) when `POST`ing, but `GET` works fine. Could you help with this? – cst1992 Apr 14 '16 at 11:07
-1

You can also try the following configuration if you do not wish to set the stripPrefix property:

zuul:
  routes:
    customer:
      path: /customer/**
      url: http://localhost:9000/customer
Laurel
  • 5,965
  • 14
  • 31
  • 57
akshaya pandey
  • 997
  • 6
  • 16