[英]Java: Web Service server getting client information
Using some online examples (copy/paste) I created web service server and client connecting to it. 使用一些在线示例(复制/粘贴),我创建了Web服务服务器和连接到它的客户端。 Works OK. 工作正常。 Client sends requests (everything runs on same PC) and server responds. 客户端发送请求(一切都在同一台PC上运行),服务器响应。 Now I am trying to add a piece of code (on a server side) that identifies who connected and send request (get client info basically) Did some research and found code. 现在,我试图添加一段代码(在服务器端),以识别谁连接并发送请求(基本上获取客户端信息)进行了一些研究并找到了代码。 I added that code to where I think it should go to but I get NullPointerException. 我将该代码添加到了我认为应该去的位置,但是我得到了NullPointerException。 I am obviously missing something. 我显然缺少了一些东西。 Code below shows implementation of web service methods. 下面的代码显示了Web服务方法的实现。 I added new "client identification code" to addPerson method. 我在addPerson方法中添加了新的“客户端标识代码”。 Upon execution client calls addPerson and server crashes at 执行后,客户端调用addPerson并导致服务器崩溃
HttpExchange exchange = (HttpExchange)msgx.get("com.sun.xml.ws.http.exchange"); HttpExchange exchange =(HttpExchange)msgx.get(“ com.sun.xml.ws.http.exchange”);
I know this is something simple(code is in a wrong spot?) but I am completely new to this wed service concept. 我知道这很简单(代码在错误的位置?),但是对于这种婚礼服务概念我是完全陌生的。 Any help will be greatly appreciated. 任何帮助将不胜感激。
Code at https://www.journaldev.com/9123/jax-ws-tutorial https://www.journaldev.com/9123/jax-ws-tutorial上的代码
@WebService(endpointInterface = "tests.PersonService")
public class PersonServiceImplementation implements PersonService {
@Resource
WebServiceContext wsContext;
private static Map<Integer,Person> persons = new HashMap<Integer,Person>();
@Override
@WebMethod
public boolean addPerson(Person p) {
//// idetify who poked us ////
MessageContext msgx = wsContext.getMessageContext();
HttpExchange exchange = (HttpExchange)msgx.get("com.sun.xml.ws.http.exchange");
InetSocketAddress remoteAddress = exchange.getRemoteAddress();
String remoteHost = remoteAddress.getHostName();
System.out.println("Who poked us: " + remoteHost);
//////////////////////////////
if(persons.get(p.getId()) != null) return false;
persons.put(p.getId(), p);
return true;
}
@Override
public boolean deletePerson(int id) {
if(persons.get(id) == null) return false;
persons.remove(id);
return true;
}
@Override
public Person getPerson(int id) {
return persons.get(id);
}
@Override
public Person[] getAllPersons() {
Set<Integer> ids = persons.keySet();
Person[] p = new Person[ids.size()];
int i=0;
for(Integer id : ids){
p[i] = persons.get(id);
i++;
}
return p;
}
}
InetSocketAddress remoteAddress = exchange.getRemoteAddress();
String remoteHost = remoteAddress.getHostName();
Are you sure that remoteAddress has value,if remoteAddress is null then remoteAddress.getHostName() will throw a NullPointerException.So,you should add a conditional. 您确定remoteAddress具有值,如果remoteAddress为null,则remoteAddress.getHostName()将引发NullPointerException。因此,您应该添加条件。
InetSocketAddress remoteAddress = exchange.getRemoteAddress();
String remoteHost;
if(remoteAddress != null){
remoteHost = remoteAddress.getHostName();
}
System.out.println("Who poked us: " + remoteHost);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.