繁体   English   中英

从 Java 客户端调用 SOAP 服务时出现错误请求 [400] - 如何设置有效负载?

[英]Bad Request [400] when calling SOAP service from Java Client - how to set payload?

我正在使用带有 Java 11 的 SpringBoot。

我正在尝试使用 SOAP web 服务,但出现以下错误:

org.springframework.ws.client.WebServiceTransportException:错误请求 [400]

I can however call the web service successfully (200) in Postman and SOAP UI, so there's something wrong with my Java code ( ProconSoapClient.java ).

Postman 请求(200):

curl --location --request POST 'http://******/ProCONWS/WebService_PCSOrderEntry.asmx' \
--header 'Content-Type: application/soap+xml;charset=UTF-8;action="http://www.procon.dk/calculate_MD5"' \
--header 'Content-Length: 458' \
--data-raw '<soap:Envelope xmlns:soap="http://www.w3.org/2003/05/soap-envelope" xmlns:proc="http://www.procon.dk/">
   <soap:Header/>
   <soap:Body>
      <proc:calculate_MD5>
         <proc:user>*****</proc:user>
         <proc:timestamp>20220915 144942</proc:timestamp>
         <proc:Secret_word>********</proc:Secret_word>
      </proc:calculate_MD5>
   </soap:Body>
</soap:Envelope>'

Java 代码(400):

ProconSoapClient.java

如何设置正文(有效负载)? objectFactory.createString接受String但我需要发送CalculateMD5

// request headers
Map<String, String> headers = new HashMap<>();
headers.put("Cache-Control", "no-cache");
headers.put("Accept-Encoding", "gzip,deflate");
headers.put("Content-Type", "application/soap+xml;charset=UTF-8;action=\"http://www.procon.dk/calculate_MD5\"");
headers.put("Content-Length", "458");
headers.put("Connection", "Keep-Alive");
//headers.put("charset", "utf-8");
WebServiceMessageCallback callback = getRequestCallback(headers);

// request object
ObjectFactory objectFactory = new ObjectFactory();
CalculateMD5 calculateMD5 = objectFactory.createCalculateMD5();
calculateMD5.setUser(PROCON_SOAP_ACCESS_USER);
calculateMD5.setTimestamp(URLEncoder.encode(DateUtil.dateToString(new Date(), "yyyyMMdd HHmmss"), StandardCharsets.UTF_8));
calculateMD5.setSecretWord(PROCON_SOAP_ACCESS_SECRETWORD);

// payload
final String payload = marshal(calculateMD5);
// todo: what must go here? accepts a String but the object is a CalculateMD5
JAXBElement<String> jaxbElementPayload = objectFactory.createString(payload);

// call the SOAP service
Object response = getWebServiceTemplate().marshalSendAndReceive(PROCON_SOAP_ACCESS_URL, jaxbElementPayload, callback);

生成的类:

CalculateMD5ObjectFactory对象是从 WSDL 生成的。

计算MD5.java

/**
 * <p>Java class for anonymous complex type.
 * 
 * <p>The following schema fragment specifies the expected content contained within this class.
 * 
 * <pre>
 * &lt;complexType&gt;
 *   &lt;complexContent&gt;
 *     &lt;restriction base="{http://www.w3.org/2001/XMLSchema}anyType"&gt;
 *       &lt;sequence&gt;
 *         &lt;element name="user" type="{http://www.w3.org/2001/XMLSchema}string" minOccurs="0"/&gt;
 *         &lt;element name="timestamp" type="{http://www.w3.org/2001/XMLSchema}string" minOccurs="0"/&gt;
 *         &lt;element name="Secret_word" type="{http://www.w3.org/2001/XMLSchema}string" minOccurs="0"/&gt;
 *       &lt;/sequence&gt;
 *     &lt;/restriction&gt;
 *   &lt;/complexContent&gt;
 * &lt;/complexType&gt;
 * </pre>
 * 
 * 
 */
@XmlAccessorType(XmlAccessType.FIELD)
@XmlType(name = "", propOrder = {
    "user",
    "timestamp",
    "secretWord"
})
@XmlRootElement(name = "calculate_MD5")
public class CalculateMD5 {

    protected String user;
    protected String timestamp;
    @XmlElement(name = "Secret_word")
    protected String secretWord;

    /**
     * Gets the value of the user property.
     * 
     * @return
     *     possible object is
     *     {@link String }
     *     
     */
    public String getUser() {
        return user;
    }

    /**
     * Sets the value of the user property.
     * 
     * @param value
     *     allowed object is
     *     {@link String }
     *     
     */
    public void setUser(String value) {
        this.user = value;
    }

    /**
     * Gets the value of the timestamp property.
     * 
     * @return
     *     possible object is
     *     {@link String }
     *     
     */
    public String getTimestamp() {
        return timestamp;
    }

    /**
     * Sets the value of the timestamp property.
     * 
     * @param value
     *     allowed object is
     *     {@link String }
     *     
     */
    public void setTimestamp(String value) {
        this.timestamp = value;
    }

    /**
     * Gets the value of the secretWord property.
     * 
     * @return
     *     possible object is
     *     {@link String }
     *     
     */
    public String getSecretWord() {
        return secretWord;
    }

    /**
     * Sets the value of the secretWord property.
     * 
     * @param value
     *     allowed object is
     *     {@link String }
     *     
     */
    public void setSecretWord(String value) {
        this.secretWord = value;
    }

}

ObjectFactory.java

@XmlRegistry
public class ObjectFactory {

    private final static QName _String_QNAME = new QName("http://www.procon.dk/", "string");

    /**
     * Create a new ObjectFactory that can be used to create new instances of schema derived classes for package: com.clubtravel.powwow.soap.client.procon.generated
     * 
     */
    public ObjectFactory() {
    }

    /**
     * Create an instance of {@link CalculateMD5 }
     * 
     */
    public CalculateMD5 createCalculateMD5() {
        return new CalculateMD5();
    }

    /**
     * Create an instance of {@link CalculateMD5Response }
     * 
     */
    public CalculateMD5Response createCalculateMD5Response() {
        return new CalculateMD5Response();
    }

    /**
     * Create an instance of {@link PcsCreateOrder }
     * 
     */
    public PcsCreateOrder createPcsCreateOrder() {
        return new PcsCreateOrder();
    }

    /**
     * Create an instance of {@link PcsCreateOrderResponse }
     * 
     */
    public PcsCreateOrderResponse createPcsCreateOrderResponse() {
        return new PcsCreateOrderResponse();
    }

    /**
     * Create an instance of {@link PcsGetOrderStatus }
     * 
     */
    public PcsGetOrderStatus createPcsGetOrderStatus() {
        return new PcsGetOrderStatus();
    }

    /**
     * Create an instance of {@link PcsGetOrderStatusResponse }
     * 
     */
    public PcsGetOrderStatusResponse createPcsGetOrderStatusResponse() {
        return new PcsGetOrderStatusResponse();
    }

    /**
     * Create an instance of {@link JAXBElement }{@code <}{@link String }{@code >}
     * 
     * @param value
     *     Java instance representing xml element's value.
     * @return
     *     the new instance of {@link JAXBElement }{@code <}{@link String }{@code >}
     */
    @XmlElementDecl(namespace = "http://www.procon.dk/", name = "string")
    public JAXBElement<String> createString(String value) {
        return new JAXBElement<String>(_String_QNAME, String.class, null, value);
    }

}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM