Error 500 in java webservices when testing in Postman

Asked

Viewed 269 times

0

I am a good time trying to solve this problem,I configured my environment about 3 times but when it comes to testing Postman always gives the error 500,I believe that my problem is not in the entity or in the DAO,however when doing web project in netbeans creating the server, persistence class ,classes as entity,using @Xmlrootelement annotation yet the problem continues, Ws configuration

package ws;

import java.util.Set;
import javax.ws.rs.core.Application;

/**
 *
 * @author felipe
 */
@javax.ws.rs.ApplicationPath("api")
public class ApplicationConfig extends Application {

    @Override
    public Set<Class<?>> getClasses() {
        Set<Class<?>> resources = new java.util.HashSet<>();
        addRestResourceClasses(resources);
        return resources;
    }

    /**
     * Do not modify addRestResourceClasses() method.
     * It is automatically populated with
     * all resources defined in the project.
     * If required, comment out calling this method in getClasses().
     */
    private void addRestResourceClasses(Set<Class<?>> resources) {
        resources.add(ws.ClienteWS.class);
    }

}

Class Ws

    package ws;

import entidades.Cliente;
import java.util.List;
import javax.ws.rs.core.Context;
import javax.ws.rs.core.UriInfo;
import javax.ws.rs.Consumes;
import javax.ws.rs.Produces;
import javax.ws.rs.GET;
import javax.ws.rs.Path;
import javax.ws.rs.PUT;
import javax.ws.rs.core.MediaType;
import rn.ClienteRN;

/**
 * REST Web Service
 *
 * @author felipe
 */
@Path("cliente")
public class ClienteWS {


    private ClienteRN clienteRn; 
    @Context
    private UriInfo context;
    public ClienteWS() {
       clienteRn=new ClienteRN();
    }


   @GET
    @Produces(MediaType.APPLICATION_JSON)
    public List<Cliente> getClientes() {
        return clienteRn.imprimir();
    }


}

Way of the Postman:http://localhost:8080/Sistema_sales/api/client

Related class :

/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */

    package util;

    import javax.persistence.EntityManager;
    import javax.persistence.EntityManagerFactory;
    import javax.persistence.Persistence;

    /**
     *
     * @author felipe
     */
    public class Conexao {

         private static EntityManagerFactory emf = Persistence.createEntityManagerFactory("Sistema_VendasPU");

        public EntityManager getEntidade() {
            return emf.createEntityManager();

    //
        }
    }

Customer RN Type of a DAO

    package rn;

import entidades.Cliente;
import java.util.List;
import javax.persistence.EntityManager;
import javax.persistence.Query;
import util.Conexao;

public class ClienteRN {

    public Cliente inserir(Cliente cliente) {
        Conexao con = new Conexao();
        EntityManager em = con.getEntidade();
        em.getTransaction().begin();
        em.persist(cliente);
        em.getTransaction().commit();
        em.close();
        return cliente;

    }

Entidade Cliente :

/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package entidades;

import java.io.Serializable;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.xml.bind.annotation.XmlRootElement;

/**
 *
 * @author felipe
 */
@XmlRootElement
@Entity
public class Cliente implements Serializable {

    private static final long serialVersionUID = 1L;
    @Id
    @GeneratedValue(strategy = GenerationType.AUTO)
    private Long id;
    private String nome;
    private String telefone;

    public Cliente() {
    }

    public Cliente(String nome, String telefone) {
        this.nome = nome;
        this.telefone = telefone;
    }


    public String getNome() {
        return nome;
    }

    public void setNome(String nome) {
        this.nome = nome;
    }

    public String getTelefone() {
        return telefone;
    }

    public void setTelefone(String telefone) {
        this.telefone = telefone;
    }


    public Long getId() {
        return id;
    }

    public void setId(Long id) {
        this.id = id;
    }

    @Override
    public int hashCode() {
        int hash = 0;
        hash += (id != null ? id.hashCode() : 0);
        return hash;
    }

    @Override
    public boolean equals(Object object) {
        // TODO: Warning - this method won't work in the case the id fields are not set
        if (!(object instanceof Cliente)) {
            return false;
        }
        Cliente other = (Cliente) object;
        if ((this.id == null && other.id != null) || (this.id != null && !this.id.equals(other.id))) {
            return false;
        }
        return true;
    }

    @Override
    public String toString() {
        return "entidades.Cliente[ id=" + id + " ]";
    }

}

Error

  HTTP Status 500 – Internal Server Error
    Type Exception Report

    Message org.glassfish.jersey.server.ContainerException: java.lang.NoClassDefFoundError: Could not initialize class util.Conexao

    Description The server encountered an unexpected condition that prevented it from fulfilling the request.

    Exception

    javax.servlet.ServletException: org.glassfish.jersey.server.ContainerException: java.lang.NoClassDefFoundError: Could not initialize class util.Conexao
        org.glassfish.jersey.servlet.WebComponent.service(WebComponent.java:392)
        org.glassfish.jersey.servlet.ServletContainer.service(ServletContainer.java:382)
        org.glassfish.jersey.servlet.ServletContainer.service(ServletContainer.java:345)
        org.glassfish.jersey.servlet.ServletContainer.service(ServletContainer.java:220)
        org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:52)
    Root Cause

    org.glassfish.jersey.server.ContainerException: java.lang.NoClassDefFoundError: Could not initialize class util.Conexao
        org.glassfish.jersey.servlet.internal.ResponseWriter.rethrow(ResponseWriter.java:249)
        org.glassfish.jersey.servlet.internal.ResponseWriter.failure(ResponseWriter.java:231)
        org.glassfish.jersey.server.ServerRuntime$Responder.process(ServerRuntime.java:436)
        org.glassfish.jersey.server.ServerRuntime$1.run(ServerRuntime.java:265)
        org.glassfish.jersey.internal.Errors$1.call(Errors.java:271)
        org.glassfish.jersey.internal.Errors$1.call(Errors.java:267)
        org.glassfish.jersey.internal.Errors.process(Errors.java:315)
        org.glassfish.jersey.internal.Errors.process(Errors.java:297)
        org.glassfish.jersey.internal.Errors.process(Errors.java:267)
        org.glassfish.jersey.process.internal.RequestScope.runInScope(RequestScope.java:318)
        org.glassfish.jersey.server.ServerRuntime.process(ServerRuntime.java:236)
        org.glassfish.jersey.server.ApplicationHandler.handle(ApplicationHandler.java:1010)
        org.glassfish.jersey.servlet.WebComponent.service(WebComponent.java:373)
        org.glassfish.jersey.servlet.ServletContainer.service(ServletContainer.java:382)
        org.glassfish.jersey.servlet.ServletContainer.service(ServletContainer.java:345)
        org.glassfish.jersey.servlet.ServletContainer.service(ServletContainer.java:220)
        org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:52)
    Root Cause

    java.lang.NoClassDefFoundError: Could not initialize class util.Conexao
        rn.ClienteRN.imprimir(ClienteRN.java:27)
        ws.ClienteWS.getClientes(ClienteWS.java:40)
        sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
        sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
        sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
        java.lang.reflect.Method.invoke(Method.java:498)
        org.glassfish.jersey.server.model.internal.ResourceMethodInvocationHandlerFactory$1.invoke(ResourceMethodInvocationHandlerFactory.java:81)
        org.glassfish.jersey.server.model.internal.AbstractJavaResourceMethodDispatcher$1.run(AbstractJavaResourceMethodDispatcher.java:151)
        org.glassfish.jersey.server.model.internal.AbstractJavaResourceMethodDispatcher.invoke(AbstractJavaResourceMethodDispatcher.java:171)
        org.glassfish.jersey.server.model.internal.JavaResourceMethodDispatcherProvider$TypeOutInvoker.doDispatch(JavaResourceMethodDispatcherProvider.java:195)
        org.glassfish.jersey.server.model.internal.AbstractJavaResourceMethodDispatcher.dispatch(AbstractJavaResourceMethodDispatcher.java:104)
        org.glassfish.jersey.server.model.ResourceMethodInvoker.invoke(ResourceMethodInvoker.java:402)
        org.glassfish.jersey.server.model.ResourceMethodInvoker.apply(ResourceMethodInvoker.java:349)
        org.glassfish.jersey.server.model.ResourceMethodInvoker.apply(ResourceMethodInvoker.java:106)
        org.glassfish.jersey.server.ServerRuntime$1.run(ServerRuntime.java:259)
        org.glassfish.jersey.internal.Errors$1.call(Errors.java:271)
        org.glassfish.jersey.internal.Errors$1.call(Errors.java:267)
        org.glassfish.jersey.internal.Errors.process(Errors.java:315)
        org.glassfish.jersey.internal.Errors.process(Errors.java:297)
        org.glassfish.jersey.internal.Errors.process(Errors.java:267)
        org.glassfish.jersey.process.internal.RequestScope.runInScope(RequestScope.java:318)
        org.glassfish.jersey.server.ServerRuntime.process(ServerRuntime.java:236)
  • The error is not related to the code posted, apparently. You did not define a class called Connected, and referenced it...

  • The class util.Conexao is there? Is it imported? It seems that the error is elsewhere.

  • first obliged to help,I added the classes : connection and the Clientern(type of a dao) I checked if I did not forget any import and therefore I saw no,that value I added in the Entitymanagerfactory was a value of the persistence class created,maybe the error is on the Tomcat server,but I believe it is unlikely because it is giving access or is presenting the error.

1 answer

0


I switched the use database to postgresql and found a way to list which was the problem

Right version of the list method

 public List<Cliente> imprimir() {
   Conexao con = new Conexao();
    EntityManager em = con.getEntidade();
    em.getTransaction().begin();
    String jpql="select c from Cliente c";
    Query query = em.createQuery(jpql);
    List<Cliente> listaClientes = query.getResultList();
    em.getTransaction().commit();
    return (listaClientes);

}

Wrong version

public List<Cliente> imprimir() {
        Conexao con = new Conexao();
        EntityManager em = con.getEntidade();
        em.getTransaction().begin();
        Query query = em.createQuery("from Cliente");
        List<Cliente> listaClientes = query.getResultList();
        System.out.println("Categorias:");
        em.getTransaction().commit();
        return (listaClientes);

    }

the strange thing is that both worked without webservices but in web services only works using jpql

  • 1

    Hello, why can’t you accept? There is some error?

  • ola "you can accept your answer in 2 hours"

  • You can accept it now

Browser other questions tagged

You are not signed in. Login or sign up in order to post.