Note that there are some explanatory texts on larger screens.

plurals
  1. POHibernate Entity to Json object with jackson to http post
    primarykey
    data
    text
    <p>I'm trying to create a json object from my hibernate entity using jackson which can be posted to a remote url using jersey. I can't seem to figure out how to convert my hibernate entity to a json object. Using resteasy-jackson I was able to create my own web service accessible by localhost which outputted my json object correctly within my screen, but I was hoping to build the json object without having to use a webservice to my own app. Perhaps I'm going about this all the wrong way? I just don't want to manually have to add every property to a json object. </p> <p>What I've tried thus far, </p> <p>interface</p> <pre><code>@Path("/company") public interface CompanyResource { @GET @Produces("application/json") public List&lt;Company&gt; getAllDomains(); @POST @Produces("application/json") public Response post(Company company); @GET @Path("{id}") @Produces("application/json") public Company getDomainObject(@PathParam("id") Integer id); } </code></pre> <p>class</p> <pre><code>public List&lt;Company&gt; getAllDomains() { return this.companyDAO.findAllCompanies(); } public Response post(Company company) { companyDAO.updateCompany(company); return Response.ok().build(); } public Company getDomainObject(@PathParam("id") Integer id) { Company domainObject = this.companyDAO.findCompanyById(id); if (domainObject == null) { throw new WebApplicationException(Response.Status.NOT_FOUND); } return domainObject; } </code></pre> <p>post service</p> <pre><code>public void setupRender() throws GeneralSecurityException, UnsupportedEncodingException { try { Client client = Client.create(); String url = kayakoWebService.generateURL(); WebResource webResource = client.resource(url); ClientResponse response = webResource.accept("application/json").get(ClientResponse.class); if (response.getStatus() != 200) { throw new RuntimeException("Failed : HTTP error code : " + response.getStatus()); } //Outputs object rather than json System.out.println("test " + companyResource.getDomainObject(1)); } catch (Exception e) { e.printStackTrace(); } } </code></pre> <p>Company.class</p> <pre><code>@Entity @Cache(usage = CacheConcurrencyStrategy.READ_WRITE) public class Company extends StatefulEntity { @Validate("maxLength=50,required") private String name; @Column(nullable = true) @Temporal(TemporalType.TIMESTAMP) private Date createDate; @Column(nullable = true) @Temporal(TemporalType.TIMESTAMP) private Date cancelDate; @Column(nullable = true) @Temporal(TemporalType.TIMESTAMP) private Date modifyDate; @ManyToOne @JoinColumn(name="parent_id") private Company parent; @Column(nullable = true, length = 5) private Integer roomUnitCount; @Column(nullable = false, length = 8) private String accountNumber; @JsonIgnore public String getAccountNumber() { return accountNumber; } public void setAccountNumber(String accountNumber) { this.accountNumber = accountNumber; } //additional getters setters } </code></pre> <p>cfg.xml</p> <pre><code>&lt;?xml version="1.0" encoding="UTF-8"?&gt; &lt;!DOCTYPE hibernate-configuration PUBLIC "-//Hibernate/Hibernate Configuration DTD 3.0//EN" "http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd"&gt; &lt;hibernate-configuration&gt; &lt;session-factory&gt; &lt;property name="hibernate.connection.datasource"&gt;jdbc/domain&lt;/property&gt; &lt;property name="hbm2ddl.auto"&gt;validate&lt;/property&gt; &lt;property name="hibernate.format_sql"&gt;false&lt;/property&gt; &lt;property name="hibernate.show_sql"&gt;false&lt;/property&gt; &lt;property name="hibernate.cache.use_second_level_cache"&gt;true&lt;/property&gt; &lt;property name="hibernate.cache.use_query_cache"&gt;true&lt;/property&gt; &lt;property name="hibernate.cache.region.factory_class"&gt;net.sf.ehcache.hibernate.EhCacheRegionFactory&lt;/property&gt; &lt;property name="hibernate.generate_statistics"&gt;true&lt;/property&gt; &lt;/session-factory&gt; &lt;/hibernate-configuration&gt; </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload