如何从Jersey POST响应中获取JSON对象

时间:2015-07-11 10:30:29

标签: android json spring rest jersey-2.0

我目前正在REST API上使用Spring Android for Client和Spring Boot for the Server。

我无法使用Spring Security保护服务器,因为我不了解如何正确使用它。在阅读了大量的示例应用程序后,我放弃了Spring安全性,并找到了关于如何使用Jersey保护API的this教程。

我使用Jersey Client API来测试实现

,而不是javascript
        Client client = ClientBuilder.newClient();
    WebTarget target = client.target("http://localhost:8080/demo-business-resource/login");

    Invocation.Builder invoBuilder = target.request();
    invoBuilder.header("service_key", "3b91cab8-926f-49b6-ba00-920bcf934c2a");

      MultivaluedMap formData = new MultivaluedMapImpl();
      formData.add("username", "username2");
      formData.add( "password", "passwordForUser2");
      Response response = invoBuilder.post(Entity.form(formData));
      System.out.println(response);
response.getEntity();

POST请求成功,但我无法获得应该是响应实体的JSON对象。 response.getEntity();返回一个HttpUrlConnector对象。

这里是相关的服务器代码

@POST
@Path( "login" )
@Produces( MediaType.APPLICATION_JSON )
public Response login(
    @Context HttpHeaders httpHeaders,
    @FormParam( "username" ) String username,
    @FormParam( "password" ) String password ) {

    DemoAuthenticator demoAuthenticator = DemoAuthenticator.getInstance();
    String serviceKey = httpHeaders.getHeaderString( DemoHTTPHeaderNames.SERVICE_KEY );

    try {
        String authToken = demoAuthenticator.login( serviceKey, username, password );

        JsonObjectBuilder jsonObjBuilder = Json.createObjectBuilder();
        jsonObjBuilder.add( "auth_token", authToken );
        JsonObject jsonObj = jsonObjBuilder.build();

        return getNoCacheResponseBuilder( Response.Status.OK ).entity( jsonObj.toString() ).build();

    } catch ( final LoginException ex ) {
        JsonObjectBuilder jsonObjBuilder = Json.createObjectBuilder();
        jsonObjBuilder.add( "message", "Problem matching service key, username and password" );
        JsonObject jsonObj = jsonObjBuilder.build();

        return getNoCacheResponseBuilder( Response.Status.UNAUTHORIZED ).entity( jsonObj.toString() ).build();
    }
}


private Response.ResponseBuilder getNoCacheResponseBuilder( Response.Status status ) {
    CacheControl cc = new CacheControl();
    cc.setNoCache( true );
    cc.setMaxAge( -1 );
    cc.setMustRevalidate( true );

    return Response.status( status ).cacheControl( cc );
}

我是Jersey新手,我只想用它来生成身份验证令牌,因为我使用Spring获得404 Not Found错误。 (我正在和spring-jersery合作) 这是我对客户端的Spring方法

        RestTemplate template = new RestTemplate();

    MultiValueMap<String, String> params = new LinkedMultiValueMap<String, String>();
    params.add("service_key", "3b91cab8-926f-49b6-ba00-920bcf934c2a");

    MultiValueMap<String,String> formData=new LinkedMultiValueMap<String,String>();
    formData.add("username", "username2");
    formData.add( "password", "passwordForUser2");
    Model_LoginProfile log = new Model_LoginProfile();
    log.setLoginName("username2");
    log.setPassword("passwordForUser2");

     HttpHeaders requestHeaders=new HttpHeaders();
      requestHeaders.setContentType(MediaType.APPLICATION_JSON);
      requestHeaders.set("service_key", "3b91cab8-926f-49b6-ba00-920bcf934c2a");

      HttpEntity<Model_LoginProfile> requestEntity = new HttpEntity<Model_LoginProfile>(log, requestHeaders);

    ResponseEntity<String> result = template.postForEntity("http://localhost:8080/demo-business-resource/login", requestEntity, String.class);
    }

也许某人也有解决方法。 :)

提前致谢

0 个答案:

没有答案