请求实体不能为空

时间:2017-03-20 19:18:18

标签: web-services java-ee jersey jax-rs http-status-code-400

  

我是Jax-RS的新手。我正在尝试使用简单的GET方法   球衣。我正在获取Collection资源的正确输出,   但我得到" ERROR 400错误请求" 例如资源。一世   我陷入了困境。一切似乎都是正确的,但我是   遗漏了一些我无法弄清楚的东西。我已经   尝试了最近10天。任何帮助都会很明显。请找到   我的代码如下。我正在使用POSTMAN休息客户端投掷   "意想不到的''"作为响应,ARC正在抛出"请求实体   不能为空。"两者都是错误400错误请求。

这是我的web.xml:

package com.manish.jax_rs.Messanger.resources;

import java.util.List;

import javax.websocket.server.PathParam;
import javax.ws.rs.Consumes;
import javax.ws.rs.GET;
import javax.ws.rs.POST;
import javax.ws.rs.Path;
import javax.ws.rs.Produces;
import javax.ws.rs.core.MediaType;

import com.manish.jax_rs.Messanger.model.Message;
import com.manish.jax_rs.Messanger.service.MessageService;

@Path("/messages")
public class MessageResource {

    MessageService messageService = new MessageService();
    @GET
    @Produces(MediaType.APPLICATION_JSON)
    public List<Message> getMessages(){

        return messageService.getAllMessages();
    }

    @GET
    @Path("/{id}")
    @Produces(MediaType.APPLICATION_JSON)
    public Message getMessage(@PathParam("id") int id){


        return messageService.getMessage(id);
    }

}

这是我的MessageResource.java:

package com.manish.jax_rs.Messanger.Dao;

import java.util.HashMap;
import java.util.Map;

import com.manish.jax_rs.Messanger.model.Message;


public class DaoClass {


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


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

这是DaoClass.java:

package com.manish.jax_rs.Messanger.service;

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

    import com.manish.jax_rs.Messanger.Dao.DaoClass;
    import com.manish.jax_rs.Messanger.model.Message;

    public class MessageService {

        private Map<Integer,Message> messages= DaoClass.getMessages();

        Message m1= new Message(1,"Hello John","John");
        Message m2=  new Message(2,"Hello Mathews","Mathews");
        Message m3= new Message(3,"Hello Albert","Albert");

        public MessageService(){
            messages.put(1,m1);
            messages.put(2,m2);
            messages.put(3, m3);
        }
        public List<Message> getAllMessages(){
            return new ArrayList<Message>(messages.values());
        }
        public Message getMessage(int id){
            return messages.get(id);
        }
    }

这是MessageService.java:

package com.manish.jax_rs.Messanger.model;

import java.util.Date;

import javax.xml.bind.annotation.XmlRootElement;

@XmlRootElement
public class Message {

    private int id;
    private String message;
    //private Date created;
    private String author;

    public Message() {

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

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

    public int getId() {
        return id;
    }
    public void setId(int id) {
        this.id = id;
    }
    public String getMessage() {
        return message;
    }
    public void setMessage(String message) {
        this.message = message;
    }

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



[List of jars][1]




[correct output for collection resource "messages" in Postman rest client][2]


[Incorrect output for instance resource /messages/1][3]enter code here


  [1]: https://i.stack.imgur.com/sWfMl.jpg
  [2]: https://i.stack.imgur.com/y5Nnf.jpg
  [3]: https://i.stack.imgur.com/8CwId.jpg

这是Message.java:

{{1}}

2 个答案:

答案 0 :(得分:4)

请检查您的PathParam导入。 而不是&#34; javax.websocket.server.PathParam;&#34;使用&#34; import javax.ws.rs.PathParam;&#34;。 这应该可以正常工作。

答案 1 :(得分:-1)

您已导入import javax.websocket.server.PathParam;应该是import javax.ws.rs.PathParam;