如何使用Spring Boot在运行时创建指向系统中REST端点的URL?

时间:2019-03-28 16:01:26

标签: java spring rest spring-boot

我正在使用Spring Boot构建REST API。在我的情况下,有2个控制器:ExportControllerImportController。请在此处检查示例代码:

导出控制器:

@RestController
public class ExportController {

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

}

导入控制器:

@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 = ...
    }

}

在我的ImportController内,我想生成一个指向/export端点的URL,例如http://www.example.com/export?id=1234

我没有在application.properties中配置有关主机或端口的任何内容。我想在运行时获取它们。

能否请您告诉我如何实现?我在互联网上进行了很多搜索,但找不到答案。谢谢您的帮助。

4 个答案:

答案 0 :(得分:1)

如果您可以将spring-hateoas引入项目,那么它将起作用:

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

}

这将产生http://localhost:8080/export?id=1234

答案 1 :(得分:0)

@RequestMapping(path="/import",...)
public ResponseEntity<?> method(@RequestParam(name="url") final String url)//@PathVariable(name="url") final String url
{   
    String finalURL = "yourURL"
    sendRequestToURL(finalURL);  
}

@RequestMapping(path="/{finalURL}",...)
public ResponseEntity<?> sendRequestToURL(@PathVariable("finalURL") String url)
{
    //yourCode
}

OR

@RequestMapping(path="/import",...)
    public ResponseEntity<?> method(@RequestParam(name="url") final String url)//@PathVariable(name="url") final String url
    {   
        String finalURL = "yourURL"
        URL url = new URL(finalURL);
        HttpURLConnection conn = (HttpURLConnection) url.openConnection();
        conn.setRequestMethod("yourMethod");
    }

答案 2 :(得分:0)

ServletUriComponentsBuilder起,您可以使用Spring framework随附的3.1.RELEASE

鉴于您有权访问当前请求,可以执行以下操作。

 UriComponents uriComponents = ServletUriComponentsBuilder
        .fromRequest(httpServletRequest)
        .replacePath("/export")
        .queryParam("id",1234)
        .build();
 String url = uriComponents.toUri();

答案 3 :(得分: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";
    }
}