gpt4 book ai didi

java - RESTful 中的 500 内部服务器错误

转载 作者:行者123 更新时间:2023-12-01 22:33:52 26 4
gpt4 key购买 nike

当我尝试访问此资源时,我收到此错误消息500 内部服务器错误

我是网络服务新手,我正在尝试意识到这个问题

休息服务

@Path("travelgood1")
public class TravelGood {

private LameDuck lameDuckClient;
private NiceView niceViewClient;
private SimpleDateFormat parserSDF;
private HashMap<String, List<Itinerary>> userItineraries;
private int lastItineraryId;

public TravelGood() {
LameDuck_Service service = new LameDuck_Service();
lameDuckClient = service.getLameDuck();

NiceView_Service niceViewService = new NiceView_Service();
niceViewClient = niceViewService.getNiceView();

parserSDF = new SimpleDateFormat("yyyy-MM-dd");

userItineraries = new HashMap<String, List<Itinerary>>();
}

@POST
@Path("{uid}/itineraries")
@Produces(MediaType.APPLICATION_JSON)
public int createItinerary(@PathParam("uid") String userId) {
if (!userItineraries.containsKey(userId)) {
userItineraries.put(userId, new LinkedList<Itinerary>());
}

Itinerary itinerary = new Itinerary(++lastItineraryId);
userItineraries.get(userId).add(itinerary);

return itinerary.getId();
}
}

客户端

@Test
public void TestP1() {

WebResource clientItinerariesResource = client.resource("http://localhost:8080/tg/webresources/travelgood1/AnneStrandberg/itineraries");
int itineraryId = clientItinerariesResource.post(Integer.class);

最佳答案

我猜您在这一行中有NullPointerException:

if (!userItineraries.containsKey(userId)) {

发生这种情况是因为 userItineraries 未初始化。据我了解,您尝试使用 map 实现某种缓存。在本例中初始化它:

private Map<String, List<Itinerary>> userItineraries = new HashMap<String, List<Itinerary>>();

关于java - RESTful 中的 500 内部服务器错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27172489/

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