Reputation: 46
I have problem with REST service I'm trying to make. I use GlassFish 4.1 and Jersay 2.1 which is built-in.
@Path("/driver")
@RequestScoped
public class DriverResource {
private static Logger logger = LogManager.getLogger(DriverResource.class);
@Inject
private DriverManager driverManager;
private SharedResponseFactory responseFactory = new SharedResponseFactory();
@GET
@Path("/login/{pesel}/{password}")
@Produces("application/json")
public Response logIn(@PathParam("pesel") String pesel, @PathParam("password") String password) {
try {
Driver driver = driverManager.logIn(pesel, password);
logger.debug("Zalogowano kierowcę: " + driver.getFullName());
return responseFactory.getSuccesResponse(driver);
} catch (ErrorDAOException e) {
logger.catching(e);
return responseFactory.getFailureResponse(e);
} catch (NoDataFoundDAOException e) {
logger.catching(e);
return responseFactory.getFailureResponse(e);
} catch (Exception e) {
logger.catching(e);
return responseFactory.getFailureResponse(e);
}
}
}
When I'm trying to return my Entity I get response like this:
{}
In my Entities there are many cyclic references and I don't operate on actual implementation but interfaces. I need to make it the way Retrofit in my Android application could deserialize it.
Glassfish's logs are empty, there are no errors related to rest. I have no idea how to make it working.
I tried to use @JsonIdentityInfo
to handle cyclic references and @JsonTypeInfo
to make interfaces possible to desserialize.
I think there's a small trick that will make it working but unfortunately I don't know it...
Upvotes: 0
Views: 1110
Reputation: 473
I found a relatively easy way to test for cyclic references. If you use JAXB (which is included with glassfish), you can try marshalling your entity to XML. A JAXBException is thrown if any cyclic references are found.
Here is a method to marshall an object to XML:
public static <T> String marshalToXml(T instance) throws javax.xml.bind.JAXBException {
JAXBContext jaxbContext = JAXBContext.newInstance(instance.getClass());
Marshaller jaxbMarshaller = jaxbContext.createMarshaller();
jaxbMarshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true);
StringWriter writer = new StringWriter();
jaxbMarshaller.marshal(instance, writer);
return writer.toString();
}
and the service can test the object like this:
@Path("/driver")
@RequestScoped
public class DriverResource {
private static Logger logger = LogManager.getLogger(DriverResource.class);
@Inject
private DriverManager driverManager;
private SharedResponseFactory responseFactory = new SharedResponseFactory();
@GET
@Path("/login/{pesel}/{password}")
@Produces("application/json")
public Response logIn(@PathParam("pesel") String pesel, @PathParam("password") String password) {
try {
Driver driver = driverManager.logIn(pesel, password);
marshalToXml(driver); //remember to import static method
logger.debug("Zalogowano kierowcę: " + driver.getFullName());
return responseFactory.getSuccesResponse(driver);
} catch (ErrorDAOException e) {
logger.catching(e);
return responseFactory.getFailureResponse(e);
} catch (NoDataFoundDAOException e) {
logger.catching(e);
return responseFactory.getFailureResponse(e);
} catch (JAXBException e) {
//view error message to see cyclic reference
logger.catching(e);
return responseFactory.getFailureResponse(e);
} catch (Exception e) {
logger.catching(e);
return responseFactory.getFailureResponse(e);
}
}
}
Upvotes: 1