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.

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

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.

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.

Spring Data JPA example.

 

 

spring rest docs.

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