Monday, May 13, 2019

JAXB unmarshalling

 javax.xml.bind.JAXBContext jaxbCtx = javax.xml.bind.JAXBContext.newInstance(sdr.getClass().getPackage().getName());
            javax.xml.bind.Unmarshaller unmarshaller = jaxbCtx.createUnmarshaller();
            sdr = (SdResultSet) unmarshaller.unmarshal(new StringReader( responseString)); //NOI18N
           
            ArrayList<SdResult> sdResults=(ArrayList<SdResult>) sdr.sdResults.sdResult;
           
           
            for(SdResult obj:sdResults )
            {
               
               
            }

Friday, May 10, 2019

Rest Client XML

import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.NameValuePair;
import org.apache.http.auth.AuthenticationException;

import org.apache.http.auth.UsernamePasswordCredentials;
import org.apache.http.client.ClientProtocolException;

import org.apache.http.client.methods.CloseableHttpResponse;

import org.apache.http.client.methods.HttpPost;
import org.apache.http.impl.client.HttpClients;

import org.apache.http.util.EntityUtils;

import org.apache.http.entity.StringEntity;
import org.apache.http.impl.auth.BasicScheme;
import org.apache.http.impl.client.CloseableHttpClient;   
       









CloseableHttpClient client = HttpClients.createDefault();
    HttpPost httpPost = new HttpPost("API_URL");

   
    String json = "XML_CONTENT";
    StringEntity entity =       new StringEntity(json); 
         
    httpPost.setEntity(entity);
    httpPost.setHeader("Accept", "application/xml");
    httpPost.setHeader("Content-type", "application/xml");
   
   


    UsernamePasswordCredentials creds
      = new UsernamePasswordCredentials("USER_NAME", "PWD");
    httpPost.addHeader(new BasicScheme().authenticate(creds, httpPost, null));

    CloseableHttpResponse response = client.execute(httpPost);
   
   
    String responseString;
        responseString = EntityUtils.toString(response.getEntity(), "UTF-8");
        System.out.println(response.getStatusLine().getStatusCode());
        System.out.println(responseString);
       
 //   assertThat(response.getStatusLine().getStatusCode(), equalTo(200));
    client.close();