gpt4 book ai didi

java - Soap 消息调用 Web 服务时的 NullPointer

转载 作者:行者123 更新时间:2023-12-01 21:47:14 26 4
gpt4 key购买 nike

尝试使用 Spring Integration ws 来使用 Web 服务,在 Web 服务端我得到一个空指针,因为传递的对象似乎没有被编码或没有映射到 xml 中,下面是客户端调用服务的片段。

public class Main {

public static void main(String[] args) {
ClassPathXmlApplicationContext context
= new ClassPathXmlApplicationContext("springws.xml");
MessageChannel channel = context.getBean("request", MessageChannel.class);

String body = "<getPojo xmlns=\"http://johnson4u.com/\"><pojo>\n"
+ " <pojoId>23</pojoId>\n"
+ " <pojoName>dubic</pojoName>\n"
+ "</pojo></getPojo>";

MessagingTemplate messagingTemplate = new MessagingTemplate();
Message<?> message = messagingTemplate.sendAndReceive(
channel, MessageBuilder.withPayload(body).build());

System.out.println(message.getPayload());
}

WSDL 由 JAxWS 端点类生成

  package com.johnson4u;


import javax.jws.WebService;
import javax.jws.WebMethod;
import javax.jws.WebParam;


@WebService(serviceName = "SpringService")
public class SpringService {


@WebMethod(operationName = "getPojo" )
public Pojo getPojo(@WebParam(Pojo pjRequest){
//Null Pointer occurs here as pjRequest might not be mapped to xml

System.out.println("Pojo name is "+pjRequest.getPojoName());

return new Pojo(234,"IM new Pojo");
}

还有 POJO

  package com.johnson4u;


public class Pojo {

private int pojoId;
private String pojoName;

public Pojo(int pojoId, String pojoName) {
this.pojoId = pojoId;
this.pojoName = pojoName;
}


public int getPojoId() {
return pojoId;
}

public void setPojoId(int pojoId) {
this.pojoId = pojoId;
}

public String getPojoName() {
return pojoName;
}

public void setPojoName(String pojoName) {
this.pojoName = pojoName;
}

不幸的是,stackoverflow无法正确格式化wsdl,但是命名空间id基于包名称com.johnson4u,下面是spring-ws-context.xml

<int:channel   id="request" />      
<int:channel id="response" />

<ws:outbound-gateway id="gateway"
request-channel="request"
uri="http://localhost:20151/SpringWs/SpringService?wsdl"/>

最佳答案

我相信字符串主体应该是

String body = "<ns:getPojo xmlns:ns=\"http://johnson4u.com/\"><pojo>\n"
+ " <pojoId>23</pojoId>\n"
+ " <pojoName>dubic</pojoName>\n"
+ "</pojo><ns:/getPojo>";

未包含命名空间符号“ns”

关于java - Soap 消息调用 Web 服务时的 NullPointer,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35844092/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com