Home > Enterprise >  Redirect to a POST request from one controller to another controller Spring Boot
Redirect to a POST request from one controller to another controller Spring Boot

Time:11-07

I have a springboot project with 2 controller files as below:

File1.java
 @PostMapping("/test")
    public String testMap(String s){
         if(s!=null){
           return "found it";
         }
        else {
            // need to go to POST request in another controller
        }
        return "not found";
    }
File2.java
 @PostMapping("/test2")
    public String testMap2(String s){
         if(s!=null){
           return "found it";
         }
        return "not found 2";
    }

I have tried adding java HttpURLConnection lines to send a POST request in File1.java but it does not perform the operations within testMap2, instead it exits with not found

Could you please give some suggestions on how I could accomplish this?

CodePudding user response:

You could use RestTemplate to create another POST request, although I strongly suggest avoiding that.

Since both of these controllers are in the same project, try extracting the common logic into a @Service which should be injected in both controllers.

For example:

File1.java

@RestController
public class MyFirstController {
    private MyBusinessLogic myBusinessLogic;
 
    // Constructor injection
    public MyFirstController(MyBusinessLogic myBusinessLogic) {
        this.myBusinessLogic = myBusinessLogic;
    }
    
    @PostMapping("/test")
    public String testMap(String s){
         if(s!=null){
           return "found it";
         }
        else {
            return myBusinessLogic.doSomething(s);
        }
        return "not found";
    }
}

File2.java:

@RestController
public class MySecondController {
    private MyBusinessLogic myBusinessLogic;
 
    // Constructor injection
    public MySecondController(MyBusinessLogic myBusinessLogic) {
        this.myBusinessLogic = myBusinessLogic;
    }

    @PostMapping("/test2")
    public String testMap2(String s){
         if(s!=null){
           return myBusinessLogic.doSomething(s);
         }
        return "not found 2";
    }
}

Finally create a service for the common logic:

@Service
public class MyBusinessLogic {
   public String doSomething(String s) {
      // common logic goes here
   }
}

CodePudding user response:

You can use RestTemplate.

Lets say our controller looks like this:

  @RestController
    @RequestMapping(value = "first/")
    public class FirstRestController {

         @PostMapping("test")
         public String getTest(String s){
       
            return service.doSomething(s);
         }
    }

Basically, add this method as a bean in one of your config classes. @Bean puts the method in application context. Now we can inject this method in our services.

@Bean
public RestTemplate restTemplate() {
    return new RestTemplate();
}

Now, one of our service methods in Second App, we must call the endpoint of First.

@Service
public class SecondAppService{

  @Autowired
   private RestTemplate restTemplate;


   public String callFirst() {
      
   final URI uri =UriComponentsBuilder.fromUriString("PATH").toUri();
   restTemplate.postForEntity(uri, "something", String.class);

   }
}

This should work.

  • Related