当我尝试访问此资源时,我收到此错误消息 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);
答案 0 :(得分:2)
我猜你在这行中有NullPointerException
:
if (!userItineraries.containsKey(userId)) {
这是因为userItineraries
未初始化。据我了解,您尝试使用map实现某种缓存。在这种情况下初始化它:
private Map<String, List<Itinerary>> userItineraries = new HashMap<String, List<Itinerary>>();
答案 1 :(得分:1)
在尝试将基元int
序列化为JSON时,Jersey发生错误。如果您使用Integer
,也会发生同样的情况。使用包装器对象/ DTO作为返回类型(请参阅JAX-RS / Jersey ".get(Integer.class)" and single JSON primitive (Integer) values?)