Spring Boot WS-Server - 自定义 Http 状态 [英] Spring Boot WS-Server - Custom Http Status

查看:51
本文介绍了Spring Boot WS-Server - 自定义 Http 状态的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用 Spring Boot WS-Server 发布端点

当我使用 SoapUI 时,我看到:

HTTP/1.1 200接受:text/xml、text/html、image/gif、image/jpeg、*;q=.2, /;q=.2SOAPAction:"内容类型:text/xml;charset=utf-8内容长度:828日期:2021 年 4 月 29 日星期四 14:04:54 GMT保持活动:超时 = 60连接:保持活动

我想设置自定义 HTTP 状态作为响应(我知道这可能违反标准,但这是外部要求).我还阅读了以下主题:

Spring WS (DefaultWsdl11Definition) HTTP 状态码与 void

但是这个解决方案失败了

Spring Boot 版本:2.2.7

解决方案

问题已解决

正如我所说,我想在 SOAP 响应中设置自定义 HTTP 状态.

我找到了这个帖子:Spring WS (DefaultWsdl11Definition) HTTP 状态码与 void

作者使用 EndpointInterceptor 和 TransportContext 来获取 HttpServletResponse,然后他改变了状态.我和他的情况的不同之处在于,他从 WebService 方法返回 void 而我想返回一些响应.

在我的情况下,Spring WebServiceMessageReceiverObjectSupport 类(方法 handleConnection)中的以下代码覆盖了先前在拦截器中设置的 servlet 状态:

if (response instanceof FaultAwareWebServiceMessage && connection instanceof FaultAwareWebServiceConnection) {FaultAwareWebServiceMessage faultResponse = (FaultAwareWebServiceMessage)response;FaultAwareWebServiceConnection faultConnection = (FaultAwareWebServiceConnection)connection;faultConnection.setFaultCode(faultResponse.getFaultCode());}

为了绕过这段代码,我需要用我自己的 handleConnection 方法实现定义类,它扩展了类 WebServiceMessageReceiverHandlerAdapter

在我的实现中,我排除了状态变化.重要的是在这个类的自动装配构造函数中传递 WebMessageFactory bean,否则在应用程序启动时会引发异常.

这个类必须用Spring构造型(例如@Component)进行标记,并且在配置ServletRegistrationBean时必须在Configuration类中配置这个bean的名称:

@Bean公共 ServletRegistrationBeanmessageDispatcherServlet(ApplicationContext applicationContext){MessageDispatcherServlet servlet = new MessageDispatcherServlet();servlet.setApplicationContext(applicationContext);servlet.setTransformWsdlLocations(true);servlet.setMessageFactoryBeanName("webServiceMessageFactory");servlet.setMessageReceiverHandlerAdapterBeanName("myOwnMessageReceiverHandlerAdapter");返回新的 ServletRegistrationBean<>(servlet,"/ws/*");}

I published endpoints using Spring Boot WS-Server

When I use SoapUI I see:

HTTP/1.1 200 Accept: text/xml, text/html, image/gif, image/jpeg, *; q=.2, /; q=.2 SOAPAction: "" Content-Type: text/xml;charset=utf-8 Content-Length: 828 Date: Thu, 29 Apr 2021 14:04:54 GMT Keep-Alive: timeout=60 Connection: keep-alive

I would like to set custom HTTP Status in response (I know that it may be against the standard but it is an external requirement). I also read following topic:

Spring WS (DefaultWsdl11Definition) HTTP status code with void

But this solution failed

Spring Boot version: 2.2.7

解决方案

Problem was solved

As I said I wanted to set custom HTTP status in SOAP response.

I found this post: Spring WS (DefaultWsdl11Definition) HTTP status code with void

Author used EndpointInterceptor with TransportContext to get HttpServletResponse, then he changed status. The difference between my and his case is the fact, that he returned void from WebService method whereas I wanted to return some response.

In my situation following code in Spring WebServiceMessageReceiverObjectSupport class (method handleConnection) overrode servlet status previously set in interceptor:

if (response instanceof FaultAwareWebServiceMessage && connection instanceof FaultAwareWebServiceConnection) {
                    FaultAwareWebServiceMessage faultResponse = (FaultAwareWebServiceMessage)response;
                    FaultAwareWebServiceConnection faultConnection = (FaultAwareWebServiceConnection)connection;
                    faultConnection.setFaultCode(faultResponse.getFaultCode());
                }

In order to bypass this fragment of code I needed to define class with my own implementation of handleConnection method, which extended class WebServiceMessageReceiverHandlerAdapter

In my implementation I excluded change of status. Important thing is to pass WebMessageFactory bean in autowired constructor of this class, otherwise exception is raised during app's startup.

This class has to be marked with Spring stereotype (eg. @Component) and name of this bean has to be configured in Configuration class when configuring ServletRegistrationBean:

@Bean
    public ServletRegistrationBean<MessageDispatcherServlet> messageDispatcherServlet(ApplicationContext applicationContext){
        MessageDispatcherServlet servlet = new MessageDispatcherServlet();
        servlet.setApplicationContext(applicationContext);
        servlet.setTransformWsdlLocations(true);
        servlet.setMessageFactoryBeanName("webServiceMessageFactory");
        servlet.setMessageReceiverHandlerAdapterBeanName("myOwnMessageReceiverHandlerAdapter");
        return new ServletRegistrationBean<>(servlet,"/ws/*");
    }

这篇关于Spring Boot WS-Server - 自定义 Http 状态的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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