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.
package com.javatute.controller; @RestController @RequestMapping(value = "/student") public class StudentController { @Autowired private StudentService studentService; @RequestMapping(value = "/{id}", method = RequestMethod.GET) @ResponseBody public List<Student> findStudents(@PathVariable String id) { List<Student> studentResponse = (List<Student>) studentService.findStudents(id); return studentResponse; } @RequestMapping(value = "/{id}", method = RequestMethod.GET) @ResponseBody public List<Student> findStudentsUsingNativeQuery(@PathVariable String id) { List<Student> studentResponse = (List<Student>) studentService.findStudentsUsingNativeQuery(id); return studentResponse; } }
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.javatute.entity.Student> com.javatute.controller.StudentController.findStudentsUsingNativeQuery(java.lang.String)
to {[/student/findStudents/{name}],methods=[GET]}: There is already ‘studentController’ bean method
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.
package com.javatute.controller; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.web.bind.annotation.PathVariable; import org.springframework.web.bind.annotation.RequestBody; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RequestMethod; import org.springframework.web.bind.annotation.ResponseBody; import org.springframework.web.bind.annotation.RestController; import com.javatute.entity.Student; import com.javatute.service.StudentService; @RestController @RequestMapping(value = "/student") public class StudentController { @Autowired private StudentService studentService; @RequestMapping(value = "/update", method = RequestMethod.POST) @ResponseBody public Student saveStudent(@RequestBody Student student) { Student studentResponse = (Student) studentService.saveStudent(student); return studentResponse; } @RequestMapping(value = "/update", method = RequestMethod.PUT) @ResponseBody public Student updateStudent(@RequestBody Student student) { Student studentResponse = (Student) studentService.updateStudent(student); return studentResponse; } @RequestMapping(value = "/update", method = RequestMethod.DELETE) @ResponseBody public Student deleteStudent(@RequestBody Student student) { Student studentResponse = (Student) studentService.deleteStudent(student); return studentResponse; } @RequestMapping(value = "/find/{id}", method = RequestMethod.GET) @ResponseBody public Student getStudent(@PathVariable int id) { Student studentResponse = (Student) studentService.getStudent(id); return studentResponse; } @RequestMapping(value = "/find/{id}", method = RequestMethod.PUT) @ResponseBody public Student getStudent1(@PathVariable int id) { Student studentResponse = (Student) studentService.getStudent(id); return studentResponse; } @RequestMapping(value = "/find/{id}", method = RequestMethod.DELETE) @ResponseBody public Student getStudent2(@PathVariable int id) { Student studentResponse = (Student) studentService.getStudent(id); return studentResponse; } }
That’s all about Define multiple Rest API with the same name.
You may like.
- @RestController and @Controller annotation example in Spring Boot.
- @RequestMapping annotation example In Spring Boot.
- @RequestBody and @ResponseBody annotation example in Spring Boot.
- @PathVariable and @RequestParam annotations in Spring Boot.
- @SpringBootApplication annotation example in Spring Boot.
- @Component, @Controller, @Service and @Repository annotations example using Spring Boot.
- @Configuration annotation example using spring boot.
- @ComponentScan example in spring boot.
- Spring transaction management basic.
- Spring transaction management example using spring boot.
- @Transactional REQUIRED vs REQUIRES_NEW example in spring boot.
- What is spring data JPA and what are the benefits.
- Spring Data JPA example using spring boot.
- Spring boot datasource configuration using tomcat.
- Deploy Spring boot war in JBoss EAP server.
- Jboss 7 EPA datasource configuration using oracle and spring boot.
- Spring Boot interceptor example.
- Filter example in Spring Boot.
Spring Data JPA example.
- Spring Data JPA greater than Example
- Spring Data JPA less than Example
- Spring Data JPA IsNull Example Using Spring Boot
- Spring Data findById() Vs getOne()
- Spring Data JPA CrudRepository findById()
- Spring Data JPA JpaRepository getOne()
- Spring Data CrudRepository saveAll() and findAll().
- Spring Data CrudRepository existsById()
- Spring Data JPA delete() vs deleteInBatch()
- Spring Data JPA deleteAll() Vs deleteAllInBatch()
- Spring Data JPA JpaRepository deleteAllInBatch()
- Spring Data JPA deleteInBatch() Example
- Spring Data JPA JpaRepository saveAndFlush() Example
- Spring Data JPA CrudRepository count() Example
- Spring Data JPA CrudRepository delete() and deleteAll()
- Spring Data JPA CrudRepository deleteById() Example
- CrudRepository findAllById() Example Using Spring Boot
- Spring Data CrudRepository save() Method.
- Sorting in Spring Data JPA using Spring Boot.
- Spring Data JPA example using spring boot.
- Spring Data JPA and its benefit.
spring rest docs.
Summary – We can define two Rest API with the same name but HTTP request must be different.