注入调用方ip地址和端口在java restful web服务中不工作

xggvc2p6  于 2021-07-03  发布在  Java
关注(0)|答案(2)|浏览(353)

我正在尝试获取客户端的ip地址和端口,但在部署代码时,它会提供以下输出:

May 10, 2016 2:50:56 PM com.sun.jersey.spi.inject.Errors processErrorMessages
SEVERE: The following errors and warnings have been detected with resource and/or provider classes:
SEVERE: Missing dependency for method public javax.ws.rs.core.Response resources.Login.loginUser(java.lang.String,javax.servlet.http.HttpServletRequest) at parameter at index 1
SEVERE: Method, public javax.ws.rs.core.Response resources.Login.loginUser(java.lang.String,javax.servlet.http.HttpServletRequest), annotated with POST of resource, class resources.Login, is not recognized as valid resource method.
Exception in thread "main" com.sun.jersey.spi.inject.Errors$ErrorMessagesException
at com.sun.jersey.spi.inject.Errors.processErrorMessages(Errors.java:170)
at com.sun.jersey.spi.inject.Errors.postProcess(Errors.java:136)
at com.sun.jersey.spi.inject.Errors.processWithErrors(Errors.java:199)
at com.sun.jersey.server.impl.application.WebApplicationImpl.initiate(WebApplicationImpl.java:773)
at com.sun.jersey.api.container.ContainerFactory.createContainer(ContainerFactory.java:172)
at com.sun.jersey.api.container.ContainerFactory.createContainer(ContainerFactory.java:264)
at com.sun.jersey.api.container.ContainerFactory.createContainer(ContainerFactory.java:246)
at com.sun.jersey.api.container.httpserver.HttpServerFactory.create(HttpServerFactory.java:117)
at com.sun.jersey.api.container.httpserver.HttpServerFactory.create(HttpServerFactory.java:92)
at gateway.GatewayRestServer.main(GatewayRestServer.java:30)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:606)
at com.intellij.rt.execution.application.AppMain.main(AppMain.java:144)

这是我的rest服务器中的代码

import javax.servlet.http.HttpServletRequest;
import javax.ws.rs.*;
import javax.ws.rs.core.Context;
import javax.ws.rs.core.MediaType;
import javax.ws.rs.core.Response;

...

@POST
@Path("/post")
@Consumes(MediaType.TEXT_PLAIN)
public Response loginUser(String username, @Context HttpServletRequest request) {
    Loog.i(this, "Login di " + username);
    String netIP = request.getRemoteAddr();
    int netPort = request.getRemotePort();
    ...
}

如果我删除@context httpservletrequest,它就可以正常工作。

ycl3bljg

ycl3bljg1#

您不需要在参数中传递@context。。
这是我的界面:

@POST
@Path("/lookup")
@Consumes(MediaType.APPLICATION_JSON)
@Produces(MediaType.APPLICATION_JSON)
List<Client> searchClient(Criterias cr);

这是我的课:

@Stateless
public class XServiceImpl implements XService {

    @Context
    private HttpServletRequest request;

    @Override
    @Public
    public List<Client> searchClient(Criterias cr) {

        try {
            List<Client> baux = null;
            String lang = RequestContext.getLocale(request).getLanguage();
            cr.setLang(lang);

        } catch (SQLException e) {
            LOGGER.log(Level.SEVERE, e.getLocalizedMessage());
            throw new ServiceException();
        } finally {
            reqInfo.logExecutionTime();
        }

    }
3lxsmp7m

3lxsmp7m2#

好吧,我想出来了:

@Context
private HttpContext request;

@POST
@Consumes(MediaType.TEXT_PLAIN)
public Response loginUser(String username) {

    String netIP = request.getRequest().getRequestUri().getHost().toString();
    int netPort = request.getRequest().getRequestUri().getPort();
    ...
}

注入对象的类型错误,我使用了com.sun.jersey.api.core.httpcontext;相反。

相关问题