BAEL-1547: Request method not supported - 405, examples. (#5464)

This commit is contained in:
azrairshad 2018-10-17 10:06:37 -04:00 committed by Eugen
parent 90978bf35e
commit 2f01b43056
4 changed files with 90 additions and 0 deletions

View File

@ -0,0 +1,25 @@
package com.baeldung.spring.controller;
import com.baeldung.spring.domain.Employee;
import com.baeldung.spring.exception.InvalidRequestException;
import com.baeldung.spring.service.EmployeeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;
import java.util.List;
@RestController
@RequestMapping(value="/api")
public class RequestMethodController {
@Autowired
EmployeeService service;
@RequestMapping(value = "/employees", produces = "application/json", method={RequestMethod.GET,RequestMethod.POST})
public List<Employee> findEmployees()
throws InvalidRequestException {
return service.getEmployeeList();
}
}

View File

@ -0,0 +1,26 @@
package com.baeldung.spring.exception;
public class InvalidRequestException extends RuntimeException {
/**
*
*/
private static final long serialVersionUID = 4088649120307193208L;
public InvalidRequestException() {
super();
}
public InvalidRequestException(final String message, final Throwable cause) {
super(message, cause);
}
public InvalidRequestException(final String message) {
super(message);
}
public InvalidRequestException(final Throwable cause) {
super(cause);
}
}

View File

@ -0,0 +1,12 @@
package com.baeldung.spring.service;
import com.baeldung.spring.domain.Employee;
import org.springframework.stereotype.Service;
import java.util.List;
@Service
public interface EmployeeService {
List<Employee> getEmployeeList();
}

View File

@ -0,0 +1,27 @@
package com.baeldung.spring.service;
import com.baeldung.spring.domain.Employee;
import org.springframework.stereotype.Service;
import java.util.ArrayList;
import java.util.List;
@Service
public class EmployeeServiceImpl implements EmployeeService{
@Override
public List<Employee> getEmployeeList() {
List<Employee> employeeList = new ArrayList<>();
employeeList.add(createEmployee(100L, "Steve Martin", "333-777-999"));
employeeList.add(createEmployee(200L, "Adam Schawn", "444-111-777"));
return employeeList;
}
private Employee createEmployee(long id, String name, String contactNumber) {
Employee employee = new Employee();
employee.setId(id);
employee.setName(name);
employee.setContactNumber(contactNumber);
return employee;
}
}