注入URL()Spring Autowired

时间:2016-04-24 10:27:50

标签: java spring spring-mvc

我希望将Java URL类注入到我的Spring项目中,但是URL会引用一个参数(您希望打开的URL)。

我该怎么做呢?

我目前有

private HttpURLConnection getConnectionHandler(String urlToPing) throws MalformedURLException, IOException, ProtocolException {
        URL url = new URL(urlToPing);

        HttpURLConnection connection = (HttpURLConnection) url.openConnection();

        connection.setRequestMethod("GET");
        connection.connect();

        return connection;
    }

1 个答案:

答案 0 :(得分:1)

Spring已经将String转换为URL,它可以开箱即用。

只需更改方法签名即可使用URL而不是String

相关问题