It really depends on how re-usable you need your method to be. If you only ever want to send requests with that set of 4 parameters, that's probably about as concise as you can get. If you're looking to send arbitrary JSON data, you'd probably want to construct the JSONObject outside the method, and pass it in as a single argument.
If you're looking for small, syntactic wins, you might want to check out the google Guava and Gson libraries. They'd let you slightly condense this to:
public void callingMethod() {
Map<String, Object> params = ImmutableMap.of(
"param1Name", param1Value,
"param2Name", param2Value,
);
makeRequest(params);
}
public void makeRequest(Map<String, Object> params) {
HttpPost request = new HttpPost(url);
request.addHeader("Content-Type", "application/json");
request.setEntity(new Gson().toJson(params)));
}
Alternatively, if you're interacting with a whole REST API, you could use a library like Jersey to model it as a Java class, and then create a proxy to hide the fact that you're making an HTTP request at all:
@Path("/v1")
public interface RestApi {
@POST
@Path("/objects/{objectId}/create")
@Consumes(MediaType.APPLICATION_JSON)
@Produces(MediaType.APPLICATION_JSON)
ResponseObject createObject(@PathParam("objectId") Integer objectId, RequestObject body);
}
public class RequestObject {
public final String param1;
public final List<Integer> param2;
public RequestObject(String param1, List<Integer> param2) {
this.param1 = param1;
this.param2 = param2;
}
}
public class ResponseObject {
// etc
}
public static void main() {
String url = "https://api.example.com";
Client client = ClientBuilder.newBuilder().build().target(url);
RestApi restApi = WebResourceFactory.newResource(RestApi.class, clientBuilder);
ResponseObject response = restApi.createObject(12, new RequestObject("param1", ImmutableList.of(1,2,3));
}
Er, I guess the point here is that Java isn't particularly concise.