使用restTemplate发布post方法,错误java.net.UnknownHostException

时间:2017-05-09 09:19:30

标签: resttemplate

我需要将Json发布到服务器并获取Json结果,我将使用restTemplate来实现。但问题是它始终发生错误java.net.UnknownHostException。 控制器:

Headings_wd[a] = Label(root, text = Headings[a], width = 10, anchor = 'e')

Units_wd[a] = Label(root, text = Units[a], width = 10, anchor = 'e' , font = ("Helvetica" , 6) )

test.jsp:

@RequestMapping(value = "/test")
public String test(HttpServletRequest request, HttpServletResponse response){
    String user=request.getParameter("txtUser");
    HttpSession session = request.getSession();
    session.setAttribute("result",accountService.findFarmsByUser(user));
    return "test";
}

服务: 我在xml中配置端口和主机。

<form action="test">
    <input type="text" value="insert search" name="txtUser">
    <input type="submit" value="submit" name="btnSubmit">
    <input type="text" value="" name="result">
</form>

我遗失的任何事情?它适用于邮递员。我在公司工作,所以我必须为主机配置主机和端口。

错误:

  //create  request json
    JSONObject json = new JSONObject().put(
            "input",
            new JSONObject().put(
                    "username",
                    "Administrator"));

    // set headers
    HttpHeaders headers = new HttpHeaders();
    headers.setContentType(MediaType.APPLICATION_JSON);
    HttpEntity<String> entity = new HttpEntity<String>(json.toString(), headers);
    headers.set(proxyHost,Host);
    headers.set(proxyPort,Port);

    ResponseEntity<String> loginResponse = restTemplate
            .exchange(url, HttpMethod.POST, entity, String.class);
    logger.info("reutrn data: {}",loginResponse);

1 个答案:

答案 0 :(得分:0)

可能是贵公司的代理人。您可以在spring config中定义它:

    @Bean
    public RestTemplate restTemplate(RestTemplateBuilder builder) {

        SimpleClientHttpRequestFactory requestFactory = new SimpleClientHttpRequestFactory();
        Proxy proxy = new Proxy(Type.HTTP, new InetSocketAddress("clientproxy.corp.url", 8080));
        requestFactory.setProxy(proxy);
        return builder.requestFactory(requestFactory).setConnectTimeout(5000).setReadTimeout(5000).build();
    }