如何将angular.js中的请求/响应对象写入jax-rs

时间:2016-06-19 09:05:04

标签: java rest jersey jax-rs

我正在构建我的后端,我需要能够将angular.js中的请求/响应对象交换到我的后端(JAX-RS,Jersey)。

我的后端目前看起来像这样:

@POST
@Path("/search")
@ApiResponses(value = {@ApiResponse(code=200,message="returns results"),
               @ApiResponse(code=404,message="not found")})
@Produces({ "application/json" })
public Response getPostShop( 
        @QueryParam("keyphrase") String keyphrase,
        @QueryParam("product") String product,      
        @QueryParam("priceRange") List<Double> priceRange,
        @Context SecurityContext securityContext) 
    throws NotFoundException {  
    SearchRequest searchRequest = new SearchRequest();
    searchRequest.setKeyphrase(keyphrase);
    searchRequest.setProduct(product);
    searchRequest.setPriceRange(priceRange);
    //do something with the "searchRequest"
    return Response.ok(searchResponse).build();
}

Angular.Js(像这样的东西)

response.compose = function(searchRequest) {

        return $http({

            method: 'POST', //or GET
            url: '/search',
            data: {
                searchRequest : searchRequest
            },
            headers: {'Content-Type':'application/json'}
        });
    }

其中searchRequest

 $scope.searchRequest = {
                'keyphrase' : $scope.keyphrase,
                'product' : $scope.product,
                'priceRange' : $scope.priceRange,
        };

NEW GET请求:

@GET
@Path("/testkeyphrase")
@ApiResponses(value = {@ApiResponse(code=200,message="returns results"),
           @ApiResponse(code=404,message="not found")})
@Produces({ "application/json" })
@Consumes({ "application/json" })
public Response getTestKeyphrase( 
        @ApiParam(value="keyphrase that the user searches for..", required=true) TestSearchRequest testSearchRequest,           
        @Context SecurityContext securityContext) 
throws NotFoundException {      
    String kyphrase = testSearchRequest.getKeyphrase();
    return Response.ok(testSearchRequest).build();
}

并且TestSearchRequest是:

public class TestSearchRequest {

    public TestSearchRequest(String keyphrase) {
        this.setKeyphrase(keyphrase);
    }

    private String keyphrase;

    public String getKeyphrase() {
        return keyphrase;
    }

    public void setKeyphrase(String keyphrase) {
        this.keyphrase = keyphrase;
    }

}

我需要的不是接收所有单独的参数,而是直接接收searchRequest对象并分别发送回SearchResponse对象(以JSON格式)。此外,我需要参数在请求的主体中,而不是在URI中。 有任何想法吗?

0 个答案:

没有答案