Restlet Post Request不提供传递的JSON数据

时间:2013-05-23 19:23:11

标签: java api restlet restlet-2.0

我正在使用Restlet 2.1.2并且我的请求表达式文本为空。我查看了论坛并读到该实体是瞬态的,如果没有正确缓存将被销毁,但是当我有一个提供安全性的警卫时,它不会触及我所知道的实体。更有趣的是,对于一个仅检查用户是否可以进行身份​​验证的路由/身份验证,可以获取实体文本,即JSON对象。但是当运行/ account / create的代码时,都使用@Post(“json:json”)发布请求,我无法获取发送的JSON数据。下面我添加了我的代码和我发出的示例curl语句。对于结果,请参阅AuthenticationResource和Account Resource类。

public class Executable{

    public static void main(String[] args) throws Exception{
        Component com = new Component();
        com.getServers().add( Protocol.HTTP,  8182 );
        com.getDefaultHost().attach( "/myapi", new RoutingApp() );
        com.start();  
    }
}

public class RoutingApp extends Application {

    public RoutingApp() {
        super();
    }

    @Override  
    public synchronized Restlet createInboundRoot() { 

        Context context = getContext();
        Router router = new Router(context);

        router.attach( "/account/create", AccountResource.class );
        router.attach( "/authenticate", AuthenticationResource.class );

        ChallengeAuthenticator authenticator = new ChallengeAuthenticator( 
                context, ChallengeScheme.HTTP_BASIC, "myapi" );

        MySecretVerifier verifier = new MySecretVerifier();
        MyEnroler enroler = new MyEnroler();
        authenticator.setVerifier( verifier );
        authenticator.setEnroler( enroler );

        MyAuthorizer authorizer = new MyAuthorizer();
        authorizer.setNext( router );

        authenticator.setNext( authorizer );

        return authenticator; 
    }
}

public class MySecretVerifier extends SecretVerifier {

    @Override
    public int verify( String identifier, char[] secret ) {

       [... Get User by identifier ...]

       if( [... Check User Secret ...] ) {
           Request request = Request.getCurrent();
           request.getAttributes().put( "currentUser", user );
           return SecretVerifier.RESULT_VALID;
       } else {
           return SecretVerifier.RESULT_INVALID;
       }
    }
}

public class MyEnroler implements Enroler {

    @Override
    public void enrole(ClientInfo ci) {

        Request request = Request.getCurrent();
        User user = (User) request.getAttributes().get( "currentUser" );

        if( null != user ) {

            [... Get User Roles ...]

            for( [... Each Role ...] ) {
                Role role = newRole( r.name, "" );
                ci.getRoles().add( role );
            }
        }
    }
}

public class MyAuthorizer extends Authorizer {

    @Override
    protected boolean authorize( Request request, Response response ) {

        ClientInfo info = request.getClientInfo();
        Method method = request.getMethod();
        User user = (User) request.getAttributes().get( "currentUser" );
        String uri = request.getResourceRef().getRemainingPart( true, false );
        List<Role> userRoles = info.getRoles();

        boolean authorized = false;

        if( [... Check if User is Authorized ...] ) {
            request.getAttributes().putAll( [... add some properties from a HashMap ...] );
            authorized = false;
        }

        return authorized;
    }
}

public class AuthenticationResource extends ServerResource {

    @Post("json:json")
    @Override
    public String performPost(Representation entity) {

        Request request = Request.getCurrent();

        request.getEntityAsText();     //returns { "id" : "larry", "secret" : "test" }
        request.getEntity().getText(); //returns null
        entity.toString();                  //returns [application/json]
        entity.getText();                  //returns null
    }
}

public class AccountResource extends ServerResource {

    @Post("json:json")
    @Override
    public String performPost( Representation entity ) {

        Request request = Request.getCurrent();

        request.getEntityAsText();     //returns null
        request.getEntity().getText(); //returns null
        entity.toString();                  //returns [application/json]
        entity.getText();                   //returns null
    }
}

我的CURL声明如下:

curl -i --user larry:test -H "Content-Type: application/json" -X POST -d '{ "id" : "larry", "secret" : "test" }' http://localhost:8182/myapi/account/create
curl -i --user larry:test -H "Content-Type: application/json" -X POST -d '{ "id" : "larry", "secret" : "test" }' http://localhost:8182/myapi/authenticate

1 个答案:

答案 0 :(得分:0)

如果其他人遇到类似的问题,那么实际上是问题类“AccountResource”的名称。我不确定为什么,但我只能假设它与构建在Restlet中的AccountResource接口有关,导致了这个问题。