【问题标题】:Java process builder issue with schema架构的 Java 进程构建器问题
【发布时间】:2021-05-27 01:32:26
【问题描述】:

我正在使用 java process builder 执行 curl 命令以将架构发布到架构注册表,但是 得到错误。架构格式有问题,不确定如何作为流程构建器的参数传递。请提供任何建议

在此处输入代码

sourceSchema = "{"additionalProperties":false,"properties":{"age":{"type":"integer"},"firstname":{"type":"string"},"lastname":{"type":"string","minLength":2,"maxLength":4},"email":{"type":"string","format":"email"},"designation":{"default":"","type":"string"},"mobile":{"type":"string","pattern":"^(\\\\([0-9]{3}\\\\))?[0-9]{3}-[0-9]{4}$"}},"required":["firstname","lastname","age"],"title":"customer","type":"object"}";

String convertSourceSchema = sourceSchema.replace("\"", "\\\"");
        
        String constructSourceSchema = "{\"schema\":\""+convertSourceSchema+"\",\"schemaType\":\"JSON\"}";
        
            
        String[] command = {"curl", "-k", "-X", "POST", "-H", "Content-Type: application/vnd.schemaregistry.v1+json", "--data", ""+constructSourceSchema+"", ""+url+""};        

        ProcessBuilder process = new ProcessBuilder(command); 
        Process p;
        try
        {
            process.redirectErrorStream(true);
            p = process.start();
                        
            BufferedReader reader = new BufferedReader(new InputStreamReader(p.getInputStream()));
            StringBuilder builder = new StringBuilder();
            String line = null;
            while ((line = reader.readLine()) != null) {
                builder.append(line);
                //builder.append(",");
            }
            String result = builder.toString();
            System.out.print(result);

        }
        catch (Exception e)
        {   System.out.print("error");
            e.printStackTrace();
        }

> Blockquote

error am getting

% Total    % Received % Xferd  Average Speed   Time    Time     Time  Current                                 Dload  Upload   Total   Spent    Left  Speed  0     0    0     0    0     0      0      0 --:--:-- --:--:-- --:--:--     0100   635  100   208  100   427    208    427  0:00:01 --:--:--  0:00:01  2396Unexpected character ('s' (code 115)): was expecting double-quote to start field name at [Source: (org.glassfish.jersey.message.internal.ReaderInterceptorExecutor$UnCloseableInputStream); line: 1, column: 3]2021-02-24 11:19:16.409  INFO 18428 --- [nio-8080-exec-1] c.c.e.s.u.c.SchemaPublisherUtility  


【问题讨论】:

    标签: java curl apache-kafka processbuilder confluent-schema-registry


    【解决方案1】:
    
    [curl, -k, -X, POST, -H, Content-Type: application/vnd.schemaregistry.v1+json, --data, {"schema":"{\"additionalProperties\":false,\"properties\":{\"age\":{\"type\":\"integer\"},\"firstname\":{\"type\":\"string\"},\"lastname\":{\"type\":\"string\",\"minLength\":2,\"maxLength\":4},\"email\":{\"type\":\"string\",\"format\":\"email\"},\"designation\":{\"default\":\"\",\"type\":\"string\"},\"mobile\":{\"type\":\"string\",\"pattern\":\"^(\\\\([0-9]{3}\\\\))?[0-9]{3}-[0-9]{4}$\"}},\"required\":[\"firstname\",\"lastname\",\"age\"],\"title\":\"customer\",\"type\":\"object\"}","schemaType":"JSON"}, https://hostname/topicname-value/versions]
    
    @RequestBody schema -->  {"schema":"{\"additionalProperties\":false,\"properties\":{\"age\":{\"type\":\"integer\"},\"firstname\":{\"type\":\"string\"},\"lastname\":{\"type\":\"string\",\"minLength\":2,\"maxLength\":4},\"email\":{\"type\":\"string\",\"format\":\"email\"},\"designation\":{\"default\":\"\",\"type\":\"string\"},\"mobile\":{\"type\":\"string\",\"pattern\":\"^(\\\\([0-9]{3}\\\\))?[0-9]{3}-[0-9]{4}$\"}},\"required\":[\"firstname\",\"lastname\",\"age\"],\"title\":\"customer\",\"type\":\"object\"}","schemaType":"JSON"}
    
    public class SchemaPublisherUtility {
    
        private final Logger LOG = LoggerFactory.getLogger(getClass());
    
        String url = "https://hostname/topicname-value/versions";
    
        String Topic = "dev.json.schemaregistry";
    
        ObjectMapper objectMapper = new ObjectMapper();
    
        @PostMapping(path = "/publishschema", consumes = "application/json", produces = "application/json")
        public void addSchema(@RequestBody String sourceSchema) throws IOException {
    
            LOG.info("Schema Publish Started");
    
            String convertSourceSchema = sourceSchema.replace("\"", "\\\"");
    
            String constructSourceSchema = "{\"schema\":\"" + convertSourceSchema + "\",\"schemaType\":\"JSON\"}";
    
            try {
                URL requestUrl = new URL(url);
    
                HttpsURLConnection conn = (HttpsURLConnection) requestUrl.openConnection();
    
                conn.setRequestProperty("Content-Type", "application/json");
                conn.setDoOutput(true);
                conn.setRequestMethod("POST");
    
                OutputStreamWriter out = new OutputStreamWriter(conn.getOutputStream());
                out.write(constructSourceSchema);
                out.close();
    
                new InputStreamReader(conn.getInputStream());
    
            } catch (Exception e) {
                e.printStackTrace();
            }
    
            LOG.info("Schema Publish Completed");
    
        }
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2011-01-09
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-11-23
      • 1970-01-01
      • 2016-10-13
      相关资源
      最近更新 更多