简体   繁体   English

从java类调用控制器方法

[英]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. 我只想从另一个java类调用这个控制器方法。 How can I make it work? 我怎样才能使它工作? please suggest.. 请建议..

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. 我建议使用更常见的Spring / MVC方法,将业务逻辑外包给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. 这样,您就可以在控制器的测试中模拟UploadTestResultService,并且您还可以自己测试该服务的uploadTestResult方法,而不需要在控制器中。

EDIT: How to obtain the Spring context is outside of the scope of this question. 编辑:如何获取Spring上下文超出了本问题的范围。 I assume you know basic Spring and also basic java. 我假设你知道基本的Spring和基本的java。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM