Skip to content

Commit 2f01b43

Browse files
azrairshadEugen
authored andcommitted
BAEL-1547: Request method not supported - 405, examples. (eugenp#5464)
1 parent 90978bf commit 2f01b43

4 files changed

Lines changed: 90 additions & 0 deletions

File tree

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
package com.baeldung.spring.controller;
2+
3+
import com.baeldung.spring.domain.Employee;
4+
import com.baeldung.spring.exception.InvalidRequestException;
5+
import com.baeldung.spring.service.EmployeeService;
6+
import org.springframework.beans.factory.annotation.Autowired;
7+
import org.springframework.web.bind.annotation.RequestMapping;
8+
import org.springframework.web.bind.annotation.RequestMethod;
9+
import org.springframework.web.bind.annotation.RestController;
10+
11+
import java.util.List;
12+
13+
@RestController
14+
@RequestMapping(value="/api")
15+
public class RequestMethodController {
16+
17+
@Autowired
18+
EmployeeService service;
19+
20+
@RequestMapping(value = "/employees", produces = "application/json", method={RequestMethod.GET,RequestMethod.POST})
21+
public List<Employee> findEmployees()
22+
throws InvalidRequestException {
23+
return service.getEmployeeList();
24+
}
25+
}
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
package com.baeldung.spring.exception;
2+
3+
public class InvalidRequestException extends RuntimeException {
4+
5+
/**
6+
*
7+
*/
8+
private static final long serialVersionUID = 4088649120307193208L;
9+
10+
public InvalidRequestException() {
11+
super();
12+
}
13+
14+
public InvalidRequestException(final String message, final Throwable cause) {
15+
super(message, cause);
16+
}
17+
18+
public InvalidRequestException(final String message) {
19+
super(message);
20+
}
21+
22+
public InvalidRequestException(final Throwable cause) {
23+
super(cause);
24+
}
25+
26+
}
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
package com.baeldung.spring.service;
2+
3+
import com.baeldung.spring.domain.Employee;
4+
import org.springframework.stereotype.Service;
5+
6+
import java.util.List;
7+
8+
@Service
9+
public interface EmployeeService {
10+
11+
List<Employee> getEmployeeList();
12+
}
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
package com.baeldung.spring.service;
2+
3+
import com.baeldung.spring.domain.Employee;
4+
import org.springframework.stereotype.Service;
5+
6+
import java.util.ArrayList;
7+
import java.util.List;
8+
9+
@Service
10+
public class EmployeeServiceImpl implements EmployeeService{
11+
12+
@Override
13+
public List<Employee> getEmployeeList() {
14+
List<Employee> employeeList = new ArrayList<>();
15+
employeeList.add(createEmployee(100L, "Steve Martin", "333-777-999"));
16+
employeeList.add(createEmployee(200L, "Adam Schawn", "444-111-777"));
17+
return employeeList;
18+
}
19+
20+
private Employee createEmployee(long id, String name, String contactNumber) {
21+
Employee employee = new Employee();
22+
employee.setId(id);
23+
employee.setName(name);
24+
employee.setContactNumber(contactNumber);
25+
return employee;
26+
}
27+
}

0 commit comments

Comments
 (0)