0

I'm using Spring Boot to build a REST API. In my situation, there are 2 controllers: ExportController and ImportController. Please check the example code here:

Export Controller:

@RestController
public class ExportController {

    @GetMapping(value = "/export", produces = MediaType.APPLICATION_OCTET_STREAM_VALUE)
    public ResponseEntity<Resource> export(@RequestParam("id") String id) {
        // Processing...
    }

}

Import Controller:

@RestController
public class ImportController {

    @PostMapping(value = "/import", produces = MediaType.APPLICATION_JSON_VALUE)
    public ResponseEntity<?> importData(HttpServletRequest request) {
        // Processing...

       // What should I do here?
       String url = ...
    }

}

Inside my ImportController, I want to generate a URL pointing to the /export endpoint, e.g. http://www.example.com/export?id=1234.

I don't configure anything about the host or port in the application.properties. I want to get them at runtime.

Could you please show me how to achieve it? I searched a lot on the Internet but couldn't find the answer. Thank you for your help.

Triet Doan
  • 11,455
  • 8
  • 36
  • 69
  • https://stackoverflow.com/questions/21083170/how-to-configure-port-for-a-spring-boot-application – sh.seo Mar 28 '19 at 16:10
  • Duplicate of https://stackoverflow.com/questions/5012525/get-root-base-url-in-spring-mvc – sparse Mar 28 '19 at 16:13

3 Answers3

1

If you can live with bringing spring-hateoas into your project then this will work:

import static org.springframework.hateoas.mvc.ControllerLinkBuilder.methodOn;

@RestController
public class ImportController {

    @PostMapping(value = "/import", produces = MediaType.APPLICATION_JSON_VALUE)
    public ResponseEntity<?> importData(HttpServletRequest request) {
        String someId = "1234";

        ControllerLinkBuilder linkBuilder = ControllerLinkBuilder.linkTo(methodOn(ExportController.class).export(someId));

        URI uri = linkBuilder.toUri();

        return ResponseEntity.ok(uri);
    }

}

This yields http://localhost:8080/export?id=1234

Not a JD
  • 1,864
  • 6
  • 14
0

You can make use of ServletUriComponentsBuilder that comes with Spring framework since 3.1.RELEASE.

Given that you have access to current request, You can do something like below.

 UriComponents uriComponents = ServletUriComponentsBuilder
        .fromRequest(httpServletRequest)
        .replacePath("/export")
        .queryParam("id",1234)
        .build();
 String url = uriComponents.toUri();
Raja Anbazhagan
  • 4,092
  • 1
  • 44
  • 64
  • Hi, I don't want to hardcoded the string "/export" but to read it at runtime instead. – Triet Doan Mar 28 '19 at 17:20
  • @TrietDoan, I see your point. But somewhere in your code there is going to be an `ImportController` with a `RequestMapping` annotation that has a hardcoded `path` value. You can always move these path values to a static String constant and use it everywhere else. – Raja Anbazhagan Mar 28 '19 at 17:46
0
@RestController
public class ImportController {
    @PostMapping(value = "/import", produces = MediaType.APPLICATION_JSON_VALUE)
    public ResponseEntity<?> importData(HttpServletRequest request) {
        // Processing...

       String url = request.getScheme() + "://" + 
            request.getServerName() + ":" +                           
            request.getServerPort() + "/export";
    }
}