以对象作为参数的GET Web服务方法

时间:2018-01-11 17:36:43

标签: java rest web-services

我只需要创建一个Web service来获取一些资源。已存在一种方法,可以访问这些资源。我只需要编写一个Web service方法,即:

    public ArrayList<RequirementObject> getRequirmentsBeneathRequirementSpecification(Context context, RequirementSpecificationObject requirementSpecificationObject)
    throws Exception{
    ArrayList<RequirementObject> requirementObjectList = new ArrayList();


    DomainObject dom = new DomainObject(
      requirementSpecificationObject.getRequirementSpecificationBusinessObject().getObjectId());
    String relType = Common.RELATIONSHIP_TYPE_SPECIFICATION_STRUCTURE;
    String objType = Common.OBJECT_TYPE_REQUIREMENT;
    StringList objSelectList = new StringList("id");
    short recurseLevel = 1;



    MapList relBusObjPageList = dom.getRelatedObjects(context, relType, objType, objSelectList, null, true, true, 
      recurseLevel, "", "");
    for (int ii = 0; ii < relBusObjPageList.size(); ii++)
    {
      String requirementId = (String)((Map)relBusObjPageList.get(ii)).get("id");


      RequirementObject requirementObject = new RequirementObject(new BusinessObject(requirementId));


      requirementObjectList.add(requirementObject);
    }
    return requirementObjectList;
}

并且在此方法中作为参数传递的RequirementSpecificationObject是:

package com.encoe.simulia.objects;
import com.matrixone.json.JSONArray;
import com.matrixone.json.JSONObject;
import java.util.ArrayList;
import java.util.Iterator;
import matrix.db.BusinessObject;
import matrix.util.MatrixException;

public class RequirementSpecificationObject
{
  private BusinessObject _requirementSpecificationBusinessObject = null;
  private ArrayList<RequirementObject> _requirementObjectList = null;

  public RequirementSpecificationObject(BusinessObject requirementSpecificationBusinessObject)
  {
    setRequirementSpecificationBusinessObject(requirementSpecificationBusinessObject);
  }

  public BusinessObject getRequirementSpecificationBusinessObject()
  {
    return this._requirementSpecificationBusinessObject;
  }

  public ArrayList<RequirementObject> getRequirementObjectList()
  {
    return this._requirementObjectList;
  }

  public void setRequirementSpecificationBusinessObject(BusinessObject requirementSpecificationBusinessObject)
  {
    this._requirementSpecificationBusinessObject = requirementSpecificationBusinessObject;
  }

  public void setRequirementObjectList(ArrayList<RequirementObject> requirementObjectList)
  {
    this._requirementObjectList = requirementObjectList;
  }

  public String toString()
  {
    String msg = "--- Requirement Specification ---";
    msg = "Requirement Specification Id: " + getRequirementSpecificationBusinessObject().getObjectId();
    msg = "Requirement Specification Name: " + getRequirementSpecificationBusinessObject().getName();
    if ((getRequirementObjectList() != null) && (!getRequirementObjectList().isEmpty()))
    {
      msg = msg + "------ Requirement List ------";
      for (RequirementObject requirementObject : getRequirementObjectList()) {
        msg = msg + requirementObject;
      }
      msg = msg + "------------------------------";
    }
    return msg;
  }

  public JSONObject toJSON()
    throws MatrixException
  {
    JSONObject jsonObject = new JSONObject();
    BusinessObject bus = getRequirementSpecificationBusinessObject();

    jsonObject.put("id", bus.getObjectId());
    if ((getRequirementObjectList() != null) && (!getRequirementObjectList().isEmpty()))
    {
      JSONObject requirementListObject = new JSONObject();
      JSONArray jsonArray = new JSONArray();
      for (RequirementObject requirementObject : getRequirementObjectList()) {
        jsonArray.put(requirementObject.toJSON());
      }
      requirementListObject.put("Requirement List", jsonArray);
      jsonObject.put("Requirement", requirementListObject);
    }
    return jsonObject;
  }

  public TestCaseObject getTestCaseByTestCaseId(String testCaseId)
  {
    ArrayList<RequirementObject> requirementObjects = getRequirementObjectList();
    Iterator localIterator2;
    for (Iterator localIterator1 = requirementObjects.iterator(); localIterator1.hasNext(); localIterator2.hasNext())
    {
      RequirementObject requirementObject = (RequirementObject)localIterator1.next();
      ArrayList<TestCaseObject> testCaseObjects = requirementObject.getTestCaseObjectList();
      localIterator2 = testCaseObjects.iterator(); continue;TestCaseObject testCaseObject = (TestCaseObject)localIterator2.next();
      if (testCaseObject.getTestCaseBusinessObject().getObjectId().equals(testCaseId)) {
        return testCaseObject;
      }
    }
    return null;
  }

  public TestExecutionParameterObject getTestExecutionParameterObjectByTitle(String title)
  {
    Iterator localIterator2;
    for (Iterator localIterator1 = getRequirementObjectList().iterator(); localIterator1.hasNext(); localIterator2.hasNext())
    {
      RequirementObject requirementObject = (RequirementObject)localIterator1.next();
      localIterator2 = requirementObject.getTestCaseObjectList().iterator(); continue;TestCaseObject testCaseObject = (TestCaseObject)localIterator2.next();

      Iterator localIterator3 = testCaseObject.getTestCaseParameterObjectList().iterator();
      while (localIterator3.hasNext())
      {
        TestCaseParameterObject testCaseParameterObject = (TestCaseParameterObject)localIterator3.next();
        if (testCaseParameterObject.getTestCaseParameterObject().getName().contains(title)) {
          return testCaseObject.getTestExecutionParameterByTitle(title);
        }
      }
    }
    return null;
  }
}

所以我想写的Web服务方法应该是这样的:

@GET
   @Path("/getRequirements")
   public Response getRequirements(@javax.ws.core.context HttpServletRequest request, ????????????)
   {
       output = new JSONObject();
       matrix.db.Context context = null;
       try
       {
           output.put("msg", "KO");
           try
           {
               boolean isSCMandatory = false;
               context = getAuthenticatedContext(request, isSCMandatory);

               WartsilaMethods wartsilaMethods = new WartsilaMethods();

               RequirementSpecificationObject requirementSpecificationObject = wartsilaMethods.getRequirmentsBeneathRequirementSpecification(context,??????);
               output.put("Requirement Specification Object", requirementSpecificationObject.toJSON());
               output.put("msg", "OK");
           }
           catch (Exception e)
           {
               output.put("msg", e.getMessage());
               e.printStackTrace();
           }
           return Response.status(200).entity(output.toString()).build();
       }
       catch (Exception e1)
       {
           e1.printStackTrace();
       }
   }

我的问题是我不知道如何在Web service调用查询中传递该对象(用????表示的字段)。

非常感谢你的帮助!

0 个答案:

没有答案