s21s
s21s

Reputation: 29

Redirect to a POST request from one controller to another controller Spring Boot

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?

Upvotes: 0

Views: 4069

Answers (2)

huseyinkadioglu
huseyinkadioglu

Reputation: 119

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+"first/").toUri();
   restTemplate.postForEntity(uri, "something", String.class);
   // check your resttemplate docs, i used postForEntity here.

   // if necessery return something with response, this method expects the return string but you get the idea.
   }
}

This should work.

Upvotes: 1

Ervin Szilagyi
Ervin Szilagyi

Reputation: 16775

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
   }
}

Upvotes: 2

Related Questions