Jersey Client API - authentication

后端 未结 7 2126
不知归路
不知归路 2020-11-30 23:14

I\'m using the Jersey client API to submit SOAP requests to a JAX-WS webservice. By default Jersey is somehow using my Windows Nt credentials for authentication when challen

7条回答
  •  北荒
    北荒 (楼主)
    2020-11-30 23:59

    Please find following working code without SSL

    I am using put request , if need post/get just change it.

    pom.xml

    
        4.0.0
        com.javacodegeeks.enterprise.rest.jersey
        JerseyJSONExample
        0.0.1-SNAPSHOT
    
        
            
                maven2-repository.java.net
                Java.net Repository for Maven
                http://download.java.net/maven/2/
                default
            
        
    
        
    
            
                com.sun.jersey
                jersey-server
                1.9
            
    
            
                com.sun.jersey
                jersey-client
                1.9
            
    
            
                com.sun.jersey
                jersey-json
                1.9
            
    
        
    
    
    

    Java Class

    package com.rest.jersey.jerseyclient;
    
    import com.rest.jersey.dto.Employee;
    import com.sun.jersey.api.client.Client;
    import com.sun.jersey.api.client.ClientResponse;
    import com.sun.jersey.api.client.WebResource;
    import com.sun.jersey.api.client.config.ClientConfig;
    import com.sun.jersey.api.client.config.DefaultClientConfig;
    import com.sun.jersey.api.client.filter.HTTPBasicAuthFilter;
    import com.sun.jersey.api.client.filter.LoggingFilter;
    import com.sun.jersey.api.json.JSONConfiguration;
    
    public class JerseyClient {
    
        public static void main(String[] args) {
            try {
    
                String username = "username";
                String password = "p@ssword";
    
    
                //{"userId":"12345","name ":"Viquar","surname":"Khan","email":"Vaquar.khan@gmail.com"}
    
    
    
    
    
                Employee employee = new Employee("Viquar", "Khan", "Vaquar.khan@gmail.com");
    
    
                ClientConfig clientConfig = new DefaultClientConfig();
    
                clientConfig.getFeatures().put( JSONConfiguration.FEATURE_POJO_MAPPING, Boolean.TRUE);
    
                Client client = Client.create(clientConfig);
                //
    
    
                    final HTTPBasicAuthFilter authFilter = new HTTPBasicAuthFilter(username, password);
                    client.addFilter(authFilter);
                    client.addFilter(new LoggingFilter());
    
                //
                WebResource webResource = client
                        .resource("http://localhost:7001/VaquarKhanWeb/employee/api/v1/informations");
    
                  ClientResponse response = webResource.accept("application/json")
                    .type("application/json").put(ClientResponse.class, employee);
    
    
                if (response.getStatus() != 200) {
                    throw new RuntimeException("Failed : HTTP error code : "
                            + response.getStatus());
                }
    
                String output = response.getEntity(String.class);
    
                System.out.println("Server response .... \n");
                System.out.println(output);
    
            } catch (Exception e) {
    
                e.printStackTrace();
    
            }
    
        }
    
    }
    

    POJO

    package com.rest.jersey.dto;
    
    public class Employee {
    
        private String name;
        private String surname;
        private String email;
        public String getName() {
            return name;
        }
        public void setName(String name) {
            this.name = name;
        }
        public String getSurname() {
            return surname;
        }
        public void setSurname(String surname) {
            this.surname = surname;
        }
        public String getEmail() {
            return email;
        }
        public void setEmail(String email) {
            this.email = email;
        }
        @Override
        public String toString() {
            return "Employee [name=" + name + ", surname=" + surname + ", email=" + email + "]";
        }
        public Employee(String name, String surname, String email) {
            super();
            this.name = name;
            this.surname = surname;
            this.email = email;
        }
    
    }
    

提交回复
热议问题