Question

I have portlet controller response mapping defined like so :

@ResourceMapping("create")
public String create(ResourceRequest request, ResourceResponse response){

    return "test"

}

I'm trying to forard a new request to a seperate service using :

@ResourceMapping("create")
public String create(ResourceRequest request, ResourceResponse response){

    new HttpGet("/path-to-my-service/myService");

    return "test"

}

No errors are displayed but i think the Get request is not being invoked correctly because I do not have access to the domain name that the serice resides on. Can this be accessed using the ResourceRequest/ResourceResponse ? Looking at the properties of this obj's these properties do not seem to be available. Is there something else I can try to invoke the Get request ?

I can get this to work on the browser by providing the full path :

http://localhost:8080/path-to-my-service/myService But I need to resolve the domain of the service from within the @ResourceMapping ?

Était-ce utile?

La solution

No errors are displayed

It shouldn't, this code do nothing

new HttpGet("/path-to-my-service/myService");

I suppose you trying to call external services with http client, you must provide full http call code to get it

thomething lik this, example for http client 4.3

    public static String executeGet(String url) {

        CloseableHttpClient httpClient = HttpClients.createDefault();
        CloseableHttpResponse response = null;
        String resp = null;  
        try {

            HttpGet httpGet = new HttpGet(url);
            response = httpClient.execute(httpGet);

            int code = response.getStatusLine().getStatusCode();
            if (code == HttpURLConnection.HTTP_OK) {
                resp = EntityUtils.toString(response.getEntity());
            } else {
                // handle wrong response here
            }
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            if (response != null) {
                close(response);
            }
        }
        return resp;
}

You should provide full url to this method, this means you should know host name. If you know that your portlet and your services on the same host you can get host name from ServletRequest, every portal implementation provide approach to get HttpServletRequest from PorteltRequest

Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top