Java REST-将条目标记为已读/未读

Java REST-将条目标记为已读/未读,java,string,rest,Java,String,Rest,我做了这个简单的RESP应用程序,用于将博客条目输入到html页面,下面是一个示例代码。。你知道为什么吗?当我在谷歌上搜索时,它看起来应该没问题。。我在向你展示我所做的。。每个条目都有其id,保存在arraylist中,并且只有一个简单的属性文本。。我想稍后添加一些特殊命令,这样我也可以在命令行中添加一个事件。。我想添加一个函数,我可以将其标记为已读/未读,默认为未读。。有什么办法吗 谢谢 @Path("/") public class RootResource { private final

我做了这个简单的RESP应用程序,用于将博客条目输入到html页面,下面是一个示例代码。。你知道为什么吗?当我在谷歌上搜索时,它看起来应该没问题。。我在向你展示我所做的。。每个条目都有其id,保存在arraylist中,并且只有一个简单的属性文本。。我想稍后添加一些特殊命令,这样我也可以在命令行中添加一个事件。。我想添加一个函数,我可以将其标记为已读/未读,默认为未读。。有什么办法吗

谢谢

@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 = "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;
}
@XmlRootElement(name=“entries”)
@XmlAccessorType(XmlAccessType.NONE)
公共类EntryListResource{
private final AtomicInteger nextEntryId=新的AtomicInteger(0);
private-UriInfo-UriInfo=null;
@得到
@生成(MediaType.APPLICATION\u XML)
公众反应{
返回Response.ok(this.cacheControl(CC.build());
}
@xmlement(name=“blog”)
公共集合getEntries(){
最终集合引用=新的ArrayList();
for(最终EntryResource条目:entries.values()){
最终EntryResourceRef=新EntryResourceRef();
ref.setId(entry.getId());
ref.setHref(getEntryUri(条目));
参考。添加(参考);
}
返回参考文献;
}

您可以使用0和1,这将帮助您对结果进行排序

在获取记录时,您可以在查询本身中附加“已读”或“未读”

Select news.*, 
(case news.STATUS
when 0 then 'Unread' 
when 1 then 'Read' 
end) as Status
from News news
这是一个本机SQL,也可以在HQL中执行

另一种解决方案是,您可以在新闻实体中使用枚举。这样,您可以在UI和DB中显示字符串,它将保存序号值,即0或1。下面给出了示例代码

@Table(name = "NEW")
public class New 
{
    private NewsStatus newsStatus;
}


public static enum NewsStatus {
    READ{
        public String toString(){
            return "Read";
        }
    }, 
    UNREAD{
        public String toString(){
            return "Unread";
        }
    }
    public int getValue(){
        return this.ordinal() + 1;
    }
    public String getLabel(){
        return this.toString();
    }
    public static NewsStatus getByLabel(String label) 
    {
        if(label == null)
            return null;
        for(NewsStatus ns : NewsStatus.values()) {
            if(label.equalsIgnoreCase(ns.getLabel()))
                return ns;
        }
        return null;
    }
    public static NewsStatus getByValue(int value){
        return NewsStatus.values()[value-1];
    }
}
如果需要进一步的细节,请告诉我


Krish

谢谢。我只是在将其插入JSF时遇到问题。我应该使用buttoncommand调用这里的哪个方法?
@Table(name = "NEW")
public class New 
{
    private NewsStatus newsStatus;
}


public static enum NewsStatus {
    READ{
        public String toString(){
            return "Read";
        }
    }, 
    UNREAD{
        public String toString(){
            return "Unread";
        }
    }
    public int getValue(){
        return this.ordinal() + 1;
    }
    public String getLabel(){
        return this.toString();
    }
    public static NewsStatus getByLabel(String label) 
    {
        if(label == null)
            return null;
        for(NewsStatus ns : NewsStatus.values()) {
            if(label.equalsIgnoreCase(ns.getLabel()))
                return ns;
        }
        return null;
    }
    public static NewsStatus getByValue(int value){
        return NewsStatus.values()[value-1];
    }
}