0

I am trying to create a REST web service that returns the details of a user.

Here is my code:

//Actual web service methods implemented from here
    @GET
    @Path("login/{email}/{password}")
    @Produces("application/json")
    public Tourist loginUser(@PathParam("email") String email, @PathParam("password") String password) {
        List<Tourist> tourists = super.findAll();
        for (Tourist tourist : tourists) {
            if (tourist.getEmail().equals(email) && tourist.getPassword().equals(password)) {
                return tourist;
            }
        }
        //if we got here the login failed
        return null;
    }

This produces the following JSON:

{
    "email": "[email protected]",
    "fname": "Adrian",
    "lname": "Olar",
    "touristId": 1
}

What i need is:

    {"tourist":{
            "email": "[email protected]",
            "fname": "Adrian",
            "lname": "Olar",
            "touristId": 1
        }
    }

What would i need to add to my code to produce this?

8
  • That's not a valid JSON so you can't without violating the spec. Are you sure that's exactly what you want? Commented May 6, 2014 at 12:40
  • This is not a REST service. Where are the links in the json? Commented May 6, 2014 at 13:22
  • @inf3rno Links are not necessary for REST. Commented May 6, 2014 at 14:01
  • 1
    I'm not sure if this is just intended as an example, but you should never submit password information over GET, as it will get cached and inspected. Commented May 6, 2014 at 14:03
  • 1
    @Tichodromamuraria You are terribly wrong. en.wikipedia.org/wiki/HATEOAS REST should always return hypermedia, not just plain data. Commented May 6, 2014 at 14:06

1 Answer 1

1

If you really want to wrap a Tourist into another object, you can do this.

Tourist.java:

package entities;

import javax.xml.bind.annotation.XmlRootElement;

@XmlRootElement
public class Tourist {

    int touristId;
    String email;
    String fname;
    String lname;

TouristWrapper.java:

package entities;

import javax.xml.bind.annotation.XmlRootElement;

@XmlRootElement
public class TouristWrapper {

    Tourist tourist;

SOResource.java:

package rest;

import entities.Tourist;
import entities.TouristWrapper;
import javax.ws.rs.GET;
import javax.ws.rs.Path;
import javax.ws.rs.PathParam;
import javax.ws.rs.Produces;

@Path("/so")
public class SOResource {

    @GET
    @Path("/tourists/{id}")
    @Produces("application/json")
    public TouristWrapper loginUser(@PathParam("id") int id) {
        Tourist tourist = new Tourist(id, "[email protected]", "John", "Doe");
        TouristWrapper touristWrapper = new TouristWrapper(tourist);
        return touristWrapper;
    }
}

I have simplified your usecase but you get the point: No Tourist is returned but a TouristWrapper. The JSON returned is this:

{
    "tourist": {
        "email": "[email protected]",
        "fname": "John",
        "lname": "Doe",
        "touristId": 1
    }
}
Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.