Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Invoke controller method from java class

I just want to know whether controller class method is accessible from another java class.    

following is my controller and its method.

@Controller
public class TestResultUploadController {
    @RequestMapping(method = RequestMethod.POST,value="/uploadTestResult")  
    public @ResponseBody  
     String uploadTestResult(String testResultBarcode,int deviceLoc) {

         //some code goes here
    return something;
}

I just want to call this controller method from another java class. How can I make it work? please suggest..

like image 995
Swapnil Walivkar Avatar asked Dec 04 '13 12:12

Swapnil Walivkar


People also ask

How spring determines your controller classes?

Spring implements a controller in a very abstract way, which enables you to create a wide variety of controllers. Spring 2.5 introduced an annotation-based programming model for MVC controllers that uses annotations such as @RequestMapping , @RequestParam , @ModelAttribute , and so on.

What is a controller class in Java?

A controller class is normally a class part of the Model View Controller (MVC) pattern. A controller basically controls the flow of the data. It controls the data flow into model object and updates the view whenever data changes.

What is controller annotation in Java?

The @Controller annotation indicates that a particular class serves the role of a controller. Spring Controller annotation is typically used in combination with annotated handler methods based on the @RequestMapping annotation. It can be applied to classes only. It's used to mark a class as a web request handler.


1 Answers

Short answer: yes, it is possible. In your other class/thread, you can do

// this will create a new instance of that controller where no fields are wired/injected
TestResultUploadController controller = new TestResultUploadController();
controller.uploadTestResult("someString", 1234); 

However, keep in mind that your setup is highly unusual and all your autowired fields wouldn't be wired correctly. If you obtain your controller from the context, you'd be able to have your fields wired/injected properly:

// obtain controller bean from context, should have fields wired properly
TestResultUploadController controller = ctx.getBean(TestResultUploadController.class);
controller.uploadTestResult("someString", 1234); 

or you can, in your other class, have:

@Autowired private TestResultUploadController controller;

....
public void doStuff(){
    controller.uploadTestResult("someString", 1234); 
}

Again, this is highly unusual, but highly possible. However, just cause something is possible to be done, doesn't mean you should do it. I would recommend the more common Spring/MVC approach in which you outsource the business logic to Services. Basically, to have something like this:

@Controller
public class TestResultUploadController {

    @Autowired private UploadTestResultService uploadTestResultService;

    @RequestMapping(method = RequestMethod.POST,value="/uploadTestResult")  
    public @ResponseBody String uploadTestResult(String testResultBarcode,int deviceLoc) {
        return uploadTestResultService.uploadTestResult(testResultBarcode, deviceLoc);
    }
}

And in your thread:

//somehow get the service
UploadTestResultService uploadTestResultService = //somehowGetTheService (whether from context or in some other way)
uploadTestResultService.uploadTestResult(testResultBarcode, deviceLoc);

That way, you'd be able to mock the UploadTestResultService in your tests of the controller, and you'd also be able to test the uploadTestResult method of that service on its own without it being in a controller.

EDIT: How to obtain the Spring context is outside of the scope of this question. I assume you know basic Spring and also basic java.

like image 136
Nikola Yovchev Avatar answered Sep 28 '22 11:09

Nikola Yovchev