首页 \ 问答 \ 无法捕获RESTful Web服务上的请求(Unable to capture request on a RESTful Web service)

无法捕获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
更新时间:2023-11-13 11:11

最满意答案

我建议:

=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.

相关问答

更多

相关文章

更多

最新问答

更多
  • 获取MVC 4使用的DisplayMode后缀(Get the DisplayMode Suffix being used by MVC 4)
  • 如何通过引用返回对象?(How is returning an object by reference possible?)
  • 矩阵如何存储在内存中?(How are matrices stored in memory?)
  • 每个请求的Java新会话?(Java New Session For Each Request?)
  • css:浮动div中重叠的标题h1(css: overlapping headlines h1 in floated divs)
  • 无论图像如何,Caffe预测同一类(Caffe predicts same class regardless of image)
  • xcode语法颜色编码解释?(xcode syntax color coding explained?)
  • 在Access 2010 Runtime中使用Office 2000校对工具(Use Office 2000 proofing tools in Access 2010 Runtime)
  • 从单独的Web主机将图像传输到服务器上(Getting images onto server from separate web host)
  • 从旧版本复制文件并保留它们(旧/新版本)(Copy a file from old revision and keep both of them (old / new revision))
  • 西安哪有PLC可控制编程的培训
  • 在Entity Framework中选择基类(Select base class in Entity Framework)
  • 在Android中出现错误“数据集和渲染器应该不为null,并且应该具有相同数量的系列”(Error “Dataset and renderer should be not null and should have the same number of series” in Android)
  • 电脑二级VF有什么用
  • Datamapper Ruby如何添加Hook方法(Datamapper Ruby How to add Hook Method)
  • 金华英语角.
  • 手机软件如何制作
  • 用于Android webview中图像保存的上下文菜单(Context Menu for Image Saving in an Android webview)
  • 注意:未定义的偏移量:PHP(Notice: Undefined offset: PHP)
  • 如何读R中的大数据集[复制](How to read large dataset in R [duplicate])
  • Unity 5 Heighmap与地形宽度/地形长度的分辨率关系?(Unity 5 Heighmap Resolution relationship to terrain width / terrain length?)
  • 如何通知PipedOutputStream线程写入最后一个字节的PipedInputStream线程?(How to notify PipedInputStream thread that PipedOutputStream thread has written last byte?)
  • python的访问器方法有哪些
  • DeviceNetworkInformation:哪个是哪个?(DeviceNetworkInformation: Which is which?)
  • 在Ruby中对组合进行排序(Sorting a combination in Ruby)
  • 网站开发的流程?
  • 使用Zend Framework 2中的JOIN sql检索数据(Retrieve data using JOIN sql in Zend Framework 2)
  • 条带格式类型格式模式编号无法正常工作(Stripes format type format pattern number not working properly)
  • 透明度错误IE11(Transparency bug IE11)
  • linux的基本操作命令。。。