1

swagger-maven-pluginswagger.json を生成するために使用します。ただし、実行ごとにプロパティの順序が変わることに気付きました。たとえば、次のようになります。

{
  ...
  "definitions" : {
    "MyClass1" : {
      "type" : "object",
      "properties" : {
        "name" : {
          "type" : "string"
        },
        "title" : {
          "type" : "string"
        },
        "description" : {
          "type" : "string"
        },
      }
    }
  }
  ...
}

そして、次の世代の後:

{
  ...
  "definitions" : {
    "MyClass1" : {
      "type" : "object",
      "properties" : {
        "description" : {
          "type" : "string"
        },
        "title" : {
          "type" : "string"
        },
        "name" : {
          "type" : "string"
        }
      }
    }
  }
  ...
}

Javaの私のクラス:

public interface MyClass1 {
   String getName();
   String getTitle();
   String getDescription();
}
4

1 に答える 1

1

Java ランタイムでは、クラスで宣言されたメソッドの正確な順序を知ることはできません。開くとjava.lang.Class#getDeclaredMethods()( https://docs.oracle.com/javase/8/docs/api/java/lang/Class.html#getDeclaredMethods--を参照)、それが表示されThe elements in the returned array are not sorted and are not in any particular order.ます。

だからこそ、ジャクソンはあなたのためにそれをすることができません.

ただし、2 つの解決策があります。

@JsonPropertyOrder1.注釈を使用できます:

@JsonPropertyOrder({"name", "title", "description"})
public interface MyClass1 {
   String getName();
   String getTitle();
   String getDescription();
}

2.フィールドを持つクラスを使用できます(フィールドの順序は保持されます)

public class MyClass1 {
   String name;
   String title;
   String description;
   //Getters skipped
}
于 2018-08-13T15:53:48.427 に答える