简体   繁体   中英

Java Jersey Jax-WS not receiving JSON parameters

I inherited a large project with many web services. Seems they were created with Jersey Jax-WS. I'm tasked with making one of them accept JSON as a POST request. I can't figure out why the parameters are not received.

I'm using SoapUI to send the request RESTful request. I created a REST project and set the needed Endpoint and Resource values. The Media Type dropdown field is set to application/json. Here is the JSON string I've placed in the textarea immediately below the Media Type field:

{"corp":" 877310","rateCodes":["N6","3Z"],"headend_designator":" 238005"," eqp_protocol_aiu":"M3","eqp_typ_aiu":"JD"}

Now in my java file called BsgHandleResource

@WebService
@Singleton
@Path("/bsghandle")
public class BsgHandleResource {

Further down I have this method:

@POST
@Path("/getBsgHandles")
@Consumes(MediaType.APPLICATION_JSON)
@Produces({ MediaType.APPLICATION_JSON, MediaType.APPLICATION_XML })
public BsgHandleResponse getBsgHandlesJson(
        @PathParam("appkey") String appkey,
        @PathParam("rateCodes") final List<String> rateCodes,
        @PathParam("corp") String corp,
        @PathParam("headend_designator") String headend_designator,
        @PathParam("eqp_protocol_aiu") String eqp_protocol_aiu,
        @PathParam("eqp_typ_aiu") String eqp_typ_aiu,
        @PathParam("forceUpdate") boolean forceUpdate) {

    HttpServletRequest request = getRequestObject();

    logger.debug("getBsgHandlesJson() called for rateCodes="
            + rateCodes.toString() + " from ip" + request.getRemoteAddr());

    return processGetBsgHandleByRateCode(appkey, rateCodes, corp, 
        headend_designator, eqp_protocol_aiu, eqp_typ_aiu, 
                    forceUpdate, request);
}

When I send the request from SoapUI, the output I see in the log file is:

getBsgHandlesJson() called for rateCodes=[]...

My pom.xml file contains these dependencies:

<dependency>
    <groupId>com.sun.jersey</groupId>
    <artifactId>jersey-server</artifactId>
    <version>1.2</version>
</dependency>
<dependency>
    <groupId>com.sun.jersey</groupId>
    <artifactId>jersey-bundle</artifactId>
    <version>1.2</version>
</dependency>
<dependency>
    <groupId>com.sun.jersey</groupId>
    <artifactId>jersey-json</artifactId>
    <version>1.8</version>
</dependency>   

Can someone suggest what I am missing?

Edit: regarding user2004685 suggestion, my code now looks like this:

@POST
@Path("/getBsgHandles")
@Consumes(MediaType.APPLICATION_JSON)
@Produces({ MediaType.APPLICATION_JSON, MediaType.APPLICATION_XML })
public BsgHandleResponse getBsgHandlesJson(BsgHandleRequest obj) {

    HttpServletRequest request = getRequestObject();

    logger.debug("getBsgHandlesJson() called for rateCodes="
            + obj.getRateCodes().toString() + " from ip" + request.getRemoteAddr());

    return processGetBsgHandleByRateCode(obj.getAppkey(), obj.getRateCodes(), obj.getCorp(), 
        obj.getHeadendDesignator(), obj.getEqpProtocolAiu(), obj.getEqpTypAiu(), 
                    obj.getForceUpdate(), request);

}

But now I don't even get any logger output. I do get the following error in the soapui response:

<data contentType="text/plain" contentLength="343"><![CDATA[Unrecognized field "headend_designator" (Class com.our.company.oss.vcwh.queryservice.billing.BsgHandleRequest), not marked as ignorable
 at [Source: org.apache.catalina.connector.CoyoteInputStream@6bb342eb; line: 1, column: 65] (through reference chain: com.our.company.oss.vcwh.queryservice.billing.BsgHandleRequest["headend_designator"])]]></data>

Here are the fields in BsgHandleRequest.java:

@XmlRootElement(name = "bsgHandleRequest")
@XmlAccessorOrder(XmlAccessOrder.ALPHABETICAL)
public class BsgHandleRequest {

    private static final long serialVersionUID = 1L;

    private List<String> rateCodes;
    private String corp;
    private String headend_designator;
    private String eqp_protocol_aiu;
    private String eqp_typ_aiu;
    private String appkey;
    private boolean forceUpdate;

Here is the getter and setter for headend_designator. I don't see anything wrong or different from any of the other ones.

public String getHeadendDesignator() {
    return headend_designator;
}
public void setHeadendDesignator(String headend_designator) {
    this.headend_designator = headend_designator;
}

This is no path parameter in your path @Path("/getBsgHandles") and you are trying to use @PathParam . You should be using a POJO Object instead.

Make a new POJO Class for your Request Object:

public class MyRequestObject {
    private String appkey;
    private List<String> rateCodes;
    private String corp;
    private String headend_designator;
    private String eqp_protocol_aiu;
    private String eqp_typ_aiu;
    private boolean forceUpdate;

    /* Constructor */
    public MyRequestObject() {}

    /* Getter-Setters */
}

Change your code like this to make use of your POJO Object:

@POST
@Path("/getBsgHandles")
    @Consumes(MediaType.APPLICATION_JSON)
@Produces({ MediaType.APPLICATION_JSON, MediaType.APPLICATION_XML })
public BsgHandleResponse getBsgHandlesJson(MyRequestObject obj) {

    HttpServletRequest request = getRequestObject();

    LOGGER.debug("getBsgHandlesJson() called for rateCodes="
            + obj.getRateCodes().toString() + " from ip" + request.getRemoteAddr());

    return processGetBsgHandleByRateCode(obj.getAppkey(), obj.getRateCodes(), obj.getCorp(), 
        obj.getHeadendDesignator(), obj.getEqpProtocolAiu(), obj.getEqpTypAiu(), 
                    obj.getForceUpdate(), request);
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM