dimitrisd
dimitrisd

Reputation: 652

500 Internal Server Error in RESTfull

When I try to access this resource this I get this error message 500 Internal Server Error

I am new to web services and I a trying to realize the issue

Rest Service

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

Client

@Test
    public void TestP1() {

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

Upvotes: 0

Views: 844

Answers (2)

Vlad
Vlad

Reputation: 10780

An error occurs in Jersey when trying to serialize the primitive int to JSON. The same will happen if you use an Integer. Use a wrapper object/DTO for your return type (see JAX-RS / Jersey ".get(Integer.class)" and single JSON primitive (Integer) values?)

Upvotes: 1

AlexR
AlexR

Reputation: 115328

I guess that you have NullPointerException in this line:

if (!userItineraries.containsKey(userId)) {

It happens because userItineraries is not initialized. As far as I understand you try to implement some kind of cache using map. In this case initialize it:

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

Upvotes: 2

Related Questions