我正在尝试使用杰克逊从我的休眠实体创建一个 json 对象,该对象可以使用球衣发布到远程 url。我似乎无法弄清楚如何将我的休眠实体转换为 json 对象。使用resteasy-jackson,我能够创建我自己的可由localhost访问的Web服务,它在我的屏幕上正确输出了我的json对象,但我希望构建json对象而不必对我自己的应用程序使用webservice。也许我正在以错误的方式处理这一切?我只是不想手动将每个属性添加到 json 对象。
到目前为止我尝试过的,
界面
@Path("/company")
public interface CompanyResource {
@GET
@Produces("application/json")
public List<Company> getAllDomains();
@POST
@Produces("application/json")
public Response post(Company company);
@GET
@Path("{id}")
@Produces("application/json")
public Company getDomainObject(@PathParam("id") Integer id);
}
班级
public List<Company> 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;
}
邮政服务
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();
}
}
公司类
@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
}
cfg.xml
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE hibernate-configuration PUBLIC "-//Hibernate/Hibernate Configuration DTD 3.0//EN" "http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd">
<hibernate-configuration>
<session-factory>
<property name="hibernate.connection.datasource">jdbc/domain</property>
<property name="hbm2ddl.auto">validate</property>
<property name="hibernate.format_sql">false</property>
<property name="hibernate.show_sql">false</property>
<property name="hibernate.cache.use_second_level_cache">true</property>
<property name="hibernate.cache.use_query_cache">true</property>
<property name="hibernate.cache.region.factory_class">net.sf.ehcache.hibernate.EhCacheRegionFactory</property>
<property name="hibernate.generate_statistics">true</property>
</session-factory>
</hibernate-configuration>