6

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

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

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

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.

Is it possible?

Luke 10X
  • 1,071
  • 2
  • 14
  • 30
Diego Martinoia
  • 4,592
  • 1
  • 17
  • 36
  • 1
    Pretty sure it's not possible to specify the port at this level, considering that the application at this stage is already bound to another one. I'd like to know more as to why you want to do this, but I feel like it'd be doable at a higher level of server abstraction. – Makoto Sep 14 '16 at 15:58
  • @Makoto Ops requirement to have "control" calls (shutdown) on a different port than "application" calls. What do you mean by "at an higher level of abstraction"? – Diego Martinoia Sep 14 '16 at 16:28
  • My initial thought would be to consider using rewrite rules, but if you're just looking for shutdown routes...there may be a better way to do it. Spring Boot comes with a built-in shutdown route. – Makoto Sep 14 '16 at 16:47
  • @Makoto I'm not aware of the shutdown route being provided. Can you link some resource? – Diego Martinoia Sep 15 '16 at 08:51

2 Answers2

7

What you are trying to do would imply that the application is listening on multiple ports. This would in turn mean that you start multiple tomcat, since spring-boot packages one container started on a single port.

What you can do

You can launch the same application twice, using different spring profiles. Each profile would configure a different port.

2 properties:

application-one.properties: server.port=8080

application-two.properties: server.port=8081

2 controllers

@Profile("one")
public class Controller1 {
  @RequestMapping(value="/path1") public...
}

@Profile("two")
public class Controller2 {
  @RequestMapping(value="/path2") public...
}

Each controller is activated when the specified spring profile is provided.

Launch twice

$ java -jar -Dspring.profiles.active=one YourApp.jar
$ java -jar -Dspring.profiles.active=two YourApp.jar
Community
  • 1
  • 1
alexbt
  • 16,415
  • 6
  • 78
  • 87
  • OP seems to use tomcat. What about tomcat configuration? – Ortomala Lokni Sep 14 '16 at 17:55
  • what do you mean ? The question and answer is about a spring-boot application, which embeds a tomcat by default. The answer sets tomcat's ports through the application.properties. What more tomcat configuration would be required than the ports ? Perhaps you mistaken this question with a typical spring app, however this is spring-boot. – alexbt Sep 14 '16 at 17:57
  • I was thinking about a spring-boot application deployed inside a standalone tomcat instance. But this is maybe outside the scope of this question. – Ortomala Lokni Sep 14 '16 at 18:03
  • 1
    This is a valid solution. I'll keep this open for a while to see if something else comes up. For clarity: we are using the springboot embedded tomcat, not an external one. – Diego Martinoia Sep 15 '16 at 08:52
2

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.

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.

Audrius Meškauskas
  • 20,936
  • 12
  • 75
  • 93