user3572512
user3572512

Reputation: 21

Spring MVC @pathvariable annotated does not pass the value from interface to implementation

Service Class

@Service

@RequestMapping(value = "employees")

public interface EmployeeService {

    @RequestMapping(value = "{id}", method = RequestMethod.GET)
    public @ResponseBody Employee getEmployee(@PathVariable("id") int employeeId) throws EmpException;

    @RequestMapping(method = RequestMethod.GET)
    public @ResponseBody List<Employee> getAllEmployees() throws EmpException;

    @RequestMapping(method = RequestMethod.POST)
    public @ResponseBody Employee createEmployee(@RequestBody Employee employee) throws EmpException;


    @RequestMapping(value ="{id}", method = RequestMethod.DELETE)
    public @ResponseBody UserInfo deleteEmployee(@PathVariable("id") int employeeId) throws EmpException;

    @RequestMapping(value="{id}", method = RequestMethod.PUT)
    public @ResponseBody Employee updateEmployee(@RequestBody Employee employee,@PathVariable("id") int employeeId) throws EmpException;

}

Implementation class

@Service("employeeService")

public class EmployeeServiceImpl implements EmployeeService {    
   @Autowired
   private Employee employee;

   private static final Logger logger = LoggerFactory.getLogger(EmployeeServiceImpl.class);
public Employee getEmployee(@PathVariable("id") int employeeId) throws EmpException {
        logger.info("Start getEmployee. ID="+employeeId);
        employee = employeeDao.getEmployee(employeeId);
        if(employee != null) {
            return employee;
        } else {
            throw new EmpException("ID: "+employeeId+" is not available");
        }
    }
}

in implementation class also i used @pathvariable annotation then only value for employeeId will be pass from interface to implementation otherwise null pointer expression will be occur.any other way to pass the value from interface to implementation without using @pathvariable .

Upvotes: 2

Views: 809

Answers (1)

NimChimpsky
NimChimpsky

Reputation: 47300

Request mappings don't go on service class, they go on controller.

For which you'll need @Controller annotation.

Upvotes: 2

Related Questions