Define multiple Rest API with the same name

In this post, we will see we can define multiple Rest API with the same name or not?

We can define multiple Rest API with the same name but HTTP request must be different. For example, we can’t define below API in controller class it will throw an exception. Here We have the same API name and also the same HTTP request method.

GET – localhost:9091/student/{id}
GET – localhost:9091/student/{id}

Let’s see below code snippet.

In the above code snippet, we are defining two API with the same name and also we have same HTTP method i.e GET. It will throw java.lang.IllegalStateException as below.

Caused by: java.lang.IllegalStateException: Ambiguous mapping. Cannot map ‘studentController’ method
public java.util.List<com.netsurfingzone.entity.Student> com.netsurfingzone.controller.StudentController.findStudentsUsingNativeQuery(java.lang.String)
to {[/student/findStudents/{name}],methods=[GET]}: There is already ‘studentController’ bean method

multiple Rest API with the same name

 

 

But we can multiple REST API with the same name as below.

GET – localhost:9091/student/{id}
PUT- localhost:9091/student/{id}
DELETE- localhost:9091/student/{id}

or

POST – http://localhost:9091/student/update
PUT – http://localhost:9091/student/update
DELETE – http://localhost:9091/student/update

ALL are valid APIs. Lets’s observe below controller class.

 

That’s all about Define multiple Rest API with the same name.

You may like.

Spring Data JPA example.

 

 

spring rest docs.

Summary – We can define two Rest API with the same name but HTTP request must be different.

Top