liferay
Liferay에서 안정적인 웹 서비스 사용하기
수색…
Liferay JSON 서비스를 사용하여 GET 요청 사용
Liferay는 JSON을 통해 다른 시스템에서 사용할 수있는 많은 기본 및 맞춤 서비스를 제공합니다. 특정 liferay 인스턴스의 서비스를 탐색하려면 주어진 URL (이 경우 로컬 인스턴스)을 사용하십시오.
http://localhost:8080/api/jsonws/
필요한 서비스를 선택하고 주어진 구문과 매개 변수로 서비스를 사용하십시오.
/user/get-user-by-email-address
companyId and emailAddress 를 사용하여 예상 데이터 유형뿐만 아니라 소비자가 처리 할 수있는 예외가있는 사용자를 검색합니다.
다음 예는 포틀릿에서이 서비스를 사용합니다. 주어진 유틸리티 클래스 메쏘드는 필요한 인자들을 전달하면서 webservice를 호출합니다 :
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.URL;
import org.json.simple.JSONObject;
import org.json.simple.parser.JSONParser;
import org.json.simple.parser.ParseException;
import sun.misc.BASE64Encoder;
import com.liferay.portal.kernel.util.StringUtil;
import com.liferay.portal.theme.ThemeDisplay;
public class WebServiceUtil {
public static String requestWebService(ThemeDisplay themeDisplay) {
String url="http://localhost:8080/api/jsonws/user/get-user-by-email-address/company-id/{company-id}/email-address/{email-address}";
String groupId= Long.toString(themeDisplay.getCompanyId());
String userEmail="[email protected]";
String[] searchList={"{company-id}","{email-address}"};
String[] replList={groupId,userEmail};
//Path params are replaced with args to make web service call
url=StringUtil.replace(url, searchList, replList);
System.out.println(url);
StringBuilder sb = new StringBuilder();
JSONObject jsonObject=new JSONObject();
try
{
URL urlVal = new URL(url);
HttpURLConnection conn = (HttpURLConnection) urlVal.openConnection();
//The user credentials are directly used here only for the purpose of example,always fetech these details from an external props file.
String uname ="[email protected]";
String pswd="test";
String authStr=uname+":"+pswd;
//Encoding username+pswd to be added to request header for making web service call
String authStrEnc=new BASE64Encoder().encode(authStr.getBytes());
/*Authorization type is set to consume web service
and encoded combination is set in header to autheticate caller*/
conn.setRequestMethod("GET");
conn.setRequestProperty("Accept", "application/json");
conn.setRequestProperty("Authorization", "Basic "+authStrEnc);
BufferedReader brf = new BufferedReader(new InputStreamReader(conn.getInputStream()));
JSONParser json=new JSONParser();
jsonObject=(JSONObject)json.parse(brf);
int cp;
while ((cp = brf.read()) != -1) {
sb.append((char) cp);
}
}
catch(IOException e)
{
System.out.println("Something went wrong while reading/writing in stream!!");
}
catch (ParseException e) {
System.out.println("Parse error");
}
//For purpose of simplicity we have fetched one of the fields from JSON response
return (String)jsonObject.get("firstName");
}
}
Modified text is an extract of the original Stack Overflow Documentation
아래 라이선스 CC BY-SA 3.0
와 제휴하지 않음 Stack Overflow
