'POST Request give me empty JSON even if it shouldn't be

I'm creating a REST service. I've already completed the GET functions and I'm currently working on POST. Here, I found a strange problem.

I'm using GSON Library with Java on Eclipse, running con Liberty Server. I've included gson-2.6.2.jar in the library, as well as jersey-bundle-1.19.1.jar.

Basically when I send a POST request (either with Postman or with Angular), I receive a empty JSON. But actually, my json isn't empty because I can console.log() it on Angular and I can write it in Postman request's body.

Also, if I choose String as parameter type, I get an empty string, while if I choose every other type, like the POJO class Libro, I get a 415 Unsupported Media Type.

I really can't understand what's going on.

Here some code:

LibraryApp.java

package rest;

import javax.ws.rs.ApplicationPath;
import javax.ws.rs.core.Application;

@ApplicationPath("/rest")
public class LibraryApp extends Application{

}

ServletRest.java

package rest;

import java.sql.SQLException;
import java.util.List;

import javax.naming.NamingException;
import javax.ws.rs.Consumes;
import javax.ws.rs.GET;
import javax.ws.rs.POST;
import javax.ws.rs.Path;
import javax.ws.rs.PathParam;
import javax.ws.rs.Produces;
import javax.ws.rs.core.MediaType;
import javax.ws.rs.core.Response;

import com.google.gson.*;

import jsonClasses.Libro;
import jsonClasses.LibroDB;

@Path("/libreria")
public class ServletRest{
    
    @GET
    @Produces(MediaType.APPLICATION_JSON)
    public Response getListaLibri() {

        List<Libro> bookList = null;
        
        String r = "";

        try {
            bookList = LibroDB.selectAll(0);
            if (bookList.size() > 0) {
                r = new GsonBuilder().setPrettyPrinting().create().toJson(bookList);
            } else {
                r = "Nessun libro in lista";
            }
        } catch (NamingException ex) {
            r = "Si è verificato un errore Naming";
        } catch (SQLException ex) {
            r = "Si è verificato un errore SQL";
        }
        
        return Response.ok(r).build();

    }
    
    @GET
    @Path("{id}")
    @Produces(MediaType.APPLICATION_JSON)
    public Response getLibro(@PathParam("id") String id) {
        
        Libro book = null;
        Long bookId = Long.parseLong(id);
        
        String r = "";
 
        try {
            book = LibroDB.select(bookId);
            if (book != null) {
                r = new GsonBuilder().setPrettyPrinting().create().toJson(book);
            } else {
                r = "Nessun libro trovato";
            }
        } catch (NamingException ex) {
            r = "Si è verificato un errore Naming";
        } catch (SQLException ex) {
            r = "Si è verificato un errore SQL";
        }
        
        return Response.ok(r).build();

    }
    
    @POST
    @Consumes(MediaType.APPLICATION_JSON)
    @Produces(MediaType.APPLICATION_JSON)
    public String postLibro(String json) {
        return json;
    }
    
}

Libro.java (POJO)

package jsonClasses;

import model.CategoriaLibro;
import model.StatoLibro;

public class Libro {
    
    private Long id;
    private String titolo;
    private String autore;
    private Integer anno;
    private CategoriaLibro categoriaLibro;
    private StatoLibro statoLibro;
    
    public Long getId() {
        return id;
    }
    public void setId(Long id) {
        this.id = id;
    }
    
    public String getTitolo() {
        return titolo;
    }
    public void setTitolo(String titolo) {
        this.titolo = titolo;
    }
    
    public String getAutore() {
        return autore;
    }
    public void setAutore(String autore) {
        this.autore = autore;
    }
    
    public Integer getAnno() {
        return anno;
    }
    public void setAnno(Integer anno) {
        this.anno = anno;
    }
    
    public CategoriaLibro getCategoriaLibro() {
        return categoriaLibro;
    }
    public void setCategoriaLibro(CategoriaLibro categoriaLibro) {
        this.categoriaLibro = categoriaLibro;
    }
    
    public StatoLibro getStatoLibro() {
        return statoLibro;
    }
    public void setStatoLibro(StatoLibro statoLibro) {
        this.statoLibro = statoLibro;
    }

}

web.xml

<?xml version="1.0" encoding="UTF-8"?>
<web-app id="WebApp_ID" version="3.0"
    xmlns="http://java.sun.com/xml/ns/javaee"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://java.sun.com/xml/ns/javaee 
    http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd">

    <description>micraWebApp</description>
    <resource-ref>
        <description>DB Connection</description>
        <res-ref-name>jdbc/micraeclipse</res-ref-name>
        <res-type>javax.sql.DataSource</res-type>
        <res-auth>Container</res-auth>
    </resource-ref>

    <servlet>
        <servlet-name>LibraryApp</servlet-name>
        <servlet-class>com.ibm.websphere.jaxrs.server.IBMRestServlet</servlet-class>
        <init-param>
            <param-name>javax.ws.rs.Application</param-name>
            <param-value>rest.LibraryApp</param-value>
        </init-param>
        <load-on-startup>1</load-on-startup>
    </servlet>

    <servlet-mapping>
        <servlet-name>LibraryApp</servlet-name>
        <url-pattern>/rest</url-pattern>
    </servlet-mapping>

</web-app>


Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source