Java Web 服务客户端在调用 Web 方法时发生 NullPointerException
Java Web Service Client NullPointerException on calling a Web Method
每当我从 Web 服务调用方法时,客户端都会收到 NullPointerException。
这些是我从测试中得到的结果:
工具 "SoapUI" 表明 WebService 有效并且 returns 预期值
None的变量是null
WebService 的返回值似乎没有到达客户端
客户端成功连接到WebService
异常:
javafx.fxml.LoadException:
... Not Important ...
Caused by: java.lang.NullPointerException
at com.sun.xml.internal.bind.v2.ClassFactory.create0(Unknown Source)
at com.sun.xml.internal.bind.v2.ClassFactory.create(Unknown Source)
at com.sun.xml.internal.bind.v2.runtime.reflect.Lister$CollectionLister.startPacking(Unknown Source)
at com.sun.xml.internal.bind.v2.runtime.reflect.Lister$CollectionLister.startPacking(Unknown Source)
at com.sun.xml.internal.bind.v2.runtime.reflect.NullSafeAccessor.get(Unknown Source)
at com.sun.xml.internal.bind.v2.runtime.reflect.Accessor.getUnadapted(Unknown Source)
at com.sun.xml.internal.bind.v2.runtime.JAXBContextImpl.get(Unknown Source)
at com.sun.xml.internal.ws.db.glassfish.RawAccessorWrapper.get(Unknown Source)
at com.sun.xml.internal.ws.client.sei.ResponseBuilder$DocLit$PartBuilder.readResponse(Unknown Source)
at com.sun.xml.internal.ws.client.sei.ResponseBuilder$DocLit.readResponse(Unknown Source)
at com.sun.xml.internal.ws.client.sei.StubHandler.readResponse(Unknown Source)
at com.sun.xml.internal.ws.db.DatabindingImpl.deserializeResponse(Unknown Source)
at com.sun.xml.internal.ws.db.DatabindingImpl.deserializeResponse(Unknown Source)
at com.sun.xml.internal.ws.client.sei.SyncMethodHandler.invoke(Unknown Source)
at com.sun.xml.internal.ws.client.sei.SyncMethodHandler.invoke(Unknown Source)
at com.sun.xml.internal.ws.client.sei.SEIStub.invoke(Unknown Source)
at com.sun.proxy.$Proxy40.getOnlineUser(Unknown Source)
at application.Controller.init(Controller.java:141)
at application.Controller.initialize(Controller.java:74)
at javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:2548)
... 17 more
我的网络服务:
@WebService
@SOAPBinding(style = javax.jws.soap.SOAPBinding.Style.DOCUMENT, use=javax.jws.soap.SOAPBinding.Use.LITERAL, parameterStyle=javax.jws.soap.SOAPBinding.ParameterStyle.WRAPPED)
public interface CClientService {
public ObservableList<Benutzer> getOnlineUser(int id);
}
我的 Web 服务实现:
@WebService
public class CClientServiceImpl implements CClientService {
private BenutzerService benutzerService;
private Notification notification;
@Override
public ObservableList<Benutzer> getOnlineUser(int id) {
return benutzerService.getAllOnlineBenutzerWithoutMe(id);
}
}
我的通知:
public class Notification extends Observable {
@Override
public synchronized void addObserver(Observer o) {
notifyTheObservers(o);
super.addObserver(o);
}
@Override
public synchronized void deleteObserver(Observer o) {
super.deleteObserver(o);
notifyTheObservers(o);
}
public void notifyTheObservers(Object arg){
setChanged();
super.notifyObservers(arg);
}
}
我的 BenutzerService:
public class BenutzerService extends Repository<Benutzer> {
public ObservableList<Benutzer> getAllOnlineBenutzerWithoutMe(int id){
ObservableList<Benutzer> listeAllerBenutzer = FXCollections.observableArrayList();
listeAllerBenutzer.setAll(em.createNamedQuery("Benutzer.findAllOnlineWithoutMe",Benutzer.class).setParameter("b_id", id).getResultList());
return listeAllerBenutzer;
}
}
我的存储库:
public abstract class Repository<T> {
protected EntityManagerFactory emf = Persistence.createEntityManagerFactory("unitname");
protected EntityManager em = emf.createEntityManager();
abstract public void create(T object);
abstract public void update(T object);
abstract public void delete(T object);
abstract public T findById(int id);
abstract public ObservableList<T> getAll();
}
我的客户:
public class CClient implements Observer {
private Client client;
private final String host = "localhost";
private final String port = "8080";
private final String path = "MyCoolPath?wsdl";
private CClientService cclientService;
public CClient(){
client = new Client();
this.cclientService = getService();
}
public Client getClient() {
return client;
}
@Override
public void update(Observable o, Object arg) {
switch(arg.getClass().getSimpleName()){
case "String":
System.out.println("Got it, my String.");
break;
default:
System.out.println(arg.getClass().getSimpleName()+"<<<<<<<<");
break;
}
}
private CClientService getService(){
URL urlAdresse = null;
try {
urlAdresse = new URL("http://"+ host + ":"+ port + "/" + path);
} catch (MalformedURLException e) {
System.out.println("No Connection!");
e.printStackTrace();
}
QName qnameService = new QName("http://webservice/",
"CClientServiceImplService");
QName qnamePort = new QName("http://webservice/",
"CClientServiceImplPort");
Service service = Service.create(urlAdresse,qnameService);
CClientService ws = service.getPort(qnamePort,CClientService.class);
return ws;
}
public CClientService getCclientService() {
return this.cclientService;
}
}
Web 服务和客户端在不同的项目中。
首先,我将注释 @WebService
从我的 "CClientServiceImpl.class" 更改为 @WebService(endpointInterface="webservice.CClientService")
,因为我意识到 System.out.println(ws.get...().size());
我得到了错误消息 "Unkown Port",在我更改注释后 "Unknown Port" 错误已解决。
问题是,我想 return 一个 "ObservableList"... 我将其更改为 "List"(在 "CClientServiceImpl.class" 和 "BenutzerService.class")问题解决了......我检查了一切,但没有尝试如果我得到正常的 java.util.List
.
会发生什么
所以我的 WebService 接口方法如下所示:
public List<Benutzer> getOnlineUser(int id);
和这样的实现:
@Override
public List<Benutzer> getOnlineUser(int id) {
return benutzerService.getAllOnlineBenutzerWithoutMe(id);
}
最后我的服务改成了这样:
public List<Benutzer> getAllOnlineBenutzerWithoutMe(int id){
List<Benutzer> listeAllerBenutzer = FXCollections.observableArrayList();
listeAllerBenutzer = (em.createNamedQuery("Benutzer.findAllOnlineWithoutMe",Benutzer.class).setParameter("b_id", id).getResultList());
return listeAllerBenutzer;
}
每当我从 Web 服务调用方法时,客户端都会收到 NullPointerException。
这些是我从测试中得到的结果:
工具 "SoapUI" 表明 WebService 有效并且 returns 预期值
None的变量是
null
WebService 的返回值似乎没有到达客户端
客户端成功连接到WebService
异常:
javafx.fxml.LoadException:
... Not Important ...
Caused by: java.lang.NullPointerException
at com.sun.xml.internal.bind.v2.ClassFactory.create0(Unknown Source)
at com.sun.xml.internal.bind.v2.ClassFactory.create(Unknown Source)
at com.sun.xml.internal.bind.v2.runtime.reflect.Lister$CollectionLister.startPacking(Unknown Source)
at com.sun.xml.internal.bind.v2.runtime.reflect.Lister$CollectionLister.startPacking(Unknown Source)
at com.sun.xml.internal.bind.v2.runtime.reflect.NullSafeAccessor.get(Unknown Source)
at com.sun.xml.internal.bind.v2.runtime.reflect.Accessor.getUnadapted(Unknown Source)
at com.sun.xml.internal.bind.v2.runtime.JAXBContextImpl.get(Unknown Source)
at com.sun.xml.internal.ws.db.glassfish.RawAccessorWrapper.get(Unknown Source)
at com.sun.xml.internal.ws.client.sei.ResponseBuilder$DocLit$PartBuilder.readResponse(Unknown Source)
at com.sun.xml.internal.ws.client.sei.ResponseBuilder$DocLit.readResponse(Unknown Source)
at com.sun.xml.internal.ws.client.sei.StubHandler.readResponse(Unknown Source)
at com.sun.xml.internal.ws.db.DatabindingImpl.deserializeResponse(Unknown Source)
at com.sun.xml.internal.ws.db.DatabindingImpl.deserializeResponse(Unknown Source)
at com.sun.xml.internal.ws.client.sei.SyncMethodHandler.invoke(Unknown Source)
at com.sun.xml.internal.ws.client.sei.SyncMethodHandler.invoke(Unknown Source)
at com.sun.xml.internal.ws.client.sei.SEIStub.invoke(Unknown Source)
at com.sun.proxy.$Proxy40.getOnlineUser(Unknown Source)
at application.Controller.init(Controller.java:141)
at application.Controller.initialize(Controller.java:74)
at javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:2548)
... 17 more
我的网络服务:
@WebService
@SOAPBinding(style = javax.jws.soap.SOAPBinding.Style.DOCUMENT, use=javax.jws.soap.SOAPBinding.Use.LITERAL, parameterStyle=javax.jws.soap.SOAPBinding.ParameterStyle.WRAPPED)
public interface CClientService {
public ObservableList<Benutzer> getOnlineUser(int id);
}
我的 Web 服务实现:
@WebService
public class CClientServiceImpl implements CClientService {
private BenutzerService benutzerService;
private Notification notification;
@Override
public ObservableList<Benutzer> getOnlineUser(int id) {
return benutzerService.getAllOnlineBenutzerWithoutMe(id);
}
}
我的通知:
public class Notification extends Observable {
@Override
public synchronized void addObserver(Observer o) {
notifyTheObservers(o);
super.addObserver(o);
}
@Override
public synchronized void deleteObserver(Observer o) {
super.deleteObserver(o);
notifyTheObservers(o);
}
public void notifyTheObservers(Object arg){
setChanged();
super.notifyObservers(arg);
}
}
我的 BenutzerService:
public class BenutzerService extends Repository<Benutzer> {
public ObservableList<Benutzer> getAllOnlineBenutzerWithoutMe(int id){
ObservableList<Benutzer> listeAllerBenutzer = FXCollections.observableArrayList();
listeAllerBenutzer.setAll(em.createNamedQuery("Benutzer.findAllOnlineWithoutMe",Benutzer.class).setParameter("b_id", id).getResultList());
return listeAllerBenutzer;
}
}
我的存储库:
public abstract class Repository<T> {
protected EntityManagerFactory emf = Persistence.createEntityManagerFactory("unitname");
protected EntityManager em = emf.createEntityManager();
abstract public void create(T object);
abstract public void update(T object);
abstract public void delete(T object);
abstract public T findById(int id);
abstract public ObservableList<T> getAll();
}
我的客户:
public class CClient implements Observer {
private Client client;
private final String host = "localhost";
private final String port = "8080";
private final String path = "MyCoolPath?wsdl";
private CClientService cclientService;
public CClient(){
client = new Client();
this.cclientService = getService();
}
public Client getClient() {
return client;
}
@Override
public void update(Observable o, Object arg) {
switch(arg.getClass().getSimpleName()){
case "String":
System.out.println("Got it, my String.");
break;
default:
System.out.println(arg.getClass().getSimpleName()+"<<<<<<<<");
break;
}
}
private CClientService getService(){
URL urlAdresse = null;
try {
urlAdresse = new URL("http://"+ host + ":"+ port + "/" + path);
} catch (MalformedURLException e) {
System.out.println("No Connection!");
e.printStackTrace();
}
QName qnameService = new QName("http://webservice/",
"CClientServiceImplService");
QName qnamePort = new QName("http://webservice/",
"CClientServiceImplPort");
Service service = Service.create(urlAdresse,qnameService);
CClientService ws = service.getPort(qnamePort,CClientService.class);
return ws;
}
public CClientService getCclientService() {
return this.cclientService;
}
}
Web 服务和客户端在不同的项目中。
首先,我将注释 @WebService
从我的 "CClientServiceImpl.class" 更改为 @WebService(endpointInterface="webservice.CClientService")
,因为我意识到 System.out.println(ws.get...().size());
我得到了错误消息 "Unkown Port",在我更改注释后 "Unknown Port" 错误已解决。
问题是,我想 return 一个 "ObservableList"... 我将其更改为 "List"(在 "CClientServiceImpl.class" 和 "BenutzerService.class")问题解决了......我检查了一切,但没有尝试如果我得到正常的 java.util.List
.
所以我的 WebService 接口方法如下所示:
public List<Benutzer> getOnlineUser(int id);
和这样的实现:
@Override
public List<Benutzer> getOnlineUser(int id) {
return benutzerService.getAllOnlineBenutzerWithoutMe(id);
}
最后我的服务改成了这样:
public List<Benutzer> getAllOnlineBenutzerWithoutMe(int id){
List<Benutzer> listeAllerBenutzer = FXCollections.observableArrayList();
listeAllerBenutzer = (em.createNamedQuery("Benutzer.findAllOnlineWithoutMe",Benutzer.class).setParameter("b_id", id).getResultList());
return listeAllerBenutzer;
}