私はRESTサービスを提供するアプリケーションを開発してきました。私はそれに対して実行するいくつかのテスト済みコードを持っており、それが正常に機能するかどうかを確認しています。
ローカルのWeblogic開発サーバーにデプロイされたアプリケーションに対して実行すると、正常に動作します。
ただし、Red Hatマシン上の別のWeblogicサーバーにデプロイすると、400の不正な要求エラーが発生します。
サービスのテストに使用しているクライアントコードは次のとおりです。
Client client = Client.create();
//WebResource webResource = client.resource("http://10.1.1.2:7001/NotificationFramework/rest/notifications/createNotification");
WebResource webResource = client.resource("http://rhvm:7003/NotificationFramework/rest/notifications/createNotification");
ClientResponse clientResponse = webResource.type("application/json").post(ClientResponse.class, testJsonObject.toString());
JSONObject response2 = new JSONObject(clientResponse.getEntity(String.class));
System.out.println(response2);
コメントされた行は、私のローカルマシン上の行です。
これが私が得ている応答です:
An error occurred: Server returned HTTP response code: 400 for URL: http://rhvm:7003/NotificationFramework/rest/notifications/createNotification
そして、RESTサービスを提供するコードの抜粋を次に示します。
@Path("/notifications")
public class RestServices {
@POST
@Path("/createNotification")
@Consumes( {MediaType.APPLICATION_JSON} )
@Produces( {MediaType.APPLICATION_JSON} )
public static NotificationResponse createNotification(JAXBElement<Notification> n) {
// do some stuff
return notificationResponse;
}
私はすでに最後に余分な/を入れてみました。そして、Firefox用のRESTClientアドオンでテストしたところ、まったく同じ動作が得られました。
どんな助けでも大歓迎です。
前もって感謝します。
// 編集
私はそれがJAXBElementと関係があることを発見しました。
次のサービスが機能します。
@POST
@Path("testRest3")
@Consumes( {MediaType.APPLICATION_JSON} )
@Produces({MediaType.APPLICATION_JSON})
public static NotificationResponse testRest3() {
logger.info("yo3");
return new NotificationResponse(101, "yo");
}
しかし、以下はそうではありません:
@POST
@Path("testRest4")
@Consumes( {MediaType.APPLICATION_JSON} )
@Produces({MediaType.APPLICATION_JSON})
public static NotificationResponse testRest4(JAXBElement<Notification> n) {
logger.info("yo4");
return new NotificationResponse(101, "yo");
}
pestrellaが推奨するNotificationクラスを確認したところ、@XmlRootElementが欠落していることがわかりました。これを追加しましたが、まだ問題は解決していません。@Xmlにする必要があるかどうかはわかりませんが、これは初めてです。vogellaからのチュートリアルに従います。
これが私の通知クラスです:
@XmlRootElement
public class Notification {
private int applicationId;
private int notificationId;
private int priority;
private String message;
private String detail;
private String appUrl;
// methods and stuff
}
そして、Firefox用のRESTClientアドオンで送信された本文は次のとおりです。
{"appUrl":"","message":"my message","notificationId":1110001,"detail":"my detail","priority":3,"applicationId":111}