嗨,我有一个从 Liferay 的 buildServices 生成的 webService.. 该方法如下所示
public User getUserTest(long userId) {
User u = null;
try {
Token token = OAuthFactoryUtil.createToken("sasa", "sdad");
} catch (OAuthException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
try {
u = UserLocalServiceUtil.getUser(userId);
System.out.println("xx user " + u.getScreenName());
} catch (Exception e) {
System.out.println(" Exception ************* " + e.toString());
}
return u;
}
此 ws 中的参数如下:
http://localhost:8080/demo-portlet/api/json?serviceClassName=com.sample.portlet.library.service.BookServiceUtil&serviceMethodName=getUserTest&userId=10195&serviceParameters=[userId]
将userId
作为参数..
如果您需要HttpServletRequest
,您将如何传递参数..我的方法如下所示
public User getUserTest(HttpServletRequest httpRequest) {
User u = null;
try {
String version = httpRequest.getHeader("X-PHM-APP-VERSION");
Token token = OAuthFactoryUtil.createToken("sasa", "sdad");
} catch (OAuthException e1) {
e1.printStackTrace();
}
try {
String authorization = httpRequest.getHeader("Authorization");
u = UserLocalServiceUtil.getUser(Long.valueOf(authorization));
System.out.println("authorization --> " + authorization);
System.out.println("xx user " + u.getScreenName());
} catch (Exception e) {
System.out.println(" Exception ************* " + e.toString());
}
return u;
}
我需要HttpServletRequest
从 header 获取参数,而不是通过url传递。有没有更好的方法从 header 获取参数?感谢您的帮助
最佳答案
我认为 Web 服务层通常处于后期阶段,您永远不会传递请求。理想情况下,您要做的是将 header 参数传递给网络服务而不是请求
关于web-services - 如何在 Liferay WebService 中获取 HttpServletRequest,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11149350/