내 커뮤니티 업적 표시줄을 확대합니다.

Submissions are now open for the 2026 Adobe Experience Maker Awards.

Mark Solution

활동이 없어 이 대화는 잠겼습니다. 새 게시물을 작성해 주세요.

해결됨

How to invoke 3rd part REST API using AEM sling servlet

Avatar

Level 5

Hi,

 

I have a requirement to invoke the client service using rest api. 3 different post is used to insert a data, read the data and delete the data and the params accordingly. I need to get the response in json format when we are reading the data and a success msg for insert and delete. How to invoke the client service and get the response using the url, params, headers(api key)?

 

CloseableHttpClient client = HttpClients.createDefault();
HttpPost httpPost = new HttpPost(siteService.getAwsUrl());
List<NameValuePair> params = new ArrayList<>();
params.add(new BasicNameValuePair("userid", userId));
params.add(new BasicNameValuePair(URL, bookmarkUrl));
params.add(new BasicNameValuePair(ACTION, action));
params.add(new BasicNameValuePair(SITECODE, siteCode));
params.add(new BasicNameValuePair(CATEGORY, category));
params.add(new BasicNameValuePair(TITLE, title));
httpPost.setEntity(new UrlEncodedFormEntity(params));
List<Header> headerList = siteService.getHeaders();
Header[] headers = headerList.toArray(new Header[0]);
httpPost.setHeaders(headers);
CloseableHttpResponse jsonResponse = client.execute(httpPost);

the response is giving status code as 400. 

 

Thank you

1 채택된 해결책 개

Avatar

정확한 답변 작성자:
Community Advisor

You can write HTTP connection logic in same way as we do in JAVA. If your logic is working fine in JAVA class, it work fine in AEM servlet as well.

400 Error response code indicates client error. Please try same in Postman first and try to build same request in JAVA class using APIs. Once it looks fine you can create a servlet in AEM.


Document to write servlet : aem.redquark.org/2018/10/day-05-working-with-sling-servlets-in_10.html

 

Sample code to send response in JSON in servlet :

Employee employee = new Employee(1, "Karan", "IT", 5000);
String employeeJsonString = this.gson.toJson(employee);
PrintWriter out = response.getWriter();
response.setContentType("application/json");
response.setCharacterEncoding("UTF-8");
out.print(employeeJsonString);
out.flush();   

원본 게시물의 솔루션 보기

2 답변 개

Avatar

정확한 답변 작성자:
Community Advisor

You can write HTTP connection logic in same way as we do in JAVA. If your logic is working fine in JAVA class, it work fine in AEM servlet as well.

400 Error response code indicates client error. Please try same in Postman first and try to build same request in JAVA class using APIs. Once it looks fine you can create a servlet in AEM.


Document to write servlet : aem.redquark.org/2018/10/day-05-working-with-sling-servlets-in_10.html

 

Sample code to send response in JSON in servlet :

Employee employee = new Employee(1, "Karan", "IT", 5000);
String employeeJsonString = this.gson.toJson(employee);
PrintWriter out = response.getWriter();
response.setContentType("application/json");
response.setCharacterEncoding("UTF-8");
out.print(employeeJsonString);
out.flush();   

Avatar

Community Advisor