java - 如何在 Mule 连接器中添加下拉列表项?

标签 java drop-down-menu mule connector

我做了一个骡子连接器,一切都很好。我获得了 HTTP 出站连接的 4 个主要属性:url、端口、路径和方法。

我希望该方法是一个带有值的下拉列表:GET、POST。

你们知道怎么做吗?如果这是解决问题的方法,需要使用什么注释?或者在添加 Map 或 Enum 属性时它是否被 Mule 识别?

这是我当前的简单代码

/**
 * This file was automatically generated by the Mule Development Kit
 */
package com.web.testcomponent;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;

import org.mule.api.MuleEventContext;
import org.mule.api.annotations.Configurable;
import org.mule.api.annotations.Module;
import org.mule.api.annotations.Processor;
import org.mule.api.annotations.display.Placement;
import org.mule.api.annotations.expressions.Lookup;
import org.mule.api.annotations.param.Default;
import org.mule.api.annotations.param.Optional;
import org.mule.api.annotations.param.Payload;

/**
 * Generic module
 *
 * @author MuleSoft, Inc.
 */
@Module(name="testcomponent", schemaVersion="1.0-SNAPSHOT", friendlyName="HTTP/S Component", description="This is custom HTTP/S Component")
public class TestComponentModule
{

    @Lookup("myMuleContext")
    private MuleEventContext context;

    /**
     * Connection URL
     */
    @Configurable
    @Placement(order=1,group="Connection",tab="General")
    private String url;

    /**
     * Connection Port
     */
    @Configurable
    @Placement(order=2,group="Connection",tab="General")
    private String port;

    /**
     * Connection Path
     */
    @Configurable
    @Placement(order=3,group="Connection",tab="General")
    private String path;    

    /**
     * Connection Method (GET or POST)
     */
    @Configurable
    @Default(value="GET")
    @Optional
    @Placement(order=4,group="Connection",tab="General")
    private String method;

    public String getUrl() {
        return url;
    }

    public void setUrl(String url) {
        this.url = url;
    }

    public String getPort() {
        return port;
    }

    public void setPort(String port) {
        this.port = port;
    }

    public String getPath() {
        return path;
    }

    public void setPath(String path) {
        this.path = path;
    }

    public String getMethod() {
        return method;
    }

    public void setMethod(String method) {
        this.method = method;
    }

    public MuleEventContext getContext() {
        return context;
    }

    public void setContext(MuleEventContext context) {
        this.context = context;
    }

    /**
     * Custom processor
     *
     * {@sample.xml ../../../doc/TestComponent-connector.xml.sample testcomponent:test-processor}
     *
     * @param content Random content to be processed
     * @param payload The String payload itself
     * @return Some string
     */
    @Processor
    public String testProcessor(@Payload String payload)
    {       
        HttpURLConnection conn = null;

        try {
            URL url = new URL(getUrl() + "/" + getPath());
            conn = (HttpURLConnection) url.openConnection();
            conn.setRequestMethod(getMethod());
            conn.setRequestProperty("Accept", "application/json");

            if(conn.getResponseCode() != HttpURLConnection.HTTP_OK) {
                throw new RuntimeException("Failed -> HTTP error code : " + conn.getResponseCode());
            }

            BufferedReader br = new BufferedReader(new InputStreamReader(conn.getInputStream()));
            String output, buffer;
            output = "";
            while((buffer = br.readLine()) != null) {
                output += buffer;
            }

            payload = output;
            conn.disconnect();
        } catch (MalformedURLException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }

        return payload;
    }
}

最佳答案

我使用枚举没有问题:

public enum HttpMethod
{
    GET, POST
};

@Configurable
@Default(value = "GET")
@Optional
@Placement(order = 4, group = "Connection", tab = "General")
private HttpMethod method;

这不也适合你吗?

顺便说一句,看看您的 @Processor 方法的作用,我想知道您是否没有重新发明 @RestCall 处理器。另外,当您可以使用 MuleClient 执行 HTTP 调用时,为什么还要使用原始 HttpURLConnection 呢?

关于java - 如何在 Mule 连接器中添加下拉列表项?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17473105/

相关文章:

java - 构建不断失败

java - 什么是NullPointerException,我该如何解决?

iphone - 如何创建下拉框?

html - 在网站内容上方定位一个 Div,使用位置 :relative

mule - 在 Mule 应用程序中实现条件循环

mule - 如何启动 Mule 流以使用 HTTP 端点从 JMS 队列读取消息

java - JWindow 总是在顶部没有获得焦点事件

asp.net - 如何在 IE 中设置禁用 SELECT (DropDownList) 的样式?

exception - 为什么 Mule 异常(exception)策略这么啰嗦?

java - TransformProcess 在使用 DataSetIterator 时转换数据