Please make sure you have imported the correct package
import org.springframework.core.env.Environment;
and then use the Environment object
@Autowired
private Environment env; // Environment Object containts the port number
@GetMapping("/status")
public String status()
{
return "it is runing on"+(env.getProperty("local.server.port"));
}