简体   繁体   中英

Java: Web Service server getting client information

Using some online examples (copy/paste) I created web service server and client connecting to it. Works OK. Client sends requests (everything runs on same PC) and server responds. 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. I am obviously missing something. Code below shows implementation of web service methods. I added new "client identification code" to addPerson method. Upon execution client calls addPerson and server crashes at
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

@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.

InetSocketAddress remoteAddress = exchange.getRemoteAddress();
String remoteHost;
if(remoteAddress != null){
   remoteHost = remoteAddress.getHostName();
}
System.out.println("Who poked us: " +  remoteHost);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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