java - 想要使用 post 方法的剩余模板来使用服务

标签 java rest spring-boot resttemplate

public class CommonMethods {
    final Logger logger = (Logger) LoggerFactory.getLogger(CommonMethods.class);

    @Autowired
    HttpClientFactory httpClientFactory;
    enter code here
    @Autowired
    JSONCreation jsonCreation;

    String responseAsStringEmployment;

    public String getEmploymentDetails(EmpProfile empProfile) throws IOException, JSONException {

        HttpClient clientEmployment = httpClientFactory.getHttpClient();

        String urlEmployment = Config.getDefaultCommonServiceURL();
        HttpPost requestEmployment = new HttpPost(urlEmployment + "getEmploymentDetails");
        requestEmployment.addHeader("content-type", "application/json");

        StringEntity params;
        String a = jsonCreation.createJsonforNode(empProfile);
        params = new StringEntity(a);

        requestEmployment.setEntity(params);

        logger.info("Request Employment :" + requestEmployment);
        logger.info("clientEmployment :" + clientEmployment);
        HttpResponse responseEmployment = clientEmployment.execute(requestEmployment);
        logger.info("responseEmployment :" + responseEmployment);
        responseAsStringEmployment = EntityUtils.toString(responseEmployment.getEntity());
        logger.info("Response Employment : " + responseAsStringEmployment);

        logger.info("responseAsStringEmployment  " + responseAsStringEmployment);

        return responseAsStringEmployment;
    }

我必须使用rest模板而不是httpclient。 http客户端运行正常。

我尝试过的休息模板方法如下:

public String getEmployment() {

        String url = "https://empprofile-blue.bdt.com/getEmploymentDetails";
        EmpProfile empProfile = new EmpProfile();
        RestTemplate restTemplate = new RestTemplate();
        EmpProfile result = restTemplate.postForObject(url, empProfile, EmpProfile.class);

        System.out.println(result);

        logger.info(empProfile.toString());

        return result.toString();
}

想要在休息模板中配置超时如何实现。

最佳答案

@Autowired
    RestTemplate restTemplate;
    @Autowired
    HttpClientFactory httpClientFactory;


public String getPassportDetails(EmpProfile empProfile) throws JSONException {

        String url = Config.getDefaultCommonServiceURL();

        String a = jsonCreation.createJsonforNode(empProfile);
        HttpHeaders headers = new HttpHeaders();
        headers.setContentType(MediaType.APPLICATION_JSON);
        HttpEntity<String> entity = new HttpEntity<>(a, headers);
    ((HttpComponentsClientHttpRequestFactory)this.restTemplate.getRequestFactory()).setReadTimeout(1 * 5000);//timeout

        String result = restTemplate.postForObject(url, entity, String.class);
        logger.info(result);

        logger.info(empProfile.toString());

        return result;

    }

关于java - 想要使用 post 方法的剩余模板来使用服务,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43628841/

相关文章:

java - 扫描仪在使用 next() 或 nextFoo() 后跳过 nextLine()?

java - 在根项目 gradle 上找不到属性 'sourceSets'

java - Unresolved 主机异常 Android

java - 带有 { } 大括号的 Spring MVC @Path 变量

database - 在哪里将 -ifNotExists 标志添加到 h2 start 命令?

java - Eclipse 中未处理的异常类型 IOException

java - 将 5 位整数转换为列

java - RestEasy 服务部署在 Tomcat 7.0.47

rest - 学习服务器 RESTful 代码的最佳方式是什么?

java - Spring Boot 2.1.0 发布出现 404 not found 错误