0

I am developing a REST Web Service application but while using REST Client POSTMAN the Body tab is showing disabled.

I have created a class named EmployeeController which handles the URI call as is shown below:

package com.Employee;

@Path("/response")

public class EmployeeController {

    EmployeeService employeeService = new EmployeeService();

    @GET
    @Produces(MediaType.APPLICATION_JSON)
    public Response getAllEmployee(){
        List<Employee> emp = employeeService.getAllEmployee();
        return Response.status(Status.CREATED).entity(emp).build();
    }
    @POST
    @Consumes(MediaType.APPLICATION_JSON)
    @Produces(MediaType.APPLICATION_JSON)
    public Response addEmployee(Employee emp){
        Employee e = employeeService.addEmployee(emp);
        return Response.status(Status.CREATED).entity(emp).build();
    }
}

The method getAllEmployee() retrieves all the details of the employees and the method addEmployee() add the employee.

Following changes have been made in web.xml file:

<web-app id="WebApp_ID" version="2.4"
    xmlns="http://java.sun.com/xml/ns/j2ee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee 
http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd">
    <display-name>RestPathAnnotationExample</display-name>

    <servlet>
        <servlet-name>jersey-serlvet</servlet-name>
        <servlet-class>com.sun.jersey.spi.container.servlet.ServletContainer</servlet-class>
        <init-param>
            <param-name>com.sun.jersey.config.property.packages</param-name>
            <param-value>com.Employee</param-value>
        </init-param>
        <init-param>
            <param-name>com.sun.jersey.api.json.POJOMappingFeature</param-name>
            <param-value>true</param-value>
        </init-param>
        <load-on-startup>1</load-on-startup>
    </servlet>

    <servlet-mapping>
        <servlet-name>jersey-serlvet</servlet-name>
        <url-pattern>/rest/*</url-pattern>
    </servlet-mapping>

</web-app>

In POM.xml have added two maven dependencies: jersey-server and jersey-json.

Is there any issue in the code due to which the Body part in POSTMAN is disabled?

Ranju Pillai
  • 33
  • 1
  • 4
  • 7
  • 2
    GET request cannot have a body – litelite Aug 21 '17 at 19:06
  • Thanks.When I changed the HTTP method from GET to POST the body tab is enabled. – Ranju Pillai Aug 21 '17 at 19:09
  • Hi @RanjuPillai if the answer has solved your question please consider accepting it by clicking the check-mark. This indicates to the wider community that you've found a solution and gives some reputation to both the answerer and yourself. There is no obligation to do this – Ray Oct 09 '17 at 10:47
  • @litelite This is incorrect. GET request can have a body payload, even though it is not recommended. https://stackoverflow.com/a/983458/9113772 Newer postman versions allows sending Body in GET request – Yoav Sheetrit Aug 29 '22 at 11:08

1 Answers1

0

In addition to litelite's comment about GET request not having a body, I suggest you understand more how APIs work. https://softwareengineering.stackexchange.com/questions/203844/what-does-it-mean-to-expose-something

Your REST code only exposes URLs and whatever data is being 'returned' by your CRUD methods. Any other changes to your code, like the pom.xml, is invisible and irrelevant to the consumers; think of the security breach if it were otherwise. And also, POSTMAN is a testing tool. It displays, sends and intercepts data but doesn't modify its behaviour according to the data.

Ray
  • 3,864
  • 7
  • 24
  • 36