Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7912555
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 3, 20262026-06-03T13:38:29+00:00 2026-06-03T13:38:29+00:00

I am creating a Java EE client where I need to make a call

  • 0

I am creating a Java EE client where I need to make a call to a node (js) server to get the response. So I made a single class through which requests are made to node server.
Everytime I get a response, I need to send back the response code and the response itself. So I thought of creating a String array which would contain the response code and the response.

Here is my class:

import javax.ws.rs.core.MediaType;

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.LoggingFilter;
import com.sun.jersey.core.impl.provider.entity.StringProvider;

public class RequestHandler {

/**
 * Makes a HTTP Request for a given url. The type
 * determines which type of request, post or get,
 * will be made. The params cane be in form of
 * name=value pair or JSON format.
 * 
 * @param type Request method. 1 - Get, 2 - Post.
 * @param url url string to which request has to be 
 * made.
 * @param path path of the resource or service for a
 * url.
 * @param params request parameters. Can be either
 * name=value pair or JSON request.
 * 
 * @return String representation of the response.
 * 
 */
public static String[] makeRequest(int type, String url, String path, String params) {
    String[] response = new String[2];
    ClientResponse clientResponse = null;
    try {
        ClientConfig config = new DefaultClientConfig();
        config.getClasses().add(StringProvider.class); 
        Client client = Client.create(config);

        WebResource service =
                client.resource(url); 
        client.addFilter(new LoggingFilter());
        service.path("rest");

        // 1 - GET, 2 - POST
        switch (type) {
            case 1: {
                System.out.println("Making GET request to: " + url + path);
                System.out.println("Request Params: " + params);
                clientResponse = service.path(path).type(MediaType.APPLICATION_JSON_TYPE).accept(MediaType.APPLICATION_JSON).get(ClientResponse.class); //TODO Code to be corrected, include params
                break;
            }
            case 2: {
                System.out.println("Making POST request to: " + url + path);
                System.out.println("Request Params: " + params);
                clientResponse = service.path(path).type(MediaType.APPLICATION_JSON_TYPE).accept(MediaType.APPLICATION_JSON).post(ClientResponse.class, params);
                break;
            }
        }
    } catch (Exception e) {
        e.printStackTrace();
        response[0] = "500";
        response[1] = "Internal Server Error";
        return response;
    }

    response[0] = String.valueOf(clientResponse.getStatus());
    response[1] = clientResponse.getEntity(String.class);

    System.err.println("Response status: " + response[0]);

    return response;
    }//end of makeRequest()

}//end of class

But I am not convinced by creating too much string objects which would cause performance issues. So I thought of creating a Map which would send back the response code and the response.

response.put(500, "Internal Server Error");

But again, creating a map with integer and checking everytime the response code creates overhead of boxing and unboxing of Integer object, which again could result in performance.

HashMap<Integer, String> response = RequestHandler.makeRequest(2, urlString, "/login", params);

if (response.containsKey(500)) {
 return Message.INTERNAL_SERVER_ERROR;
}

Which one should I use for better performance? Or is there any better alternative out of it?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-03T13:38:30+00:00Added an answer on June 3, 2026 at 1:38 pm

    Given that a HashMap contains an array, plus a bunch of housekeeping items (size, threshold, load factor), a String[] array is going to be smaller (and thus, by one measure, more efficient) than a HashMap.

    However, avoid premature optimisation.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm trying to get a certain cookie in a java client by creating a
Im looking for the concept of creating Java Server to handle Client requests and
I am stuck creating a socket connection between blackberry client and java server. I
I'm am creating a VoIP client server pair in java using the netbeans IDE.
I'm creating a client/server pair in Java that, for now, only supports interlaced text
I'm creating a Java application where the user can search through a list of
I am creating a java.util.logging.FileHandler that is allowed to cycle through files. When multiple
I am creating a Java ME application to connect to server. My problem in
I'm tasked with creating a Java Web Service for a .NET 2.0 client to
Quick design question: I need to implement a form of communication between a client-server

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.