如何使用参数初始化 Java EE 5 JAX-WS 2.0 Web 服务

发布于 2024-09-17 23:48:46 字数 1839 浏览 4 评论 0原文

应用程序配置:

  • Web 应用程序使用 java 第一种方法通过注释创建 JAX-WS 2.0 Web 服务。
  • WebLogic 10.3

我的要求

我的要求是部署单个 Web 服务实现类,但根据访问服务的 URL 更改逻辑。

问题: 我假设一个好方法是在 web.xml 中部署不同的映射并使用不同的参数初始化它们。有更好的办法吗?

关闭访问 Web 服务的 URL 的逻辑的最佳方法是什么?我应该尝试使用初始化参数在 web.xml 中配置两个 servlet 映射(尝试过,但无法使其工作),还是应该解析服务实现中的 URL?还有其他选择吗?

我尝试过的(但没有成功)

我尝试在 中添加 web.xml 中的元素。但是,无法访问 Web 服务内的 ServletConfig 对象来检索参数。 Web 服务不具备标准 Servlet 的所有功能(即使我实现了 ServletServletContextListener)。我只能访问 WebServiceContext (看起来),从那里我只能获取 元素 - 但我需要 < init-param> 元素代替。

在 web.xml 中,我使用相同的 Java 类输入两个 元素,但它们映射到两个不同的 URL,如下所示。请注意每个 Servlet 映射中的“source”参数有何不同。

<servlet>
    <servlet-name>Foo</servlet-name>
    <servlet-class>com.Foo</servlet-class>
    <init-param>
        <param-name>source</param-name>
   <param-value>1</param-value>
    </init-param>
</servlet>
<servlet-mapping>
    <servlet-name>Foo</servlet-name>
    <url-pattern>/Foo</url-pattern>
</servlet-mapping>

<servlet>
    <servlet-name>Bar</servlet-name>
    <servlet-class>com.Foo</servlet-class>
   <init-param>
        <param-name>source</param-name>
   <param-value>2</param-value>
    </init-param>
</servlet>
<servlet-mapping>
    <servlet-name>Bar</servlet-name>
    <url-pattern>/Bar</url-pattern>
</servlet-mapping>  

Application configuration:

  • Web application using java first method of creating JAX-WS 2.0 Web Services with annotations.
  • WebLogic 10.3

My Requirements

The requirements I have are to deploy a single web service implementation class, but change logic based on the URL from which the service was accessed.

Question:
I'm assuming a good way to do this is to deploy different mappings in web.xml and initialize them with different parameters. Is there a better way?

What is the best way to switch logic off the URL from which the web service was accessed? Should I try to configure two servlet mappings in web.xml with initialization parameters (tried, but couldn't get it to work), or should I parse the URL in the service impl? Any other alternatives?

What I've Tried (but didn't work)

I have tried adding the <init-param> in the <servlet> element in web.xml. However, can't get to the ServletConfig object inside the web service to retrieve the param. The web service does not have all the functionality of a standard Servlet (even if I implement Servlet or ServletContextListener). I only have access to the WebServiceContext (it seems) and from there I can only get <context-param> elements--but I would need <init-param> elements instead.

In web.xml, I enter two <servlet> elements using the same Java class, but which map to two different URLs as follows. Notice how the "source" param is different in each Servlet mapping.

<servlet>
    <servlet-name>Foo</servlet-name>
    <servlet-class>com.Foo</servlet-class>
    <init-param>
        <param-name>source</param-name>
   <param-value>1</param-value>
    </init-param>
</servlet>
<servlet-mapping>
    <servlet-name>Foo</servlet-name>
    <url-pattern>/Foo</url-pattern>
</servlet-mapping>

<servlet>
    <servlet-name>Bar</servlet-name>
    <servlet-class>com.Foo</servlet-class>
   <init-param>
        <param-name>source</param-name>
   <param-value>2</param-value>
    </init-param>
</servlet>
<servlet-mapping>
    <servlet-name>Bar</servlet-name>
    <url-pattern>/Bar</url-pattern>
</servlet-mapping>  

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

请帮我爱他 2024-09-24 23:48:46

您很可能已经这样做了,但是您是否尝试在运行时使用 MessageContext 来确定源是什么?

@WebService
public class CalculatorService implements Calculator
{

    @Resource
    private WebServiceContext context;

    @WebMethod
    public void getCounter()
    {
        MessageContext mc = wsContext.getMessageContext();
        // you can grab the HttpSession
        HttpSession session = (HttpServletRequest)mc.get(MessageContext.SERVLET_REQUEST)).getSession();
        // ...or maybe the path info is enough
        String path = mc.get(MessageContext.PATH_INFO);
        // the query itself should almost definitely be enough
        String query = (String) mc.get(MessageContext.QUERY_STRING);
    }

}

我从 http://sirinsevinc.wordpress.com/category/jaxws/ 得到了这个想法。不过还没有尝试过。

You very well may have, but did you try using MessageContext at runtime to determine what the source is?

@WebService
public class CalculatorService implements Calculator
{

    @Resource
    private WebServiceContext context;

    @WebMethod
    public void getCounter()
    {
        MessageContext mc = wsContext.getMessageContext();
        // you can grab the HttpSession
        HttpSession session = (HttpServletRequest)mc.get(MessageContext.SERVLET_REQUEST)).getSession();
        // ...or maybe the path info is enough
        String path = mc.get(MessageContext.PATH_INFO);
        // the query itself should almost definitely be enough
        String query = (String) mc.get(MessageContext.QUERY_STRING);
    }

}

I got the idea from http://sirinsevinc.wordpress.com/category/jaxws/. Haven't tried it, though.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文