0

i am trying to get data from a server and i need to show server certificate on for this work so i have included server certificate as below but iam getting below error:

Exception in thread "main" java.lang.Error: Unresolved compilation problems:

The constructor HttpHeaders() is not visible

The constructor HttpEntity<String>() is not visible

my pom file:

<properties>
  <spring.version>4.1.0.RELEASE</spring.version>
    <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
  </properties>

  <dependencies>
    <dependency>
      <groupId>junit</groupId>
      <artifactId>junit</artifactId>
      <version>3.8.1</version>
      <scope>test</scope>
    </dependency>

    <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-core</artifactId>
            <version>${spring.version}</version>
        </dependency>

        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-web</artifactId>
            <version>${spring.version}</version>
        </dependency>


<dependency>
    <groupId>org.apache.httpcomponents</groupId>
    <artifactId>httpclient</artifactId>
    <version>4.3.4</version>
</dependency>
<dependency>
    <groupId>com.google.guava</groupId>
    <artifactId>guava</artifactId>
    <version>18.0</version>
</dependency>

  </dependencies>

my java file:

public static void main( String[] args ) throws KeyStoreException, NoSuchAlgorithmException, CertificateException, FileNotFoundException, IOException, KeyManagementException, UnrecoverableKeyException
    {

        KeyStore keyStore = KeyStore.getInstance(KeyStore.getDefaultType());
        keyStore.load(new FileInputStream("/Users/workspace/a/cert.cer"),
                "password".toCharArray());
        SSLConnectionSocketFactory socketFactory = new SSLConnectionSocketFactory(
                new SSLContextBuilder()
                        .loadTrustMaterial(null, new TrustSelfSignedStrategy())
                        .loadKeyMaterial(keyStore, "changeit".toCharArray()).build());
        HttpClient httpClient = HttpClients.custom().setSSLSocketFactory(socketFactory).build();
        ClientHttpRequestFactory requestFactory = new HttpComponentsClientHttpRequestFactory(
                httpClient);
        RestTemplate restTemplate = new RestTemplate(requestFactory);
        HttpHeaders headers = new HttpHeaders();
        headers.add("Content-Type", "application/xml");
        HttpEntity<String> entity = new HttpEntity<String>();
       ResponseEntity<String> response = restTemplate.exchange(
                "https://www.example.com",
                HttpMethod.GET, entity, String.class);
        System.out.println( "Hello World!" );
    }
Labeo
  • 5,831
  • 13
  • 47
  • 77
  • Well yea. You have compilation errors, and you need to fix them BEFORE you run your code. – Stephen C Sep 01 '15 at 13:11
  • I'd check your imports. You're likely not importing Spring's `org.springframework.http.HttpHeaders` maybe Apache's `org.apache.http.HttpHeaders`, and similar for `HttpEntity`. – Mena Sep 01 '15 at 13:13
  • if i import them i am getting clash in the import error – Labeo Sep 01 '15 at 14:00

1 Answers1

0

There are no public constructor for HttpHeaders and HttpEntity in org.apache.http. So you can not create these Objects. Also HttpEntity is an Interface.

Maybe you have imported the wrong classes.

Jens
  • 67,715
  • 15
  • 98
  • 113