添加JAXB生成的SOAP标头

时间:2019-04-03 13:31:47

标签: spring-boot soap jaxb

到目前为止,从不需要构建我的HEADER请求的SOAP部分。 好,那天到了。因此,我必须更改现有代码以支持使用SOAP和不使用HEADER的{​​{1}}请求。

这是我的代码当前正在执行的操作:

HEADER

这里是我序列化不同对象的地方:TrendingRead,MitologyRead ...

public class TrendingReadVO extends ParentRequestClientVO {
    public TrendingReadVO (final TrendingRead trendingRead) {
        super(trendingRead, "TrendingSoapAction", TrendingReadResponse.class);
    }
}

public class ParentRequestClientVO {
    private Object readRequest;
    private String serviceName;
    private Class<?> unmarshalTargetclass;

    public ParentRequestClientVO(Object readRequest, String serviceName, Class<?> unmarshalTargetclass) {
        super();
        this.readRequest = readRequest;
        this.serviceName = serviceName;
        this.unmarshalTargetclass = unmarshalTargetclass;
    }   
    public Object getReadRequest() {
        return readRequest;
    }
    public String getServiceName() {
        return serviceName;
    }
    public Class<?> getUnmarshalTargetclass() {
        return unmarshalTargetclass;
    }
}

现在,我需要将@Component public class ParentRequestClient { @Autowired private ParentHttpSoapClientConnector httpClientConnector; @Autowired private ParentSOAPResponseReader soapResponseReader; @Autowired private ParentResponseUnmarshaler responseUnmarshaler; @Autowired private JAXBContextProvider jaxbContextProvider; private DocumentBuilderFactory documentBuilderFactory = DocumentBuilderFactory.newInstance(); public <T> T getReadResponse(final ParentRequestClientVO requestClientVO) throws Exception { return this.getReadResponse(requestClientVO.getReadRequest(), requestClientVO.getServiceName(), requestClientVO.getUnmarshalTargetclass()); } public <T> T getReadResponse(Object readRequest, String serviceName, Class<?> clazz) throws Exception { String request = buildRequest(readRequest); return executeRequest(request, serviceName, clazz); } private String buildRequest(Object request) throws JAXBException, ParserConfigurationException, IOException, SOAPException { String output = ""; try { Document document = documentBuilderFactory.newDocumentBuilder().newDocument(); Marshaller marshaller = jaxbContextProvider.getJAXBContext(request.getClass()).createMarshaller(); marshaller.marshal(request, document); if (document != null) output = this.addSoapWrapper(document); } catch (JAXBException | ParserConfigurationException e) { throw new Exception(e); } return output; } /*wrap request with envelope, header and body*/ public String addSoapWrapper(Document document) throws SOAPException, IOException { ByteArrayOutputStream outputStream = new ByteArrayOutputStream(); try { SOAPMessage soapMessage = MessageFactory.newInstance().createMessage(); soapMessage.getSOAPBody().addDocument(document); soapMessage.saveChanges(); soapMessage.writeTo(outputStream); } catch (SOAPException e) { throw e; } catch (IOException e) { throw e; } return new String(outputStream.toByteArray()); } ... 添加到我的SecurityHeaderType方法中:

ParentRequestClient.buildRequest

有人知道该怎么做吗?

0 个答案:

没有答案