●Entity:GroupData.java
@XmlRootElement
@Entity
public class GroupData {
@Id @GeneratedValue(strategy = GenerationType.IDENTITY)
private int GroupID;
private String GroupName;
@Temporal(TemporalType.TIMESTAMP)
private Date CreateDate;
// getter & setter
public int getGroupID() {
return GroupID;
}
public String getGroupName() {
return GroupName;
}
public void setGroupName(String groupName) {
GroupName = groupName;
}
public Date getCreateDate() {
return CreateDate;
}
public void setCreateDate(Date createDate) {
CreateDate = createDate;
}
}
●RESTful Service:GroupDataResource.java
@Path("/Group")
public class GroupDataResource {
@GET
@Path("{groupid}")
@Produces(MediaType.APPLICATION_JSON )
public GroupData getGroup(@PathParam("groupid") int groupID) {
GroupData gData;
//操作並取回資料gData,略...
return gData;
}
}
成果如下圖
1. 去掉@XmlRootElement
2. 在web.xml加入highlight處
< servlet>
< servlet-name>Jersey REST Service</ servlet-name>
< servlet-class>com.sun.jersey.spi.container.servlet.ServletContainer</ servlet-class>
< init-param>
< param-name>com.sun.jersey.config.property.packages</ param-name>
< param-value>com.quanta.WebDemo1</ param-value>
</ init-param>
< init-param>
< param-name>com.sun.jersey.api.json.POJOMappingFeature</ param-name>
< param-value>true</ param-value>
</ init-param>
< load-on-startup>1</ load-on-startup>
</ servlet>
< servlet-mapping>
< servlet-name>Jersey REST Service</ servlet-name>
< url-pattern>/rest/*</ url-pattern>
</ servlet-mapping>
解決後的成果
讚讚~也是JSON格式,但是Date type的資料怪怪的@@
下一篇來解決這個問題吧XD
沒有留言:
張貼留言