在java中使用jersey中的json对象数组

时间:2013-09-04 09:48:10

标签: java web-services jersey

在球衣中消耗单个物体我有这样的方法

 @POST
@Consumes(MediaType.APPLICATION_FORM_URLENCODED)
@Produces(MediaType.APPLICATION_JSON)
public Response postPerson(MultivaluedMap<String, String> personParams){
    ResponseBuilder response = Response.ok();
    return response.build();
    }

    its working well.

我的疑问是如何消耗球衣中的物品清单

    some thing like this 



 @POST
@Consumes(MediaType.APPLICATION_FORM_URLENCODED)
@Produces(MediaType.APPLICATION_JSON)
    public Response postPerson(List<Person> person){

     ResponseBuilder response = Response.ok();
    return response.build();

      }

我做了一些谷歌搜索,但没有找到任何有效的例子。

2 个答案:

答案 0 :(得分:0)

您似乎希望将表单数据作为列表传递:

@POST
@Consumes(MediaType.APPLICATION_FORM_URLENCODED)
@Produces(MediaType.APPLICATION_JSON)
    public Response postPerson(@FormParam("person") List<String> person){

     ResponseBuilder response = Response.ok();
    return response.build();

      }

<html><body>
<form action="http://localhost:9998/myresource" method="POST">
<input type="checkbox" name="person" value="a">A</input>
<input type="checkbox" name="person" value="b">B</input>
<input type="checkbox" name="person" value="c">C</input>
<input type="submit">OK</input>
</form>
</body></html>

类似于Person对象,您必须映射不同的属性,以便它可以发布list person 来源:JERSEY RESTful - How to work with multiselect checkboxes?这对我有用。

答案 1 :(得分:0)

我使用jersy作为RESTful webservice和angularJS同时发布所有todo对象使用你的person对象同时更新数据

//Define an angular module for our app
var sampleApp = angular.module('sampleApp', ['ngRoute','ngResource']);

sampleApp.config(['$routeProvider',
  function($routeProvider) {
    $routeProvider.
      when('/AddNewOrder', {
        templateUrl: 'add_order.jsp',
        controller: 'AddOrderController'
    }).
when('/TestArray', {
        templateUrl: 'list.html',
        controller: 'TestArrayController'
    }).    



      }).
      otherwise({
        redirectTo: '/AddNewOrder'
      });
}]);
sampleApp.controller( 'TestArrayController', function ( $scope, $location,$http,$resource) {

var array=new Array();
var postObject1 = new Object();
postObject1.description = "testDesc1";
postObject1.summary = "testSummary1";
array.push(postObject1);
var postObject2 = new Object();
postObject2.description = "testDesc2";
postObject2.summary = "testSummary2";
array.push(postObject2);

$http.post("/AngularJS/JerseyWebService/todo/arrayTest", array).success(function(data){
    //Callback function here.
    //"data" is the response from the server.
    $scope.updateMessage=data;
});

HTML页面

<html lang="en">
  <head>
    <title>AngularJS Routing example</title>
    <script src="jquery.min.js" type="text/javascript" language="javascript"></script>
    <script src="xml2json.js" type="text/javascript" language="javascript"></script>
    <script src="angular.js"></script>
    <script src="angularjs/angular-route.js"></script>
    <script src="angularjs/angular-resource.js"></script>
    <script src="app1.js"></script>    
    <script type="text/javascript">
     var jquery = $.noConflict();
</script>
  </head>

  <body ng-app="sampleApp">

    <div class="container">
        <div class="row">
        <div class="col-md-3">
            <ul class="nav">
                <!--<li><a href="#AddNewOrder"> Add New Order </a></li>
                <li><a href="#ShowOrders"> Show Orders</a></li>-->
                <li><a href="#TestArray">Test Array Update</a></li>

            </ul>
            <div>{{updateMessage}}</div>
        </div>
        <div class="col-md-9">
            <div ng-view></div>
        </div>
        </div>
    </div>


  </body>
</html>

Jersy WebService

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

import javax.ws.rs.Consumes;
import javax.ws.rs.FormParam;
import javax.ws.rs.GET;
import javax.ws.rs.POST;
import javax.ws.rs.Path;
import javax.ws.rs.Produces;
import javax.ws.rs.QueryParam;
import javax.ws.rs.core.MediaType;

@Path("/todo")
public class TodoResource {

    @POST
    @Path("/arrayTest")
    @Consumes({ MediaType.APPLICATION_JSON })
    @Produces({ MediaType.APPLICATION_JSON })

    public String wantsJSONArray(List<Todo> array)
    {

        System.out.println("---------------------------------------------------"+array);
        // here's your array
        return "success";

    }

}

Todo.java

import javax.xml.bind.annotation.XmlRootElement;

@XmlRootElement
// JAX-RS supports an automatic mapping from JAXB annotated class to XML and JSON
// Isn't that cool?
public class Todo {
  private String summary;
  private String description;
  public String getSummary() {
    return summary;
  }
  public void setSummary(String summary) {
    this.summary = summary;
  }
  public String getDescription() {
    return description;
  }
  public void setDescription(String description) {
    this.description = description;
  }
public String toString(){
    return "{summary:"+summary+"description:"+description+"}";
}

} 

web.xml条目

<servlet>
<servlet-name>JerseyWebService</servlet-name>
<servlet-class>com.sun.jersey.spi.container.servlet.ServletContainer</servlet-class>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>JerseyWebService</servlet-name>
<url-pattern>/JerseyWebService/*</url-pattern>
</servlet-mapping>