1

I am working with Spring Cloud. I have four services in my spring boot application college-service, student-service, eureka-server and api-gateway. I am trying to call college-service and student-service with API Gateway. While I'm calling My college-service from API Gateway that's work fine but student-service is not working. While I am trying to get student-serivice, I get error 404 not found. I'm new guy in Spring Cloud. Here down is my code.

Here down is my urls, Where in last url I get 404

URL Status
http://localhost:9001/college/CLG01 200 OK
http://localhost:9002/college/student/CLG01 200 OK
http://localhost:9003/college/CLG01 200 OK
http://localhost:9003/college/student/CLG01 400 Not Found

Colleger Service

Entity

public class College { private String clgId; private String clgName; private String clgCity; List<Student> students; public College(String clgId, String clgName, String clgCity, List<Student> students) { this.clgId = clgId; this.clgName = clgName; this.clgCity = clgCity; this.students = students; } public College(String clgId, String clgName, String clgCity) { this.clgId = clgId; this.clgName = clgName; this.clgCity = clgCity; } // getter and setter } public class Student { private Long stId; private String stName; private String stEmail; private String clgId; public Student(Long stId, String stName, String stEmail, String clgId) { super(); this.stId = stId; this.stName = stName; this.stEmail = stEmail; this.clgId = clgId; } // getter and setter } 

College Controller

@RestController @RequestMapping("/college") public class CollegeController { @Autowired private CollegeService collegeService; @Autowired private RestTemplate restTemplate; @RequestMapping(value = "/{clgId}", method = RequestMethod.GET) public ResponseEntity<College> getColleges(@PathVariable("clgId") String clgId) { College college = collegeService.getCollege(clgId); List student = restTemplate.getForObject("http://student-service/college/student/" + college.getClgId(), List.class); college.setStudents(student); return ResponseEntity.ok(college); } } 

application.yml

server: port: 9001 spring: application: name: college-service eureka: instance: hostname: localhost 

Student Service

Student Controller Entity

public class Student { private Long stId; private String stName; private String stEmail; private String clgId; public Student(Long stId, String stName, String stEmail, String clgId) { super(); this.stId = stId; this.stName = stName; this.stEmail = stEmail; this.clgId = clgId; } // getter and setter } 
@RestController @RequestMapping("/college") public class StudentCotroller { @Autowired private StudentService studentService; @RequestMapping(value = "/student/{clgId}", method = RequestMethod.GET) public ResponseEntity<List<Student>> getStudents(@PathVariable("clgId") String clgId) { return ResponseEntity.ok(studentService.getStudents(clgId)); } } 

application.yml

server: port: 9002 spring: application: name: student-service eureka: instance: hostname: localhost 

Eureka Server

application.yml

server: port: 8761 eureka: instance: hostname: localhost client: registerWithEureka: false fetchRegistry: false server: waitTimeInMsWhenSyncEmpty: 0 

API Gateway Service

application.yml

server: port: 9003 eureka: instance: hostname: localhost spring: application: name: api-gateway cloud: gateway: routes: - id: college-service uri: lb://college-service predicates: - Path=/college/** - id: student-service uri: lb://student-service predicates: - Path=/student/** 
2
  • /college/student/CLG01 will always match the college service, not the student service Commented Aug 15, 2022 at 15:03
  • @spencergibb What I want to do for match this url for college-service Commented Aug 15, 2022 at 18:03

3 Answers 3

5

I had the same problem. After trying a lot things, I found thaat the issue was a dependency problem in the API Gateway.

This is bad:

<dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-gateway-mvc</artifactId> <version>4.1.0</version> </dependency> 

This is correctly:

<dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-gateway</artifactId> <version>4.1.0</version> </dependency> 

Note don't had the last one dosen't have the "-mvc".

Sign up to request clarification or add additional context in comments.

Comments

2

Predicate for your student-service is to match all the requests that start with student(Path=/student/**). However you are you calling the student-service with the request that starts with college(/college/student/CLG01). This request will me matched to college-service since you set the predicate for this service as Path=/college/**. /college/** path matches /college/student/CLG01 path.

Possible solutions:

  1. Change your StudentController request mapping from /college to /student
  2. Use different predicate for your student service, such as Host
  3. Set specific path predicate and change the order of routes:
 routes: - id: student-service uri: lb://student-service predicates: - Path=/college/student/** - id: college-service uri: lb://college-service predicates: - Path=/college/** 

3 Comments

Just wanted one clarification if from college controller i remove the request mapping at class level i.e. /college and in spring route definition for college service i keep the predicate path the same as /college/** then also the requests relative to path /college will be directed to college ms .For ex localhost:9003/college/1 will be directed to localhost:9001/1
@-Elyorbek could you please provide your comments on above query ?
Could someone please comments on the above observation
2

In your build.gradle file change the dependency from

implementation 'org.springframework.cloud:spring-cloud-starter-gateway-mvc' 

to

implementation 'org.springframework.cloud:spring-cloud-starter-gateway' 

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.