簡體   English   中英

Java @PathParam始終為null

[英]Java @PathParam always null

我正在嘗試使用Jersey使用@PathParam,但它總是將其視為null。

這是方法:網址為http://localhost:8080/GiftRegistryAPI/api/v2/inventory/david其中/v2/inventory在類級別

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

}

調試時的name參數始終為null,我無法找到任何方法讓它識別出我輸入的內容。

什么想法可能會出錯?

這是我的進口聲明

import javax.websocket.server.PathParam;

本來應該

import javax.ws.rs.PathParam;

在此錯誤中,大多數時候問題導入錯誤。 只要確保你有import javax.ws.rs.PathParam;

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM