REST Web服务 - JSON请求映射到java Complex对象

时间:2014-06-05 13:31:56

标签: java json rest object mapping

我正在尝试映射并将JSON发送到Java对象以进行Rest Web Service。我在Jersey Java中使用RESTful WS。

我是这个REST的新手,如果我没有正确发送请纠正我。

**这是My JSON的一个例子:

Google Rest插件中的MY Json输入为原始数据:** I am Calling and Testing MY Rest Service using Google Advance Rest Client. 如下

JSON输入: 我需要将它映射到:com.cordys.entity.bean.ClaimTask claimTaskObj

{ “TASKID”:[ “002481F0-8E9F-11E3-FB97-B80661C15631”, “002481F0-8E9F-11E3-FB97-B80661C15635”]}

=============================================== ================================== 我收到了以下错误:

因此,每次发送此请求时,都会出现以下错误:

com.sun.jersey.spi.container.ContainerResponse mapMappableContainerException 严重:MappableContainerException中包含的异常无法映射到响应,重新抛出到HTTP容器 org.codehaus.jackson.map.exc.UnrecognizedPropertyException:无法识别的字段“taskId”(类com.business.entity.bean.ClaimTask),未标记为可忽略  在[来源:org.apache.catalina.connector.CoyoteInputStream@409b5f9c; line:1,column:12](通过引用链:com.business.entity.bean.ClaimTask [“taskId”])     at org.codehaus.jackson.map.exc.UnrecognizedPropertyException.from(UnrecognizedPropertyException.java:53)     at org.codehaus.jackson.map.deser.StdDeserializationContext.unknownFieldException(StdDeserializationContext.java:267)     at org.codehaus.jackson.map.deser.std.StdDeserializer.reportUnknownProperty(StdDeserializer.java:673)     at org.codehaus.jackson.map.deser.std.StdDeserializer.handleUnknownProperty(StdDeserializer.java:659)     at org.codehaus.jackson.map.deser.BeanDeserializer.handleUnknownProperty(BeanDeserializer.java:1365)     在org.codehaus.jackson.map.deser.BeanDeserializer._handleUnknown(BeanDeserializer.java:725)     at org.codehaus.jackson.map.deser.BeanDeserializer.deserializeFromObject(BeanDeserializer.java:703)     在org.codehaus.jackson.map.deser.BeanDeserializer.deserialize(BeanDeserializer.java:580)     at org.codehaus.jackson.map.ObjectMapper._readValue(ObjectMapper.java:2704)     at org.codehaus.jackson.map.ObjectMapper.readValue(ObjectMapper.java:1315)     在org.codehaus.jackson.jaxrs.JacksonJsonProvider.readFrom(JacksonJsonProvider.java:419)     在com.sun.jersey.json.impl.provider.entity.JacksonProviderProxy.readFrom(JacksonProviderProxy.java:139)     at com.sun.jersey.spi.container.ContainerRequest.getEntity(ContainerRequest.java:488)     at com.sun.jersey.server.impl.model.method.dispatch.EntityParamDispatchProvider $ EntityInjecta

=============================================== ================================== 我的Rest Web服务方法是:

我的休息服务方法。

@POST
@ApiResponses(value = {
        @ApiResponse(code = 200, message = STATUS_200),
        @ApiResponse(code = 500, message = STATUS_500)
})
@Path("/claimTaskObj")
@Consumes({ "application/json", "application/xml"})
@Produces({ "application/json", "application/javascript", "application/xml" })
public Response claimTaskObj(
        com.business.entity.bean.ClaimTask claimTaskObj,
        @HeaderParam("SAMLart") String samlArt,
        @HeaderParam("otdsticket") String oTAuthenticationHeader,
        @Context UriInfo uriInfo) {
    System.out.println("TaskService.claimTask()====== IN");
    // Integration with JAXWS
    //System.out.println("oTAuthenticationHeader ="+oTAuthenticationHeader);
    ClaimTaskJaxWsClient claimTaskClient = new ClaimTaskJaxWsClient();
    ClaimTaskResponse jaxWSResponse = null;
    MultivaluedMap<String, String> inputParameters = uriInfo.getQueryParameters();
    inputParameters.add("task_id", claimTaskObj.getTaskId().get(0));

    String callback = inputParameters.getFirst("callback");
    try {
        jaxWSResponse = claimTaskClient.executeSoapRequest(inputParameters, samlArt,oTAuthenticationHeader);
        return Util.getRespone(jaxWSResponse, callback, 200);
    } catch (com.business.jaxws.task.claimtask.businessFaultDetail e) {
        return Util.getRespone(e.getFaultInfo(), callback, 500);
    } 

}

MY Jaxbi Class是:

package com.business.entity.bean;

    import java.io.Serializable;
    import java.util.ArrayList;
    import java.util.List;
    import javax.xml.bind.annotation.XmlAccessType;
    import javax.xml.bind.annotation.XmlAccessorType;
    import javax.xml.bind.annotation.XmlElement;
    import javax.xml.bind.annotation.XmlRootElement;
    import javax.xml.bind.annotation.XmlType;


@XmlAccessorType(XmlAccessType.FIELD)
@XmlType(name = "taskIdObj", propOrder = {
    "taskId"
})
@XmlRootElement(name = "ClaimTask", namespace = "http://schemas.business.com/notification/workflow/1.0")
public class ClaimTask implements Serializable {

    @XmlElement(name = "TaskId", namespace = "http://schemas.business.com/notification/workflow/1.0", required = true)
    protected List<String> taskId;


    public List<String> getTaskId() {
        if (taskId == null) {
            taskId = new ArrayList<String>();
        }
        return this.taskId;
    }

    public void setTaskId(String task_Id) {
         if (this.taskId == null) {
             this.taskId = new ArrayList<String>();
         }
         this.taskId.add(task_Id);
    }
    public void setTaskId(List<String> task_Id) {
     if (this.taskId == null) {
        this.taskId = new ArrayList<String>();
        }
    this.taskId.addAll(task_Id);
   }

  }

1 个答案:

答案 0 :(得分:2)

您可以通过实施以下代码来解决和测试您的网络服务

  ClaimTask claim = new com.howtodoinjava.model.ClaimTask();
  claim.setTaskId("002481F0-8E9F-11E3-FB97-B80661C15631");
  claim.setTaskId("002481F0-8E9F-11E3-FB97-B80661C15635");

  StringWriter writer = new StringWriter();
  JAXBContext jaxbContext = JAXBContext.newInstance(ClaimTask.class);
  Marshaller jaxbMarshaller = jaxbContext.createMarshaller();
  jaxbMarshaller.marshal(claim, writer);

 DefaultHttpClient httpClient = new DefaultHttpClient();

 // Define a postRequest request
 HttpPost postRequest = new HttpPost(
"http://localhost:8090/BPMService/v1/tasks/claimTaskObj");

 // Set the API media type in http content-type header
 postRequest.addHeader("SAMLart","XXXXXX");
 postRequest.addHeader("otdsticket","YYYYYYYYYYYYY");
 postRequest.addHeader("content-type", "application/xml");

// Set the request post body
StringEntity userEntity = new StringEntity(writer.getBuffer().toString());
postRequest.setEntity(userEntity);

// Send the request; It will immediately return the response in
// HttpResponse object if any
HttpResponse response = httpClient.execute(postRequest);

// verify the valid error code first
int statusCode = response.getStatusLine().getStatusCode();