0

要消耗球衣中的单个对象,我有这样的方法

 @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();

      }

我做了一些谷歌搜索,但没有找到任何工作示例。

4

2 回答 2

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以便它可以发布 来源:JERSEY RESTful - 如何使用多选复选框?这对我有用。listperson

于 2013-09-04T11:40:56.813 回答
0

我使用 jersy 作为 RESTful web 服务和 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 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>

杰西网络服务

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>
于 2013-11-19T09:48:15.510 回答