Skip to content

Instantly share code, notes, and snippets.

@javaeeeee
Created November 6, 2015 13:06
Show Gist options
  • Select an option

  • Save javaeeeee/9c15264afc4017624852 to your computer and use it in GitHub Desktop.

Select an option

Save javaeeeee/9c15264afc4017624852 to your computer and use it in GitHub Desktop.
A database-backed resource class for a Dropwizard application
@Path("/employees")
@Produces(MediaType.APPLICATION_JSON)
public class EmployeesResource {
/**
* The DAO object to manipulate employees.
*/
private EmployeeDAO employeeDAO;
/**
* Constructor.
*
* @param employeeDAO DAO object to manipulate employees.
*/
public EmployeesResource(EmployeeDAO employeeDAO) {
this.employeeDAO = employeeDAO;
}
/**
* Looks for employees whose first or last name contains the passed
* parameter as a substring. If name argument was not passed, returns all
* employees stored in the database.
*
* @param name query parameter
* @return list of employees whose first or last name contains the passed
* parameter as a substring or list of all employees stored in the database.
*/
@GET
@UnitOfWork
public List<Employee> findByName(
@QueryParam("name") Optional<String> name
) {
if (name.isPresent()) {
return employeeDAO.findByName(name.get());
} else {
return employeeDAO.findAll();
}
}
/**
* Method looks for an employee by her id.
*
* @param id the id of an employee we are looking for.
* @return Optional containing the found employee or an empty Optional
* otherwise.
*/
@GET
@Path("/{id}")
@UnitOfWork
public Optional<Employee> findById(@PathParam("id") LongParam id) {
return employeeDAO.findById(id.get());
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment