Xml工作正常,但json不在jax rs中

时间:2017-09-07 00:38:19

标签: api jersey jax-rs

我正在学习jax rs(jersey)的restful api,我正在制作一个messageResource类,我在其中收到消息。所有的东西都可以正常使用xml但是get函数不适用于json。我已尝试过内容类型并接受标题但它不工作并返回500错误和内容类型text / html。 甚至maven依赖moxy也在pom.xml中设置 任何人都可以帮我找到错误。

    package org.restfulapi.messenger.resources;

    import java.net.URI;
    import java.net.URISyntaxException;
    import java.util.ArrayList;
    import java.util.List;

    import javax.ws.rs.Consumes;
    import javax.ws.rs.DELETE;
    import javax.ws.rs.GET;
    import javax.ws.rs.POST;
    import javax.ws.rs.PUT;
    import javax.ws.rs.Path;
    import javax.ws.rs.PathParam;
    import javax.ws.rs.Produces;
    import javax.ws.rs.QueryParam;
    import javax.ws.rs.core.Context;
    import javax.ws.rs.core.MediaType;
    import javax.ws.rs.core.Response;
    import javax.ws.rs.core.Response.Status;
    import javax.ws.rs.core.UriInfo;

    import org.restfulapi.messenger.services.DataNotFoundException;
    import org.restfulapi.messenger.services.Link;
    import org.restfulapi.messenger.services.Message;
    import org.restfulapi.messenger.services.MessageService;

    @Path("messages")
    @Produces(value= {MediaType.APPLICATION_JSON,MediaType.APPLICATION_XML})
    @Consumes(value= {MediaType.APPLICATION_JSON,MediaType.APPLICATION_XML})
    public class MessageResource 
    {
        MessageService service=new MessageService();

        @GET
        @Path("/{messageID}")
        public Message getMessage(@PathParam("messageID")int id,@Context UriInfo uriinfo )
        {
            Message msg=service.getMessage(id);
            if(service.getMessage(id)==null)
            {
                throw new DataNotFoundException("message with id "+id+"not found");
            }

        return msg;
        }



        @GET
        public List<Message> getMessages(@QueryParam("year")int year,
                                                @QueryParam("start") int start,
                                                @QueryParam("size") int size)
        {
            if(year>0)
            {
                return service.getbyyear(year);
            }
            if(start>0 & size>0)
            {
                return service.get(start, size);
            }
            return service.getMessages();
        }

        @POST

        public Response addMessage(Message msg) throws URISyntaxException
        {
            return Response.created(new URI("/messenger/webapi/messages"+msg.getId())).entity(service.addMessage(msg)).build();

        }



package org.restfulapi.messenger.services;

import java.util.List;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.HashMap;
import java.util.Map;

public class MessageService {
    private HashMap<Integer, Message> messages = Database.getMessages();

    public MessageService()
    {

    }
    public ArrayList<Message> getMessages() {
        return new ArrayList<Message>(messages.values());
    }
;
    public Message getMessage(int id) {
        return messages.get(id);
    }

    public ArrayList<Message> getbyyear(int year)
    {
        ArrayList<Message> result =new ArrayList();
        Calendar cal=Calendar.getInstance();
        for(Message msg:messages.values())
        {
            cal.setTime(msg.getCreated());
            if(cal.get(Calendar.YEAR)==year)
                result.add(msg);
        }
        return result;
    }
    public List<Message> get(int start,int size)
    {
        ArrayList<Message> result =new ArrayList<Message>(messages.values());
        return  result.subList(start, start+size);

    }

    public Message addMessage(Message message) {
        message.setId(messages.size() + 1);
        messages.put(message.getId(), message);
        return message;
    }

}


package org.restfulapi.messenger.services;

import java.util.ArrayList;
import java.util.Date;

import javax.xml.bind.annotation.XmlRootElement;

@XmlRootElement
public class Message {
    private int id;
    private String msg;
    private Date created;
    private String author;
private ArrayList<Link> links=new ArrayList();

    public ArrayList<Link> getLinks() {
    return links;
}
public void setLinks(ArrayList<Link> links) {
    this.links = links;
}

    public Message()
    {

    }
    public Message(int id, String msg, String author) {

        this.id = id;
        this.msg = msg;
        this.author = author;
        this.created = new Date();
    }

    public int getId() {
        return id;
    }

    public void setId(int id) {
        this.id = id;
    }

    public String getMsg() {
        return msg;
    }

    public void setMsg(String msg) {
        this.msg = msg;
    }

    public Date getCreated() {
        return created;
    }

    public void setCreated(Date created) {
        this.created = created;
    }

    public String getAuthor() {
        return author;
    }

    public void setAuthor(String author) {
        this.author = author;
    }

    public void addLink(String uri, String rel) {
        Link l=new Link(uri,rel);
        links.add(l);   
    }

}


public class Database 
{

    private static  HashMap<Integer,Message> messages=new HashMap();

    public static HashMap<Integer,Message> getMessages()
    {
        return messages;
    }
}

0 个答案:

没有答案