I'm using spring-rest
to create some @RestController
servlets. The application is not run on a webserver, but as a simple command line tool with embedded tomcat.
Most of them should be running on a public port, which is specified using server.port=80
property.
Question: how can I run different @RestController
on different ports? So that some of them are only accessibly internally?
@RestController
@RequestMapping("test")
public class TestServlet {
@RequestMapping(value = "/", method = RequestMethod.GET, produces = MediaType.TEXT_PLAIN_VALUE)
@ResponseStatus(HttpStatus.OK)
@ResponseBody
public String test() { return "OK"; }
}
I would recommend to use a proxy. It could be an Apache Web Server, or Ngix. You need to configure the two virtualhosts (in different ports) in the webserver. And redirect the invocation to your tomcat server. You can load your controllers in differents paths so it'll easier to proxyfy the calls from the weberver.
Finally your clients make the invocation through the web server, not directly to tomcat.
If you're on Spring Boot, I think you should check out Spring Boot Actuator. Your application can be set to one port while actuator runs off another.
Here's a guide that shows how to change the port for Actuator - https://spring.io/guides/gs/actuator-service/