Spring Boot:在映射级别指定端口 [英] Spring Boot: specify port at the mapping level

查看:126
本文介绍了Spring Boot:在映射级别指定端口的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

Spring Boot:我想实现以下目标:一些URL路径映射到一个端口,另一些映射到另一个.

Spring Boot: I want to have achieved the following: some URL paths are mapped to a port, some to another.

换句话说,我想要类似的东西:

In other words I'd like something like:

public class Controller1 {
  @RequestMapping(value="/path1", port="8080") public...
  @RequestMapping(value="/path2", port="8081") public...
}

这样我的应用程序就可以响应localhost:8080/path1和localhost:8081/path2

So that my app responds to both localhost:8080/path1 and localhost:8081/path2

在应用中可以有2个单独的控制器.

It's acceptable to have 2 separate controllers within the app.

通过为tomcat实现EmbeddedServletContainerCustomizer,我已经成功地取得了部分成功,但是如果可能的话,能够在控制器内实现此目标也很高兴.

I have managed to partially succeed by implementing an EmbeddedServletContainerCustomizer for tomcat, but it would be nice to be able to achieve this inside the controller if possible.

有可能吗?

推荐答案

虽然不能阻止在不需要的端口上进行调用,但是可以在控制器方法的其他参数中指定HttpServletRequest,然后使用HttpServletRequest.getLocalPort()获取进行呼叫的端口.

While you cannot prevent making call on the undesired port, you can specify HttpServletRequest among other parameters of the method of the controller, and then use HttpServletRequest.getLocalPort() to obtain the port the call is made on.

然后,如果请求是在错误的端口上进行的,则可以手动返回HTTP错误代码;如果设计为必须对不同端口上的相同路径进行不同的处理,则可以转发到另一个控制器.

Then you can manually return the HTTP error code if the request is made on the wrong port, or forward to another controller if the design is such that same path on different ports must be differently processed.

这篇关于Spring Boot:在映射级别指定端口的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆