samedi 1 avril 2017

Entity classes architecture in a RESTful servise

I'm creating a RESTful service with Java. My database layer is provided by JPA (also using Spring data repository). So, I have a package with persistence entities. My RESTful API is supposed to provide data in JSON format.

Here's an example of my Controller class:

@Controller
@RequestMapping("/category")
public class CategoryController {

    @RequestMapping(value = "{categoryId:\\d+}", method = RequestMethod.GET, produces = MediaType.APPLICATION_JSON)
    public @ResponseBody CategoryEntity getCategory(@PathVariable long categoryId) {
        return = repo.findOne(categoryId);
    }

    @Autowired
    private CategoryRepository repo;
}

So, as you see, my getCategory() method returns the object of type CategoryEntity, which is a persistence entity. I'm currently in doubt whether I should leave it this way, or create another package with business entities classes, instantiate one, fill with data from the persistence entity and return in my getCategory() method. As for now, there seems to be no difference since both classes would contain exactly the same fields and would result in the same JSON. But what if things change in some way as my app developes. I was wondering if there is any best practice?

Aucun commentaire:

Enregistrer un commentaire