The request entity cannot be empty
Asked Answered
U

2

6

I am new to Jax-RS. I am trying to implement a simple GET method using jersey. I am getting the correct output for the Collection resource, but I am getting "ERROR 400 Bad Request" for instance resource . I am stuck at this point. Everything seems to be correct,but I am missing something which I am not able to figure out. I have been trying it for last 10 days. Any help will be appreciable. Please find my code below.I am using POSTMAN rest client which is throwing "Unexpected 'T'" as response and ARC is throwing "The request entity cannot be empty." and both are Error 400 bad request.

This is my web.xml:

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xmlns="http://xmlns.jcp.org/xml/ns/javaee"
    xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd"
    id="WebApp_ID" version="3.1">
    <display-name>MessangerWithoutMaven</display-name>
    <welcome-file-list>

        <welcome-file>index.jsp</welcome-file>

    </welcome-file-list>

    <servlet>
        <servlet-name>Jersey Web Application</servlet-name>
        <servlet-class>org.glassfish.jersey.servlet.ServletContainer</servlet-class>
        <init-param>
            <param-name>jersey.config.server.provider.packages</param-name>
            <param-value>com.manish.jax_rs.Messanger</param-value>
        </init-param>
        <load-on-startup>1</load-on-startup>
    </servlet>
    <servlet-mapping>
        <servlet-name>Jersey Web Application</servlet-name>
        <url-pattern>/webapi/*</url-pattern>
    </servlet-mapping>
</web-app>

This is my MessageResource.java :

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);
    }

}

This is DaoClass.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;
    }
}

This is MessageService.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);
        }
    }

This is Message.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.sstatic.net/sWfMl.jpg
  [2]: https://i.sstatic.net/y5Nnf.jpg
  [3]: https://i.sstatic.net/8CwId.jpg
Underwing answered 20/3, 2017 at 19:18 Comment(0)
R
16

Please check your import of PathParam. Instead of "javax.websocket.server.PathParam;" use "import javax.ws.rs.PathParam;". This should work fine.

Robichaux answered 21/4, 2017 at 11:48 Comment(3)
Can you elaborate on why this change should solve the issue?Automation
answers should be detailed please update your answerDelaminate
I really could not find what was the issue. Just imported the same project to STS and it worked like charm .Underwing
I
1

You have imported import javax.websocket.server.PathParam; which should be import javax.ws.rs.PathParam;

Irresistible answered 13/2, 2020 at 9:15 Comment(1)
This is just a duplicate of another answerFurmenty

© 2022 - 2024 — McMap. All rights reserved.