2

I'm trying to use the @PathParam using Jersey, but it always sees it as null.

Here's the method: The url is http://localhost:8080/GiftRegistryAPI/api/v2/inventory/david with /v2/inventory being at the class level

package com.omar.rest.inventory;

import javax.websocket.server.PathParam;
import javax.ws.rs.GET;
import javax.ws.rs.Path;
import javax.ws.rs.Produces;
import javax.ws.rs.QueryParam;
import javax.ws.rs.core.Context;
import javax.ws.rs.core.MediaType;
import javax.ws.rs.core.Request;
import javax.ws.rs.core.Response;

import org.codehaus.jettison.json.JSONArray;

import com.omar.rest.util.*;

@Path("/v2/inventory")
public class V2_Inventory {

@GET
@Produces(MediaType.APPLICATION_JSON)
public Response returnHostRegistries(@QueryParam("hostId") int hostId) throws Exception {

    String returnString = null;
    JSONArray jsonArray = new JSONArray();

    try {
        // A host ID of 0 indicates a null parameter, there will never be a host with an ID of 0
        if (hostId == 0) { 
            return Response.status(400).entity("Error: please provide a valid host ID for this search").build();
        }

        Schema dao = new Schema();
        jsonArray = dao.qryReturnHostRegistries(hostId);

        returnString = jsonArray.toString();
    }
    catch (Exception e) {
        e.printStackTrace();
        return Response.status(500).entity("Server was not able to process your request").build();
    }
    System.out.println(returnString);

    return Response.ok(returnString).build();

}

@Path("/{firstName}")
@GET
@Produces(MediaType.APPLICATION_JSON)
public Response returnSearchedRegistries(@PathParam("firstName") String name) throws Exception{

    String returnString = null;
    JSONArray jsonArray = new JSONArray();

    System.out.println("Name: " +name);
    try {
        Schema dao = new Schema();
        jsonArray = dao.qryReturnHostRegistries(name);

        returnString = jsonArray.toString();
    }
    catch (Exception e) {
        e.printStackTrace();
        return Response.status(500).entity("Server was not able to process your request").build();
    }

    System.out.println(returnString);

    return Response.ok(returnString).build();
}

}

The name parameter when debugged is always null, and I can't find any way at all of getting it to recognise I've entered anything in.

Any ideas what might be going wrong?

2
  • can you put all class? its impossible to guess when the root address is not there Commented Jan 9, 2015 at 14:54
  • It was only when I copied it into the post above I noticed an incorrect import statement. Changed the import statement and it works Commented Jan 9, 2015 at 15:05

2 Answers 2

15

It was my import statement

import javax.websocket.server.PathParam;

should have been

import javax.ws.rs.PathParam;
Sign up to request clarification or add additional context in comments.

Comments

0

In this error, most of the time the issue is wrong import. Just make sure you have import javax.ws.rs.PathParam;

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.