Spring WS(DefaultWsdl11Definition)HTTP状态代码,带有无效 [英] Spring WS (DefaultWsdl11Definition) HTTP status code with void

查看:81
本文介绍了Spring WS(DefaultWsdl11Definition)HTTP状态代码,带有无效的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我们有一个基于Spring WS的(正在运行的)SOAP Web服务,具有 DefaultWsdl11Definition .

We have a (working) SOAP web service based on Spring WS with DefaultWsdl11Definition.

基本上是这样的:

@Endpoint("name")
public class OurEndpoint {

    @PayloadRoot(namespace = "somenamespace", localPart = "localpart")
    public void onMessage(@RequestPayload SomePojo pojo) {
        // do stuff
    }
}

它在Spring中连接,并且正在正确处理我们所有的SOAP请求.唯一的问题是该方法返回 202 Accepted .这不是呼叫者想要的,他宁愿让我们返回 204 No Content (或者如果不可能的话,则返回空的 200 OK ).

It is wired in Spring and it is correctly processing all of our SOAP requests. The only problem is that the method returns a 202 Accepted. This is not what the caller wants, he'd rather have us return 204 No Content (or if that is not possible an empty 200 OK).

我们的其他端点具有有效的响应对象,并且确实返回 200 OK .当 204 可能更合适时,似乎是导致 202 的原因?

Our other endpoints have a valid response object, and do return 200 OK. It seems void causes 202 when 204 might be more appropriate?

是否可以在Spring WS中更改响应代码?我们似乎找不到正确的方法.

Is it possible to change the response code in Spring WS? We can't seem to find the correct way to do this.

我们尝试过但不起作用的事情:

Things we tried and didn't work:

  • 将返回类型更改为:
    • HttpStatus.NO_CONTENT
    • org.w3c.dom.Element<-不被接受

    有什么想法吗?

    推荐答案

    找到合适的解决方案时,我们遇到了一些丑陋的问题:

    When finding a proper solutions we've encountered some ugly problems:

    • 创建自定义适配器/拦截器是有问题的,因为当您没有响应(无效)时,Spring不会调用 handleResponse 方法
    • 手动设置状态代码不起作用,因为 HttpServletConnection 保留了一个布尔值 statusCodeSet ,该布尔值不会被更新
    • Creating custom adapters/interceptors is problematic because the handleResponse method isn't called by Spring when you don't have a response (void)
    • Manually setting the status code doesn't work because HttpServletConnection keeps a boolean statusCodeSet which doesn't get updated

    但是幸运的是,我们通过以下更改设法使它起作用:

    But luckily we managed to get it working with the following changes:

    /**
     * If a web service has no response, this handler returns: 204 No Content
     */
    public class NoContentInterceptor extends EndpointInterceptorAdapter {
    
        @Override
        public void afterCompletion(MessageContext messageContext, Object o, Exception e) throws Exception {
            if (!messageContext.hasResponse()) {
                TransportContext tc = TransportContextHolder.getTransportContext();
                if (tc != null && tc.getConnection() instanceof HttpServletConnection) {
                    HttpServletConnection connection = ((HttpServletConnection) tc.getConnection());
                    // First we force the 'statusCodeSet' boolean to true:
                    connection.setFaultCode(null);
                    // Next we can set our custom status code:
                    connection.getHttpServletResponse().setStatus(204);
                }
            }
        }
    }
    

    接下来,我们需要注册此拦截器,这可以使用Spring的XML轻松完成:

    Next we need to register this interceptor, this can be easily done using Spring's XML:

    <sws:interceptors>
        <bean class="com.something.NoContentInterceptor"/>
    </sws:interceptors>
    

    非常感谢@ m-deinum为我们指明了正确的方向!

    A big thanks to @m-deinum for pointing us in the right direction!

    这篇关于Spring WS(DefaultWsdl11Definition)HTTP状态代码,带有无效的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆