gpt4 book ai didi

Java @PathParam 始终为 null

转载 作者:行者123 更新时间:2023-12-02 20:02:54 26 4
gpt4 key购买 nike

我尝试使用 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参数始终为空,并且我根本找不到任何方法让它识别我已输入的任何内容。

有什么想法可能会出问题吗?

最佳答案

这是我的导入声明

import javax.websocket.server.PathParam;

应该是

import javax.ws.rs.PathParam;

关于Java @PathParam 始终为 null,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27862860/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com