Java:将多个参数传递给方法 [英] Java: Passing multiple parameters to a method

查看:170
本文介绍了Java:将多个参数传递给方法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

将多个参数传递给方法然后将这些参数放入有效负载的正确方法是什么?

What is the proper way to pass multiple parameters to a method and then put those parameters into a payload?

该方法应发送带有效负载的HTTP请求服务器(并从中接收响应),这很好用:

The method should send an HTTP request w/ payload to a server (and receive a response from it) and this works just fine:

public static JSONObject myMethod(String parameterOne, JSONArray parameterTwo, String parameterThree, long parameterFour) {

    ...

    HttpPost request = new HttpPost(url);
    request.addHeader("Content-Type", "application/json");

    JSONObject payload = new JSONObject();
    payload.put("parameterOne", parameterOne);
    payload.put("parameterTwo", parameterTwo);
    payload.put("parameterThree", parameterThree);
    payload.put("parameterFour", parameterFour);

    request.setEntity(new StringEntity(payload.toString()));

    ...

但是,我相信应该有另一个,更高效(和审美)的方式来执行此操作。

However, I believe that there should be another, more efficient (and aesthetic) way to perform this.

推荐答案

这实际上取决于您需要使用方法的可重用性。如果您只想发送具有该组4个参数的请求,那可能就像您可以获得的那样简洁。如果您要发送任意JSON数据,您可能希望在方法之外构造JSONObject,并将其作为单个参数传递。

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.

如果您正在寻找小的,语法上的胜利,你可能想看看谷歌番石榴 Gson 库。他们会让你稍微压缩一下:

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)));
}

或者,如果您正在与整个REST API进行交互,则可以使用像Jersey这样的库将它建模为Java类,然后创建一个代理来隐藏你正在发出HTTP请求的事实:

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));
}

呃,我想这里的重点是Java并不是特别简洁。

Er, I guess the point here is that Java isn't particularly concise.

这篇关于Java:将多个参数传递给方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆