繁体   English   中英

如何在 mule 连接器中添加下拉列表项?

[英]how do i add a drop down list item in a 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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM