Use API with JAVA

Integration questions/issues
Alexandre
Posts: 164
Joined: Thu Dec 12, 2019 11:08 am

Re: Use API with JAVA

Post by Alexandre » Tue Feb 25, 2020 2:15 pm

Hello m.allain.
Sorry for late response.
A few days have passed since your previous message. Please, let me know the result of your firewall checking.

m.allain
Posts: 13
Joined: Fri Jan 10, 2020 4:12 pm

Re: Use API with JAVA

Post by m.allain » Tue Feb 25, 2020 3:10 pm

Hello Alexandre,

I didn't answer my own post, but I managed to fix my problem. :)
I was on the wrong track looking at the side of the firewall. :roll:
It was a coding error. I was still using the same instance of "CloseableHttpClient" but it had a limited lifetime.

My code before :

Code: Select all

public class AuthController {

    private CloseableHttpClient httpClient;

    public AuthController() {
        super();
        this.httpClient = getHttpClient();
    }

    private static AuthController controller;

    public static AuthController getController() {
        if (controller == null) {
            controller = new AuthController();
        }
        return controller;
    }

    public AuthResponseModel auth() throws Exception {
        AuthResponseModel toReturn = null;
        try {
            AuthRequestModel requestModel = new AuthRequestModel();
            requestModel.setUserName("username");
            requestModel.setPassword("password");
            HttpPost request = new HttpPost("url");
            request.addHeader(CONTENT, JSON);
            request.setEntity(new StringEntity(JsonObjectUtils.getJsonFromObject(requestModel)));
            final HttpResponse response = this.httpClient.execute(request);
            if (isHttpPostOk(response)) {
                toReturn = (AuthResponseModel) JsonObjectUtils.getObjectFromJson(EntityUtils.toString(response.getEntity()),
                        AuthResponseModel.class);
            }
        } catch (Exception e) {
            
        }
        return toReturn;
    }

    private CloseableHttpClient getHttpClient() {
        return HttpClientBuilder.create().disableAutomaticRetries().setDefaultRequestConfig(
                RequestConfig.custom().setCookieSpec(CookieSpecs.STANDARD).setConnectTimeout(10000).setSocketTimeout(10000).build()).build();
    }

}
My code now :

Code: Select all

public class AuthController {

    public AuthController() {
        super();
    }

    private static AuthController controller;

    public static AuthController getController() {
        if (controller == null) {
            controller = new AuthController();
        }
        return controller;
    }

    public AuthResponseModel auth() throws Exception {
        AuthResponseModel toReturn = null;
        try {
            AuthRequestModel requestModel = new AuthRequestModel();
            requestModel.setUserName("username");
            requestModel.setPassword("password");
            HttpPost request = new HttpPost("url");
            request.addHeader(CONTENT, JSON);
            request.setEntity(new StringEntity(JsonObjectUtils.getJsonFromObject(requestModel)));
            final HttpResponse response = getHttpClient().execute(request);
            if (isHttpPostOk(response)) {
                toReturn = (AuthResponseModel) JsonObjectUtils.getObjectFromJson(EntityUtils.toString(response.getEntity()),
                        AuthResponseModel.class);
            }
        } catch (Exception e) {
            
        }
        return toReturn;
    }

    public CloseableHttpClient getHttpClient() {
        return HttpClientBuilder.create().disableAutomaticRetries().setDefaultRequestConfig(
                RequestConfig.custom().setCookieSpec(CookieSpecs.STANDARD).setConnectTimeout(10000).setSocketTimeout(10000).build()).build();
    }

}
I removed the instance of "CloseableHttpClient" as an attribute of the class and simply created a publicly accessible method (to access it from all my controllers).

Have a good day,

Alexandre
Posts: 164
Joined: Thu Dec 12, 2019 11:08 am

Re: Use API with JAVA

Post by Alexandre » Wed Feb 26, 2020 9:10 am

Good job. We are glad that the issue was solved.

luodashuai
Posts: 4
Joined: Wed May 27, 2020 4:01 am

Re: Use API with JAVA

Post by luodashuai » Wed May 27, 2020 8:38 am

hello
I have encountered the same problem and do not know how to solve it. If you have solved it, would you please provide some solutions

Alexandre
Posts: 164
Joined: Thu Dec 12, 2019 11:08 am

Re: Use API with JAVA

Post by Alexandre » Wed Jun 03, 2020 8:09 am

Hello luodashuai.
Please describe your issue in more detail. What kind of error do you have?

Post Reply