SOAP服务器POST请求

时间:2020-10-05 13:16:32

标签: java spring http post soap

我在网络领域还很新。 因此,我现在的目标是创建一个基于XML的SOAP服务器,该服务器始终以分块数据进行响应。 主要是根据邮政要求。该请求本身是不相关的。响应是固定的,应该始终有效。最后,应将其转换为简单的jar以便执行。

现在,我尝试了一些教程,但没有任何实际效果。我能做的最好的就是响应GET请求的SOAP服务器。在那里,我正在使用Spring Boot + Maven + Java

这是我的代码结构:

-> soapserver
    -> src
         ->main
               ->java
                     ->soapServer
                            ->config
                                  ->SoapWsConfig.java
                            ->constructs
                                  ->ReturnMessage.java
                            ->endpoint
                                  ->soapEndpoint.java
                            ->service
                                  ->DataFactory.java
                            ->SpringBootSoapWsApplication.java
               ->resources
                     ->returnMessage.xsd
         ->test
    -> target
    -> pom.xml

代码如下:

POM

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-parent</artifactId>
    <version>2.1.4.RELEASE</version>
    <relativePath /> <!-- lookup parent from repository -->
</parent>
<groupId>com.javatechie</groupId>
<artifactId>spring-boot-sopa-ws</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>spring-boot-sopa-ws</name>
<description>Demo project for Spring Boot</description>

<properties>
    <java.version>1.8</java.version>
</properties>

<dependencies>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-web-services</artifactId>
    </dependency>

    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-devtools</artifactId>
        <scope>runtime</scope>
        <optional>true</optional>
    </dependency>
    <dependency>
        <groupId>wsdl4j</groupId>
        <artifactId>wsdl4j</artifactId>
    </dependency>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-test</artifactId>
        <scope>test</scope>
    </dependency>
</dependencies>

<build>
    <plugins>
        <plugin>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-maven-plugin</artifactId>
        </plugin>
        <plugin>
            <groupId>org.codehaus.mojo</groupId>
            <artifactId>jaxb2-maven-plugin</artifactId>
            <version>1.6</version>
            <executions>
                <execution>
                    <id>xjc</id>
                    <goals>
                        <goal>xjc</goal>
                    </goals>
                </execution>
            </executions>
            <configuration>
                <schemaDirectory>${project.basedir}/src/main/resources/</schemaDirectory>
                <outputDirectory>${project.basedir}/src/main/java</outputDirectory>
                <clearOutputDir>false</clearOutputDir>
            </configuration>
        </plugin>
    </plugins>
</build>

</project>

SoapWSConfig

package soapServer.config;

import org.springframework.boot.web.servlet.ServletRegistrationBean;
import org.springframework.context.ApplicationContext;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.io.ClassPathResource;
import org.springframework.ws.config.annotation.EnableWs;
import org.springframework.ws.transport.http.MessageDispatcherServlet;
import org.springframework.ws.wsdl.wsdl11.DefaultWsdl11Definition;
import org.springframework.xml.xsd.SimpleXsdSchema;
import org.springframework.xml.xsd.XsdSchema;

@Configuration
@EnableWs
public class SoapWSConfig {

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

@Bean(name = "test")
public DefaultWsdl11Definition defaultWsdl11Definition(XsdSchema schema) {
    DefaultWsdl11Definition defaultWsdl11Definition = new DefaultWsdl11Definition();
    defaultWsdl11Definition.setPortTypeName("returnMsgindicator");
    defaultWsdl11Definition.setLocationUri("/ws");
    defaultWsdl11Definition.setTargetNamespace("http://www.javatechie.com/spring/soap/api/returnMsg");
    defaultWsdl11Definition.setSchema(schema);
    return defaultWsdl11Definition;

}

@Bean
public XsdSchema schema() {
    return new SimpleXsdSchema(new ClassPathResource("returnMsg.xsd"));
}

}

ReturnMessage

package soapServer.constructs;

import javax.xml.bind.annotation.XmlAccessType;
import javax.xml.bind.annotation.XmlAccessorType;
import javax.xml.bind.annotation.XmlRootElement;


@XmlAccessorType(XmlAccessType.FIELD)

@XmlRootElement(name = "ReturnMessage")
public class ReturnMessage {


protected String data;


public void setData(String data){
    this.data = data;
}


public String getData(){
    return this.data;
}


}

soapEndpoint

package soapServer.endpoint;

import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.ws.server.endpoint.annotation.Endpoint;
import org.springframework.ws.server.endpoint.annotation.ResponsePayload;

import soapServer.constructs.ReturnMessage;

import soapServer.service.DataFactory;


@Endpoint
public class soapEndpoint {

@ResponsePayload
public ReturnMessage getGETResponse() {
    return DataFactory.getData();
}


@PostMapping("/test2")
public ReturnMessage getPOSTResponse() {
    return DataFactory.getData();
}

}

DataFactory

package soapServer.service;

import soapServer.constructs.ReturnMessage;
import org.springframework.stereotype.Service;

@Service
public class DataFactory {

public static ReturnMessage getData() {
    ReturnMessage rMessage = new ReturnMessage();
    rMessage.setData("Test");
    return rMessage;
}


}

我真的想尽一切办法了。 希望你们中的一些人可以提供帮助。 预先感谢!

迈克尔

0 个答案:

没有答案