【发布时间】:2015-04-10 06:07:10
【问题描述】:
我做了这个简单的 RESP 应用程序,用于将博客条目输入到 html 页面,这是一个示例代码.. 任何想法为什么?当我用谷歌搜索时,它看起来应该没问题..我向您展示了我所做的..每个条目都有它的 ID,它保存在一个数组列表中,并且只有一个简单的属性文本..我想稍后添加一些特殊命令,所以我也可以在命令行中添加一个事件。我想添加一个函数,我可以将其标记为已读/未读,默认为未读。任何想法如何做到这一点?
谢谢
@Path("/")
public class RootResource {
private final EntryListResource entries = new EntryListResource();
@GET
@Path("favicon.ico")
public Response getFavicon() {
return Response.noContent().build();
}
@GET
public Response get(@Context UriInfo uriInfo) {
final URI location = uriInfo.getAbsolutePathBuilder().path("/entries").build();
return Response.seeOther(location).build();
}
@Path("entries")
public EntryListResource getEntries() {
return entries;
}
}
和
@XmlRootElement(name = "entryRef")
public class EntryResourceRef {
private int id;
private URI href;
@XmlAttribute
public URI getHref() {
return href;
}
@XmlAttribute
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public void setHref(URI href) {
this.href = href;
}
}
终于
@XmlRootElement(name = "entry")
@XmlAccessorType(XmlAccessType.NONE)
public class EntryResource {
private int id;
private String text;
@GET
@Produces(MediaType.APPLICATION_XML)
public Response get() {
return Response.ok(this).build();
}
@GET
@Produces(MediaType.TEXT_HTML)
public Response getHTML(@Context UriInfo uriInfo) {
final StringBuilder sb = new StringBuilder();
@PUT
@Consumes(MediaType.TEXT_PLAIN)
public Response updatePlain(String text,String description, @Context UriInfo uriInfo) {
return update(text, description, uriInfo.getAbsolutePath());
}
@PUT
@Consumes(MediaType.APPLICATION_FORM_URLENCODED)
public Response updateAsForm(@FormParam("text") String text, @FormParam("description") String description,@Context UriInfo uriInfo) {
return update(description,text, uriInfo.getAbsolutePath());
}
private Response update(String text,String description, URI self) {
if (getText().equals(text)) {
throw new WebApplicationException(
Response
.status(Response.Status.CONFLICT)
.entity("The blog entry text has not been modified")
.type(MediaType.TEXT_PLAIN)
.build()
);
}
setText(text);
return Response.seeOther(self).build();
}
来源
@XmlRootElement(name = "entries")
@XmlAccessorType(XmlAccessType.NONE)
public class EntryListResource {
private final AtomicInteger nextEntryId = new AtomicInteger(0);
private UriInfo uriInfo = null;
@GET
@Produces(MediaType.APPLICATION_XML)
public Response get() {
return Response.ok(this).cacheControl(CC).build();
}
@XmlElement(name = "blog")
public Collection<EntryResourceRef> getEntries() {
final Collection<EntryResourceRef> refs = new ArrayList<EntryResourceRef>();
for (final EntryResource entry : entries.values()) {
final EntryResourceRef ref = new EntryResourceRef();
ref.setId(entry.getId());
ref.setHref(getEntryUri(entry));
refs.add(ref);
}
return refs;
}
【问题讨论】: