3. Contact REST Code Example

The contact controller just needs to have an interface that extends PersistenceMarshallingService and a controller that implements that interface.

Example 3. PersonMarshallingService

The marshalling service extends PersistenceMarshallingService and defines all the paths for the controller as constants.

                
public interface PersonMarshallingService extends PersistenceMarshallingService<PersonResponse, PersonFindResponse, Person> {
    
    final static String PATH = "/person";
    
    public final static String FIND_BY_ID_REQUEST = PATH + PATH_DELIM + "{" + ID_VAR + "}";
    public final static String FIND_PAGINATED_REQUEST = PATH + PAGINATED;
    public final static String FIND_REQUEST = PATH;
    public final static String SAVE_REQUEST = PATH;
    public final static String UPDATE_REQUEST = FIND_BY_ID_REQUEST;
    public final static String DELETE_PK_REQUEST = FIND_BY_ID_REQUEST;
    public final static String DELETE_REQUEST = PATH;

}
                
            

Example 4. PersonController

The controller primarily delegates to it's service and also defines a @RequestMapping for each method.

                
@Controller
public class PersonController extends AbstractController<Person, PersonResponse, PersonFindResponse> 
        implements PersonMarshallingService {

    final Logger logger = LoggerFactory.getLogger(getClass());

    @Autowired
    public PersonController(ContactService service) {
        super(service);
    }
    
    @Override
    @RequestMapping(value = FIND_BY_ID_REQUEST, method = RequestMethod.GET)
    public PersonResponse findById(@PathVariable(ID_VAR) long id) {
        logger.info("Find person.  id={}", id);

        return service.findById((int)id);
    }

    @Override
    @RequestMapping(value = FIND_PAGINATED_REQUEST, method = RequestMethod.GET)
    public PersonFindResponse find(@PathVariable(PAGE_VAR) int page,
                                   @PathVariable(PAGE_SIZE_VAR) int pageSize) {
        logger.info("Find person page.  page={}  pageSize={}", page, pageSize);

        return service.find(page, pageSize);
    }

    @Override
    @RequestMapping(value = FIND_REQUEST, method = RequestMethod.GET)
    public PersonFindResponse find() {
        logger.info("Find all persons.");

        return service.find();
    }

    @Override
    @RequestMapping(value = SAVE_REQUEST, method = RequestMethod.POST)
    public PersonResponse save(@RequestBody Person request) {
        Assert.isTrue(!isPrimaryKeyValid(request), "Create should not have a valid primary key.");
        
        logger.info("Save person.  id={}", request.getId());

        return service.create(request);
    }

    @Override
    @RequestMapping(value = UPDATE_REQUEST, method = RequestMethod.PUT)
    public PersonResponse update(@RequestBody Person request) {
        Assert.isTrue(isPrimaryKeyValid(request), "Update should have a valid primary key.");
        
        logger.info("Update person.  id={}", request.getId());
        
        return service.update(request);
    }

    @Override
    @RequestMapping(value = DELETE_PK_REQUEST, method = RequestMethod.DELETE)
    public ResponseResult delete(@PathVariable(ID_VAR) long id) {
        logger.info("Delete person.  id={}", id);

        return service.delete((int)id);
    }

    @Override
    @RequestMapping(value = DELETE_REQUEST, method = RequestMethod.DELETE)
    public ResponseResult delete(@RequestBody Person request) {
        Assert.isTrue((request.getId() > 0), "Delete should have a valid primary key");
        
        int id = request.getId();
        
        return delete((int)id);
    }

}