3

I am developing a Spring Web MVC application. I have query related to CRUD or complex operation.

My question is where to put our logic, in the controller or in-service layer?

Currently, I am writing all code in controller and call save() or delete() methods of service in the controller.

See below code examples. Please help me to find the correct way to code.

Controller level:

@Controller
@RequestMapping(value = "/Shipping")
public class ShippingController {

 @Autowired
 private ShippingService shippingService;


 @RequestMapping(value = "/addNewShippingMethod/{id}", method = RequestMethod.POST)
 public String addshippingMethod(@PathVariable("id") String id, HttpServletRequest request) {
     Shipping shipping=new Shipping();
     shipping.setName(request.getParameter("name"));
     shippingService.save(shipping);
     return "/Shipping/ShippingMethodList";
    }
}
@Service
public class ShippingService {
 @Autowired
 private ShippingMethodRespository shippingMethodRespository;

 public ModelAndView saveShippingMethod(HttpServletRequest request, String id) {    
     shippingMethodRespository.save(shipping);
 }
}

Service level:

@Controller
@RequestMapping(value = "/Shipping")
        public class ShippingController {

        @Autowired
        private ShippingService shippingService;

        @RequestMapping(value = "/addNewShippingMethod/{id}", method = RequestMethod.POST)
        public ModelAndView addshippingMethod(@PathVariable("id") String id, HttpServletRequest request) {

            return shippingService.saveShippingMethod(request, id);
        }
}
@Service
public class ShippingService {
     @Autowired
     private ShippingMethodRespository shippingMethodRespository;

     public ModelAndView saveShippingMethod(HttpServletRequest request, String id) {
        Shipping shipping=new Shipping();
        shipping.setName(request.getParameter("name"));
        shippingMethodRespository.save(shipping);
        ModelAndView shippingMethodsPage = new 
        ModelAndView("/Shipping/ShippingMethodList");
        return shippingMethodsPage;
    }    
}

I have described both scenarios first one is: we save all data from the controller and return to the page.

The second one is: use a controller for redirect and writing logic and save data in the service layer.

Can you please tell me which is the best way to do code?

Mark Rotteveel
  • 100,966
  • 191
  • 140
  • 197
Bwt Developer
  • 51
  • 1
  • 1
  • 7
  • @DushyantTankariya i think its not same as my question. in that they only discussed about dependency. do you have any idea about my question plz? – Bwt Developer Jul 20 '19 at 06:42

1 Answers1

5

Yes, you can gather some knowledge about when to use the service layer? You can start with Use of Service Layer stack overflow or Implementing Business Logic from docs.oracle.com.

Now, if we look at both ways mentioned in your question, then it represents that in the first case "write business logic in Controller" is not the way you should go.

So definitely I vote for the second part because when you have service layer then you should write your business logic over there.

@Controller
@RequestMapping(value = "/Shipping")
        public class ShippingController {

        @Autowired
        private ShippingService shippingService;

        @RequestMapping(value = "/addNewShippingMethod/{id}", method = RequestMethod.POST)
        public ModelAndView addshippingMethod(@PathVariable("id") String id, HttpServletRequest request) {

            return shippingService.saveShippingMethod(request, id);
        }
}
@Service
public class ShippingService {
     @Autowired
     private ShippingMethodRespository shippingMethodRespository;

     public ModelAndView saveShippingMethod(HttpServletRequest request, String id) {
        Shipping shipping=new Shipping();
        shipping.setName(request.getParameter("name"));
        shippingMethodRespository.save(shipping);
        ModelAndView shippingMethodsPage = new 
        ModelAndView("/Shipping/ShippingMethodList");
        return shippingMethodsPage;
    }    
}
Mark Rotteveel
  • 100,966
  • 191
  • 140
  • 197
Dushyant Tankariya
  • 1,432
  • 3
  • 11
  • 17
  • @mark-rotteveel so the controller role is only handle request and route to specific path right ? and service layer is handling all transactional and business logic right ? – Bwt Developer Jul 20 '19 at 12:05
  • @BwtDeveloper Why are you @-ing me, did you mean to address Dushyant instead? – Mark Rotteveel Jul 20 '19 at 12:14
  • Yes @BwtDeveloper, you get the point. – Dushyant Tankariya Jul 22 '19 at 05:51
  • What to do when we have a premade application (configured with jhipster), and each jpa entity has its own controller / service / repository associated? I mean, what's a good strategy to implement some complex business logic, that needs to pull data from different repositories and finally return a result ? thanks – funder7 Apr 16 '20 at 12:41
  • Hi @Funder, Sorry for the delay in response. when configured with jhipster and each jpa entity has its own controller / service / repository associated then use that default architecture to implement business logic - Suggested. Perhaps any architecture can be extended you can change the architect created by jhipster and build your own - Which I'll not suggest until and unless complete knowledge in project architect. – Dushyant Tankariya Apr 27 '20 at 06:28
  • Hi @DushyantTankariya, no problem! :-) Thanks for your advice, in fact I'm keeping the original architecture, what I meant was related to working with services that need to include more than one respository. In fact I've created another service, separated from what's generated by jhipster. In this way I'm avoiding conflicts when re-importing the jdl file! – funder7 May 04 '20 at 17:06