我定义了两个项目1)GeneralOrm
用于通用Web服务,2)CompanyWS
包括公司特定的Web服务。
我正在使用Spring和Hibernate 4版本
我正在编写一个GET请求,根据两个参数employeeID
和informationID
来提取信息。只能有一个employeeID和多个
informationID。根据employeeID和Information ID,我想在我的JSON结果中显示employeeID,INFORMATION_ID,VALUE_DISPLAY_NAME和value_emp_ID。
数据库中的我的表EMP_METADATA
如下所示:
column_Name Data_Type
EMPLOYEE_ID NUMBER
INFORMATION_ID NUMBER
VALUE_DISPLAY_NAME VARCHAR2(30 BYTE)
VALUE_EMP_ID NUMBER
在CompanyWS
项内部,我按照以下方式在控制器内定义了GET请求:
内包[{1}}:CompanyWS
edu.abc.company.controller
我假设因为我的@RequestMapping(value="/get_em_metadata", method=RequestMethod.GET)
public String getEmpMetaData
(
@RequestParam(value="employee_id", defaultValue="0") Integer employeeID_,
@RequestParam(value="information_id", defaultValue="0") Integer informationID_
)
{
List<EmployeeMetaData> cvmetadata = null;
GetEmployeeResult result = new GetEmployeeResult();
try{
EmployeeMetaDataDao rmDao = (EmployeeMetaDataDao)context.getBean("EmployeeMetaDataDao");
List<EmployeeMetaData> rm = rmDao.findByEmpAndInfoId(employeeID_, informationID_);
if(rm != null) && (!rm.isEmpty()){
System.out.println("Checking Aug 31:"+rm); // This works and print outs on the console
}
} catch(Throwable th) {
th.printStackTrace();
result.setWebServiceStatus(th.getMessage(), false);
}
//return result.toJSON();
}
语句在运行webservice后在控制台上打印了以下内容,
System.out.print
我已经过了一半,我只需要以JSON格式打印结果。为了打印结果,我定义了Checking Aug 31:[edu.abc.company.orm.EmployeeMetaData@5963b830]
,如下所示并扩展了WebServiceResult类。我想知道我是否需要使用GetEmployeeResult
并扩展GetEmployeeResult
类,或者我可以直接在我的控制器中使用WebServiceResult类来
以JSON格式打印结果?任何想法我应该如何继续在控制器内打印JSON的代码。我之前没有使用过Hibernate。提前谢谢。
WebserviceResult
这里是package edu.abc.company.json;
import java.util.List;
import edu.abc.company.domain.CvMetaDataList;
import edu.abc.company.domain.companyMetaDataList;
import edu.abc.company.util.WebServiceResult;
public class GetEmployeeResult extends WebServiceResult {
}
类,其定义如下:
WebServiceResult
以下仅供参考:如果有人有兴趣查看它:
我已在package edu.abc.company.util;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.SerializationFeature;
import edu.abc.company.domain.StatusMessage;
import edu.abc.company.json.Views;
// A generic object that can be used to return data and a standardized status message from a web service.
public class WebServiceResult {
protected StatusMessage webservice_status;
// C-tor
public WebServiceResult() {}
// C-tor
public WebServiceResult(StatusMessage webserviceStatus_) {
webservice_status = webserviceStatus_;
}
// Web service status
public StatusMessage getWebServiceStatus() {
return webservice_status;
}
public void setWebServiceStatus(String message_, boolean success_) {
webservice_status = new StatusMessage();
if (success_) {
webservice_status.setStatus(Constants.SUCCESS); // Constants is another class which has messages defined, not including here
webservice_status.setMessage(message_);
} else {
webservice_status.setStatus(Constants.ERROR);
webservice_status.setMessage(message_);
}
}
// Export the object's contents as JSON.
public String toJSON(boolean pretty_) {
String json = "";
try {
ObjectMapper objectMapper = new ObjectMapper();
objectMapper.configure(SerializationFeature.INDENT_OUTPUT, pretty_);
// Convert the object to JSON.
json = objectMapper.writerWithView(Views.Normal.class).writeValueAsString(this);
}
catch(Exception e) {
e.printStackTrace();
}
return json;
}
public String toJSON() {
return toJSON(true);
}
}
项目中定义了EmployeeMetaData
,如下所示:
内包[{1}}:GeneralOrm
GeneralOrm
我已在edu.abc.company.orm
项目中定义了package edu.abc.company.orm;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.Id;
import javax.persistence.Table;
@Entity
@Table(name="EMP_METADATA")
public class EmployeeMetaData
{
public int getEmployeeId() {
return employeeId;
}
public void setEmployeeId(int employeeId) {
this.employeeId = employeeId;
}
public int getInformationId() {
return informationId;
}
public void setInformationId(int informationId) {
this.informationId = informationId;
}
public String getValueDisplayName() {
return valueDisplayName;
}
public void setValueDisplayName(String valueDisplayName) {
this.valueDisplayName = valueDisplayName;
}
public int getValueempId() {
return valueempId;
}
public void setValueEmpId(int valueempId) {
this.valueempId = valueempId;
}
@Id
@Column(name="EMPLOYEE_ID")
private int employeeId;
@Column(name="INFORMATION_ID")
private int informationId;
@Column(name="VALUE_DISPLAY_NAME")
private String valueDisplayName;
@Column(name="VALUE_EMP_ID")
private int valueempId;
}
,如下所示:
内部项目EmployeeMetaDataDao
:GeneralOrm
GeneralOrm