Webサービス、Jersey、Jackson、REST APIを初めて使用しています。 私は、Webサービス、Jersey、Jackson、REST APIの基本をいくつか取り上げました。Jersey REST API要求が機能しない
Netbeans IDEを使用してサンプルプロジェクトを開発しました。
ブラウザからREST APIを呼び出すと、開発者ツールを使用してわかった次のエラーが表示されます。 127.0.0.1:8080
次は私のエラーです
リクエストURL:http://localhost:8080/ImageShowcase/v1/user/login
リクエスト方法:
ステータスコードを取得:
リモートアドレスを許可されていない405の方法Tomcat 7に入ったログ
SEVERE:コンテキスト初期化イベントをliに送る例外クラスcom.sample.jersey.app.MyServletのstenerインスタンス
java.lang.IllegalStateException:APIキーと秘密を見つけることができません。アプリケーションを初期化できません。あなたのAPIキーとシークレットは、〜/ .stormpath/apiKey.properties
この私のプロジェクト構造に格納されていることを確認してください:コントローラパッケージで
私は、次のコードを持っているUser.java パッケージcom.sample.controller;
import com.sample.model.UserModel;
import com.sample.pojo.UserCredentials;
import javax.ws.rs.Consumes;
import javax.ws.rs.GET;
import javax.ws.rs.POST;
import javax.ws.rs.Path;
import javax.ws.rs.Produces;
import javax.ws.rs.core.Context;
import javax.ws.rs.core.MediaType;
import javax.ws.rs.core.Response;
@Path("/user")
public class User {
@Path("/login")
@POST
@Consumes("application/json")
@Produces("application/json")
public Response UserAuthentication(UserCredentials user) {
String output = "{\"username\":\"xyz\",\"password\":\"abc\"}";
UserModel userAuthentication = new UserModel();
if(userAuthentication.AuthenticateUser(user))
return Response.status(201).entity(output).build();
else
return Response.status(201).entity(output).build();
}
}
次は私のJerseyClientコード
package com.sample.jerseyclient;
import com.sun.jersey.api.client.Client;
import com.sun.jersey.api.client.ClientResponse;
import com.sun.jersey.api.client.WebResource;
public class JerseyClient {
public static void main(String[] args) {
try {
System.out.println("Client started");
Client client = Client.create();
WebResource webResource = client
.resource("http://localhost:8080/ImageShowcase/v1/user/login");
String input = "{\"username\":\"demo\",\"password\":\"demo\"}";
// POST method
ClientResponse response = webResource.accept("application/json")
.type("application/json").post(ClientResponse.class, input);
// check response status code
if (response.getStatus() != 201) {
throw new RuntimeException("Failed : HTTP error code : "
+ response.getStatus());
}
// display response
String output = response.getEntity(String.class);
System.out.println("Output from Server .... ");
System.out.println(output + "\n");
} catch (Exception e) {
e.printStackTrace();
}
}
}
続いているが、私は私の商務ロジック(データベースなど)を実装しています私のUserModelです。
package com.sample.model;
import com.sample.pojo.UserCredentials;
import java.sql.*;
public class UserModel {
public boolean AuthenticateUser(UserCredentials user) {
Database db = new Database();
Connection con = null;
try {
String username = user.getUsername();
String password = user.getPassword();
ResultSet rs;
con = db.getConnection();
if (con != null) {
String selectQuery_UserDetails = "SELECT NAME,PASSWORD FROM USER WHERE NAME=? AND PASSWORD = ?";
PreparedStatement preparedStatement = con.prepareStatement(selectQuery_UserDetails);
preparedStatement.setString(1, username);
preparedStatement.setString(2, password);
rs = preparedStatement.executeQuery(selectQuery_UserDetails);
if (rs != null) {
return true;
}
return false;
}
} catch (Exception e) {
return false;
} finally {
db.closeConnection(con);
}
return true;
}
}
これは、ユーザーの資格情報のための私のPOJOクラスです:
package com.sample.pojo;
import org.codehaus.jackson.annotate.JsonProperty;
public class UserCredentials {
@JsonProperty
private String username;
@JsonProperty
private String password;
public String getUsername() {
return username;
}
public String getPassword() {
return password;
}
}
私は私がここで間違ってやっているものを得ていないのです。 私が知りたいことは、使用している構造が正しいかどうかです。
ありがとうございました。
に設定されていることを確認。 'com.sample.jersey.app.MyServlet'の関連するコードから始めましょう。 '〜/ .stormpath/apiKey.properties'はStormpathのヒントですが、ここでは何も見ません。Btw、リクエストメソッドは実際にはPOSTではありませんGET – zyexal
これは私が混乱している部分であり、私はそのようなパッケージを持っていないのに、なぜこのように表示されているのかわかりません。 私のソリューションを実装する前に、このパッケージを含むサンプルアプリケーションを実行したので、これはBuildに関連する問題ですか? ありがとうございます。 –
私が知りたいことは、JerseyClientを実行する方法です。 これらのAPIをすべてテストするためにpostmanを使用できますか? –