API Rest Java EE erros: MUST be public scoped otherwise the method is ignored e No Persistence for EntityManager

Questions:

I am making an api for providers control but I have 2 errors when I try accessing the api methods:

Warnings

WARNING: The following warnings have been detected with resource and/or provider classes:

WARNING: A sub-resource method, java.util.List br.com.sabrina.api.controller.ProviderController.list(), MUST be public scoped otherwise the method is ignored

WARNING: A sub-resource method, br.com.sabrina.api.model.ProviderModel br.com.sabrina.api.controller.ProviderController.search(int), MUST be public scoped otherwise the method is ignored

Errors

Jul 27, 2018 1:29:06 PM org.apache.catalina.core.StandardWrapperValve invoke
SEVERE: Servlet.service() for servlet [Jersey Web Application] in context with path [/ApiRest] threw exception
javax.persistence.PersistenceException: No Persistence provider for EntityManager named provider

About the warnings I did not got what could be wrong, and about the
persistence.xml I already have it created and configured correctly,
I do not know what the problem might be.

Project structure
inserir a descrição da imagem aqui

Classes

ProviderController.java

package br.com.sabrina.api.controller;

import java.util.List;

import javax.persistence.EntityManager;
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.WebApplicationException;
import javax.ws.rs.core.Response;

import br.com.sabrina.api.model.ProviderModel;

@Path("/provider")
public class ProviderController {
    private JpaEntityManager JPAEM = new JpaEntityManager();
    private EntityManager objEM = JPAEM.getEntityManager();

    @GET
    @Path("/list")
    @Produces("application/json")
    List list() {
        try {
            String query = "SELECT c FROM ProviderModel c ";
            List providers = objEM.createQuery(query, ProviderModel.class).getResultList();
            objEM.close();
            return providers;
        } catch(Exception e) {
            throw new WebApplicationException(500); 
        }
    }

    @GET
    @Path("/search/{id}")
    @Produces("application/json")
    ProviderModel search(@PathParam("id") int id) {
        try {
            ProviderModel provider = objEM.find(ProviderModel.class, id);
            objEM.close();
            return provider;
        } catch(Exception e) {
            throw new WebApplicationException(500); 
        }
    }

    @POST
    @Path("/register")
    @Consumes("application/json")
    public Response register(ProviderModel objProvider) {
        try {
            objEM.getTransaction().begin();
            objEM.persist(objProvider);
            objEM.getTransaction().commit();
            objEM.close();
            return Response.status(200).entity("Cadastro realizado!").build();
        } catch(Exception e) {
            throw new WebApplicationException(500); 
        }
    }

    @PUT
    @Path("/update")
    @Consumes("application/json")
    public Response update(ProviderModel objProvider) {
        try {
            objEM.getTransaction().begin();
            objEM.persist(objProvider);
            objEM.getTransaction().commit();
            objEM.close();
            return Response.status(200).entity("Cadastro alterado!").build();
        } catch(Exception e) {
            throw new WebApplicationException(500); 
        }
    }

    @DELETE
    @Path("/delete/{id}")
    public Response delete(@PathParam("id") int id) {
        try {
            ProviderModel objProvider = objEM.find(ProviderModel.class, id);
            objEM.getTransaction().begin();
            objEM.remove(objProvider);
            objEM.getTransaction().commit();
            objEM.close();
            return Response.status(200).entity("Cadastro excluido!").build();
        } catch(Exception e) {
            throw new WebApplicationException(500); 
        }
    } 
}

ProviderModel.java

package br.com.sabrina.api.model;

import java.util.Date;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.Table;
import javax.persistence.Temporal;
import javax.persistence.TemporalType;

@Entity  //estrutura de mapeamento de objeto relacional
@Table(name="Provider")
public class ProviderModel {  
    //correspondente a tabela Provider do banco de dados
    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    int provider_id;

    @Column(name="name")
    String name;

    @Column(name="cpf_cnpj")
    String cpf_cnpj;

    @Column(name="rg")
    String rg;

    @Column(name="address")
    String adress;

    @Column(name="city")
    String city;

    @Column(name="email")
    String email;

    @Column(name="register_date")
    @Temporal(TemporalType.TIMESTAMP)
    Date register_date;

    public int getId() {
        return provider_id;
    }

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

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }
}

JpaEntityManager.java

package br.com.sabrina.api.controller;
import javax.persistence.EntityManager;
import javax.persistence.EntityManagerFactory;
import javax.persistence.Persistence;

public class JpaEntityManager {
    private EntityManagerFactory factory = Persistence.createEntityManagerFactory("provider");
    private EntityManager entityManager = factory.createEntityManager();

    public EntityManager getEntityManager() {
        return entityManager;
    }
}

persistence.xml


    
    
        
        org.hibernate.jpa.HibernatePersistenceProvider
        
        br.com.sabrina.api.model.ProviderModel
        
            
            
            
            
            
            
            
            
            
            
        
    

web.xml



  ApiRest
  
    index.html
    index.htm
    index.jsp
    default.html
    default.htm
    default.jsp
  
  
    Jersey Web Application
    com.sun.jersey.spi.container.servlet.ServletContainer
    1
  
  
    Jersey Web Application
    /service/*
  

Thanks

Answers:



Need Help? Chat with us