title: How to develop a spring rest web service in accessing external API?
body: <p>I want to develop the spring rest web service which accesses the external API when user sends request. User sends a json request and set of request headers to the web service and web service should authenticate the user and call the eternal API. External API response should be given to the user as the response body. This is what I want to happen basically.</p>
  
  <p>//Authenticate to access API</p>
  
  <pre><code>public class HotelbedsAuthentication {
  
      final String hotelEndpoint = "https://api.test.hotelbeds.com/hotel-api/1.0/";
      private String request;
  
      private static final String apiKey="enter given api key for free";
      private static final String secretKey="free key";
      private String signature=org.apache.commons.codec.digest.DigestUtils.sha256Hex(apiKey + secretKey + System.currentTimeMillis() / 1000);
  
      public HttpsURLConnection findHotels(){
  
          HttpsURLConnection connection = null;
  
          try{
  
          URL url = new URL(hotelEndpoint+getRequest());
  
          HttpsURLConnection con = (HttpsURLConnection) url.openConnection();
          con.setRequestProperty("X-Signature", signature);
          con.setRequestProperty("Api-Key", apiKey);
          con.setRequestProperty("Accept", "application/json");
          con.setRequestProperty("Accept-Encoding", "gzip");
          con.setRequestProperty("Content-Type", "application/json;charset=UTF-8");
          con.setRequestMethod("POST");
          con.setDoOutput(true);
          con.setDoInput (true);
  
          connection=con;
          }catch(Exception error ){
              System.out.println("An error occured "+error);
          }
          return connection;
      }
  
      public String getRequest() {
          return request;
      }
  
      public void setRequest(String request) {
          this.request = request;
      }
  
  }
  </code></pre>
  
  <p>//Rest Controller</p>
  
  <pre><code>public class FindHotelController {
  
      HotelbedsAuthentication token = new HotelbedsAuthentication();
  
  @RequestMapping(value="hotels",method=RequestMethod.POST,produces = MediaType.APPLICATION_JSON_VALUE)
      @ResponseBody
      public HotelAvailability findHotel(@RequestBody JSONObject request){
  
          HttpsURLConnection connection;
  
          File pathDir = new File("C:/Users/User/workspace/SpringRestSample/src/main/java");
  
          JCodeModel codeModel = new JCodeModel();
          JSONObject response= new JSONObject();
  
          HotelAvailability sample= new HotelAvailability();
  
          //String userRequest="{\"stay\": {\"checkIn\": \"2018-01-29\",\"checkOut\": \"2018-01-31\",\"shiftDays\": \"2\"},\"occupancies\": [{\"rooms\": 1,\"adults\": 2,\"children\": 1,\"paxes\": [{\"type\": \"AD\",\"age\": 30},{\"type\": \"AD\",\"age\": 30},{\"type\": \"CH\",\"age\": 8}]}],\"hotels\": {\"hotel\": [1067,1070,1075,135813,145214,1506,1508,1526,1533,1539,1550,161032,170542,182125,187939,212167,215417,228671,229318,23476]}}";
  
          try{
              token.setRequest("hotels");
              connection= token.findHotels();
  
              //Read request and embed to url
              OutputStream os = connection.getOutputStream();
              os.write(request.toString().getBytes("UTF-8"));
              os.close();
  
              // read the response
              InputStream in = new BufferedInputStream(connection.getInputStream());
              String result = org.apache.commons.io.IOUtils.toString(in, "UTF-8");
              JSONObject jsonObject = new JSONObject(result);
              response=jsonObject;
  
              GenerationConfig config = new DefaultGenerationConfig() {
                @Override
                public boolean isGenerateBuilders() {
                    return true;
                }
                public SourceType getSourceType(){
                    return SourceType.JSON;
                }
              };
  
              SchemaMapper mapper =new SchemaMapper(new RuleFactory(config, new GsonAnnotator(config), new SchemaStore()), new SchemaGenerator());
              mapper.generate(codeModel, "HotelAvailability","com.sample.model",response.toString());
  
              codeModel.build(pathDir);
  
              ObjectMapper objectMapper = new ObjectMapper();
              sample = objectMapper.readValue(response.toString(), HotelAvailability.class);
  
              in.close();
              connection.disconnect();
  
          }catch(Exception ex){
              System.out.println(ex);
          }
          System.out.println(sample);
          return sample;
      }
  }
  </code></pre>