liferay
Korzystanie z usługi Restful Web w Liferay
Szukaj…
Skorzystaj z usługi JSON Liferay dla żądań GET
Liferay udostępnia wiele domyślnych i niestandardowych usług dostępnych dla innych systemów za pośrednictwem JSON. Aby przeglądać usługi w określonej instancji dożywotniej, użyj podanego adresu URL - w tym przypadku instancja lokalna:
http://localhost:8080/api/jsonws/
Wybierz wymaganą usługę, skorzystaj z usługi o podanej składni i parametrach:
/user/get-user-by-email-address
Użyj companyId and emailAddress aby pobrać użytkownika z oczekiwanymi typami danych, a także możliwymi wyjątkami, które powinien companyId and emailAddress konsument.
Poniższy przykład wykorzystuje tę usługę z portletu. Podana metoda klasy narzędziowej wywołuje usługę WWW, przekazując niezbędne argumenty:
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
Licencjonowany na podstawie CC BY-SA 3.0
Nie związany z Stack Overflow
