Priyamal Priyamal - 3 months ago 7
Java Question

unlimited outputs while trying to access restful web service in spring mvc

i am using spring DAO and spring mvc in my project, once i try to access
below url i'm getting huge no of json and that won't stop untill i stop the program.i cant figure this out can some one explain what's happening here.

url :- //http://localhost:8080/plans/11/more

//controller

@ResponseBody
@RequestMapping(value="plans/{pid}/more",method=RequestMethod.GET)
public Travel showMore(@PathVariable int pid ){
Travel travel = travelRep.findOne(pid);
return travel;
}


model class

public class Travel implements Serializable {

private static final long serialVersionUID = 1L;
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
@Basic(optional = false)

private Integer idtravel;

@OneToMany(cascade = CascadeType.ALL, mappedBy = "idtravel")
private Set<Travellocation> travellocationSet;

@OneToMany(cascade = CascadeType.ALL, mappedBy = "idtravel")
private Set<Transactions> transactionsSet;

@OneToMany(cascade = CascadeType.ALL, mappedBy = "idtravel")
private Set<Travelmode> travelmodeSet;
}


//assume that there are getters and setters for the properties in the model class.

image of the output.

Answer

travellocationSet and idLocation seem to have a reference to eachother. Spring will go through all the getters so something like this will happen:

idLocation = travellocationSet->getIdLocation();
travellocationSet = idLocation->getTravellocationSet();
idLocation = travellocationSet->getIdLocation();
travellocationSet = idLocation->getTravellocationSet();
// This will continue forever

A solution can be to use the Data Transfer Object pattern. Basically you would copy your data to a new object, which can be used for your viewing part.

Comments