无法捕获RESTful Web服务上的请求(Unable to capture request on a RESTful Web service)
我是Web服务的新手,并且已经对它进行了大量的研究,但我目前无法捕获我需要用于身份验证的请求标头。
这是一个场景:我在我的JAX-RS RESTful Web服务上调用了一个login()方法,在向请求标头添加用户名/密码组合之后,我有一个Android客户端。 然后,Web服务(在JBoss AS 7.1上运行)应该捕获这些头。
问题是每次调用此login()方法时,请求似乎尚未得到服务,因此在执行WebServiceContext.getMessageContext()时会导致IllegalStateException。
我已经尝试将@PostConstruct注释添加到方法的标题中,但无济于事。 当我这样做时,该方法似乎根本没有初始化。 相反,我在调用它时有一个ClassNotFoundException。
我怎样才能解决这个问题? 我已经坚持了几天,并在客户端和Web服务上尝试了多种不同的方法来捕获这些标题,但它们要么不适用于项目的体系结构,要么根本无法按预期工作。
这是服务的界面:
@ApplicationPath("/apppath") @Path("/wspath") public interface LoginService { @GET @Path("/login") @Produces(MediaType.APPLICATION_JSON) public String login(); }
这是服务的实现:
@Stateless @Local(LoginService.class) public class LoginServiceImpl implements LoginService { @Resource WebServiceContext wsContext; @Override public String login() { // This line throws an IllegalStateException. MessageContext msgContext = wsContext.getMessageContext(); // TODO: Capture authentication data from headers. @SuppressWarnings("unchecked") Map<String, List<String>> headers = (Map<String, List<String>>) msgContext.get(MessageContext.HTTP_REQUEST_HEADERS); // Dummy return for testing purposes. return "It works" + headers.toString(); } }
这是一个简单的JUnit测试代码,我正在努力尝试在客户端修复问题。
public class LoginServiceTest { @Test public void test() { String mEmail = "a@a.com"; String mPassword = "aaaa"; HttpGet request = new HttpGet("http://localhost:8080/apppath/wspath/login"); String auth = Base64.encodeBytes((mEmail + ":" + mPassword).getBytes()); request.addHeader("Authorization", "Basic " + auth); HttpClient httpClient = new DefaultHttpClient(); try { InputStream inputStream = httpClient.execute(request).getEntity().getContent(); BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(inputStream)); String wsReturn; while ((wsReturn = bufferedReader.readLine()) != null) { System.out.println(wsReturn); } } catch (IllegalStateException e) { e.printStackTrace(); } catch (ClientProtocolException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } } }
I'm new to web services in general and have been doing a lot of research on it, but I'm currently unable to capture request headers I need for authentication purposes.
Here's the scenario: I have an Android client calling a login() method on my JAX-RS RESTful web service, after adding a username/password combination to the request headers. The web service (which runs on JBoss AS 7.1) should then capture those headers.
The problem is everytime this login() method is called, the request seems to have not being serviced yet, thus leading to an IllegalStateException upon executing WebServiceContext.getMessageContext().
I've tried adding @PostConstruct annotation to the method's header, but to no avail. When I do this the method doesn't seem to be initialized at all. Instead I have a ClassNotFoundException upon calling it.
How can I get through this issue? I've been stuck on this for days and tried multiple different approaches both at the client and the web service to capture those headers, but they would either not apply to the project's architecture or simply not work as expected.
Here's the service's interface:
@ApplicationPath("/apppath") @Path("/wspath") public interface LoginService { @GET @Path("/login") @Produces(MediaType.APPLICATION_JSON) public String login(); }
Here's the service's implementation:
@Stateless @Local(LoginService.class) public class LoginServiceImpl implements LoginService { @Resource WebServiceContext wsContext; @Override public String login() { // This line throws an IllegalStateException. MessageContext msgContext = wsContext.getMessageContext(); // TODO: Capture authentication data from headers. @SuppressWarnings("unchecked") Map<String, List<String>> headers = (Map<String, List<String>>) msgContext.get(MessageContext.HTTP_REQUEST_HEADERS); // Dummy return for testing purposes. return "It works" + headers.toString(); } }
And here's a simple JUnit test code I'm working on to try to fix the problem on client side.
public class LoginServiceTest { @Test public void test() { String mEmail = "a@a.com"; String mPassword = "aaaa"; HttpGet request = new HttpGet("http://localhost:8080/apppath/wspath/login"); String auth = Base64.encodeBytes((mEmail + ":" + mPassword).getBytes()); request.addHeader("Authorization", "Basic " + auth); HttpClient httpClient = new DefaultHttpClient(); try { InputStream inputStream = httpClient.execute(request).getEntity().getContent(); BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(inputStream)); String wsReturn; while ((wsReturn = bufferedReader.readLine()) != null) { System.out.println(wsReturn); } } catch (IllegalStateException e) { e.printStackTrace(); } catch (ClientProtocolException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } } }
原文:https://stackoverflow.com/questions/14433525
最满意答案
我建议:
=vlookup(1E+100,J2:J32,1)
虽然如果J2:J32中的任何数字都大于或大于:这可能会给出错误的答案:
10,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000。
假设J2和J32之间包括搜索范围内的J2和J32。
I suggest:
=vlookup(1E+100,J2:J32,1)
though this could give an incorrect answer if any of the numbers in J2:J32 are as big or bigger than:
10,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000.
The assumption is that between J2 and J32 includes both J2 and J32 in the search range.
相关问答
更多-
尝试这个: import csv with open("ooo.csv", "r") as f: reader = csv.reader(f) for line_num, content in enumerate(reader): if content[0] == "scenario1": print content, line_num + 1 或者没有csv模块: with open("ooo.csv") as f: for l, i in ...
-
Excel中如何查找1列中的值存在于另一个值的范围内(Excel how to find values in 1 column exist in the range of values in another)[2023-06-26]
这就是你所需要的: =NOT(ISERROR(MATCH(, | , 0))) ## pseudo code 对于A的第一个单元格,这将是: =NOT(ISERROR(MATCH(A2,$B$2:$B$5, 0))) 输入公式(并向下拖动),如下所示: 你会得到: This is what you need: =NOT(ISERROR(MATCH( , | , 0))) ## pseudo code For ... -
此查询将为您提供一个电子邮件地址列表,并使用最多使用的地址。 select email, count(*) as c from table group by email having c > 1 order by c desc 如果你想要完整的行: select * from table where email in ( select email from table group by email having count(*) > 1 ) This query will give yo ...
-
你可以使用$notLike运算符吗? 也许: $notLike: { $any: ['cat', 'hat']} 在运营商下面看看! http://docs.sequelizejs.com/manual/tutorial/querying.html 你可能看起来像这样: where: { id: { $notLike: { $any: GLIDs } } }, Can you use the $notLike operator? Perhaps: $notLike: { $ ...
-
您可以尝试以下公式(注意:这是一个数组公式,您需要使用Ctrl + Shift + Enter才能正常工作而不是单独输入 ): =INDEX(B:B,LARGE(IF(B:B<>"",ROW(B:B)),2)) 将最后的2更改为3以从最后的非空值中获取第3个。 即使非空值之间的列中存在空白,上述公式仍然有效。 IF(B:B<>"",ROW(B:B))基本上取非空行的行号,然后LARGE根据最后使用的值选择最大或第二大等,以获取你正在寻找的第n行。 INDEX只从该行中选择值。 You can try th ...
-
定义动态命名范围(通过公式功能区| 定义名称作为名称 )。 使用如下公式: =OFFSET($A$3,1,0,COUNTIF($A$3:$A$1000,"<="&TODAY())) 您可以通过从名称管理器中选择它来检查它是否正确,您将看到代表此日期范围的选定单元格<=今天的日期: 然后,为浏览器添加另一个Name (并重复iOS等),如: =OFFSET(released_dates,0,1) 然后,您可以参考命名范围browser_releases ,而不是在Lookup公式中使用整个列B. =LOO ...
-
尝试这个 SELECT * FROM table_name WHERE column_name NOT LIKE ('%.%'); 它将获取所有不包含“。”的行。 文中的任何地方 Try this SELECT * FROM table_name WHERE column_name NOT LIKE ('%.%'); it will fetch all the rows which does not contain "." anywhere in the text
-
在多列中查找值(Find Value in multiple columns)[2022-06-22]
以下是我如何设置表格,以便您可以参考公式指向的位置: 我使用的公式: =INDIRECT(ADDRESS(MATCH(B$19,IF($A$1:$A$15 = $A21,$B$1:$B$15),0),MATCH(B$20,$A$1:$D$1,0))) 这是一个数组公式,需要用ctrl-shift-Enter确认。 您需要设置标头和ID。 但后来我在B21中添加了公式。 通过Ctrl-Shift-Enter确认。 然后上下拖动它。 Here is how I Set up the table so you ... -
在列中查找最后一个值(Find last value in column)[2022-01-26]
我建议: =vlookup(1E+100,J2:J32,1) 虽然如果J2:J32中的任何数字都大于或大于:这可能会给出错误的答案: 10,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000。 假设J2和J32之间包括搜索范围内的J2和J32。 I suggest: =vlookup(1E+1 ... -
在列中查找n个最大值(Find n largest values in a column)[2022-05-20]
在MySql中,您可以使用[LIMIT {[offset,] row_count }]来执行此操作: ... ORDER BY SomeField DESC LIMIT @n; 对于SQL Server,您可以使用TOP(n)获取前n: SELECT TOP(@n) SomeFieldName FROM TABLE ORDER BY SomeField DESC 例如: SELECT TOP 5 items_sold FROM tbl_PRODUCT ORDER BY items_sold dESC ...