mercredi 1 juillet 2020

SEVERE: Servlet.service() for servlet [Jersey Web Application] in context with path [/restapp] threw exception [java.util.NoSuchElementException]

This is my first REST API application , i am trying to build brand and products application, where all products in table belongs any brands in the another table. I am testing this application in postman. While getting the results by providing particular brand id it is throwing this error.

SEVERE: Servlet.service() for servlet [Jersey Web Application] in context with path [/restapp] threw exception [java.util.NoSuchElementException] with root cause
java.util.NoSuchElementException
    at org.glassfish.json.JsonParserImpl.next(JsonParserImpl.java:361)
    at org.eclipse.yasson.internal.JsonbRiParser.next(JsonbRiParser.java:140)
    at org.eclipse.yasson.internal.Unmarshaller.getRootEvent(Unmarshaller.java:66)
    at org.eclipse.yasson.internal.Unmarshaller.deserializeItem(Unmarshaller.java:56)
    at org.eclipse.yasson.internal.Unmarshaller.deserialize(Unmarshaller.java:50)
    at org.eclipse.yasson.internal.JsonBinding.deserialize(JsonBinding.java:45)
    at org.eclipse.yasson.internal.JsonBinding.fromJson(JsonBinding.java:85)
    at org.glassfish.jersey.jsonb.internal.JsonBindingProvider.readFrom(JsonBindingProvider.java:99)
    at org.glassfish.jersey.message.internal.ReaderInterceptorExecutor$TerminalReaderInterceptor.invokeReadFrom(ReaderInterceptorExecutor.java:257)
    at org.glassfish.jersey.message.internal.ReaderInterceptorExecutor$TerminalReaderInterceptor.aroundReadFrom(ReaderInterceptorExecutor.java:236)
    at org.glassfish.jersey.message.internal.ReaderInterceptorExecutor.proceed(ReaderInterceptorExecutor.java:156)
    at org.glassfish.jersey.server.internal.MappableExceptionWrapperInterceptor.aroundReadFrom(MappableExceptionWrapperInterceptor.java:73)
    at org.glassfish.jersey.message.internal.ReaderInterceptorExecutor.proceed(ReaderInterceptorExecutor.java:156)
    at org.glassfish.jersey.message.internal.MessageBodyFactory.readFrom(MessageBodyFactory.java:1091)
    at org.glassfish.jersey.message.internal.InboundMessageContext.readEntity(InboundMessageContext.java:874)
    at org.glassfish.jersey.server.ContainerRequest.readEntity(ContainerRequest.java:271)
    at org.glassfish.jersey.server.internal.inject.EntityParamValueParamProvider$EntityValueSupplier.apply(EntityParamValueParamProvider.java:97)
    at org.glassfish.jersey.server.internal.inject.EntityParamValueParamProvider$EntityValueSupplier.apply(EntityParamValueParamProvider.java:80)
    at org.glassfish.jersey.server.spi.internal.ParamValueFactoryWithSource.apply(ParamValueFactoryWithSource.java:74)
    at org.glassfish.jersey.server.spi.internal.ParameterValueHelper.getParameterValues(ParameterValueHelper.java:92)
    at org.glassfish.jersey.server.model.internal.JavaResourceMethodDispatcherProvider$AbstractMethodParamInvoker.getParamValues(JavaResourceMethodDispatcherProvider.java:133)
    at org.glassfish.jersey.server.model.internal.JavaResourceMethodDispatcherProvider$TypeOutInvoker.doDispatch(JavaResourceMethodDispatcherProvider.java:243)
    at org.glassfish.jersey.server.model.internal.AbstractJavaResourceMethodDispatcher.dispatch(AbstractJavaResourceMethodDispatcher.java:103)
    at org.glassfish.jersey.server.model.ResourceMethodInvoker.invoke(ResourceMethodInvoker.java:493)
    at org.glassfish.jersey.server.model.ResourceMethodInvoker.apply(ResourceMethodInvoker.java:415)
    at org.glassfish.jersey.server.model.ResourceMethodInvoker.apply(ResourceMethodInvoker.java:104)
    at org.glassfish.jersey.server.ServerRuntime$1.run(ServerRuntime.java:277)
    at org.glassfish.jersey.internal.Errors$1.call(Errors.java:272)
    at org.glassfish.jersey.internal.Errors$1.call(Errors.java:268)
    at org.glassfish.jersey.internal.Errors.process(Errors.java:316)
    at org.glassfish.jersey.internal.Errors.process(Errors.java:298)
    at org.glassfish.jersey.internal.Errors.process(Errors.java:268)
    at org.glassfish.jersey.process.internal.RequestScope.runInScope(RequestScope.java:289)
    at org.glassfish.jersey.server.ServerRuntime.process(ServerRuntime.java:256)
    at org.glassfish.jersey.server.ApplicationHandler.handle(ApplicationHandler.java:703)
    at org.glassfish.jersey.servlet.WebComponent.serviceImpl(WebComponent.java:416)
    at org.glassfish.jersey.servlet.WebComponent.service(WebComponent.java:370)
    at org.glassfish.jersey.servlet.ServletContainer.service(ServletContainer.java:389)
    at org.glassfish.jersey.servlet.ServletContainer.service(ServletContainer.java:342)
    at org.glassfish.jersey.servlet.ServletContainer.service(ServletContainer.java:229)
    at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:231)
    at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:166)
    at org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:53)
    at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:193)
    at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:166)
    at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:202)
    at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:96)
    at org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:541)
    at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:139)
    at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:92)
    at org.apache.catalina.valves.AbstractAccessLogValve.invoke(AbstractAccessLogValve.java:690)
    at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:74)
    at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:343)
    at org.apache.coyote.http11.Http11Processor.service(Http11Processor.java:373)
    at org.apache.coyote.AbstractProcessorLight.process(AbstractProcessorLight.java:65)
    at org.apache.coyote.AbstractProtocol$ConnectionHandler.process(AbstractProtocol.java:868)
    at org.apache.tomcat.util.net.NioEndpoint$SocketProcessor.doRun(NioEndpoint.java:1590)
    at org.apache.tomcat.util.net.SocketProcessorBase.run(SocketProcessorBase.java:49)
    at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1149)
    at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:624)
    at org.apache.tomcat.util.threads.TaskThread$WrappingRunnable.run(TaskThread.java:61)
    at java.lang.Thread.run(Thread.java:748)

This is my Brands.java file

package shrikant.restapp.showroon.resoursces;

import java.net.URI;
import java.util.List;

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.core.Context;
import javax.ws.rs.core.MediaType;
import javax.ws.rs.core.Response;
import javax.ws.rs.core.UriInfo;

import shrikant.restapp.showroon.hibernate.entities.BrandEntity;
import shrikant.restapp.showroon.services.BrandsService;

@Path("/showroom/brands")
public class Brands {

    BrandsService service = new BrandsService();

    @GET
    @Produces(MediaType.APPLICATION_JSON)
    public List<BrandEntity> getBrands() {
        List<BrandEntity> list = service.getBrands();
        return list;
    }

    @GET
    @Produces(MediaType.APPLICATION_JSON)
    @Path("/{brandId}")
    public BrandEntity getBrand(int brandId) {
        return service.getBrand(brandId);
    }
    
    @POST
    @Consumes(MediaType.APPLICATION_JSON)
    @Produces(MediaType.APPLICATION_JSON)
    public Response postBrands(BrandEntity brand ,@Context UriInfo uri ) {
         URI location = uri.getAbsolutePath();
        service.addBrand(brand);
        return Response.created(location).entity(brand).build();
                //.status(Status.CREATED).entity(brand).build();
    }

    @PUT
    @Path("/{brandId}")
    @Consumes(MediaType.APPLICATION_JSON)
    public void putBrands(@PathParam("brandId") int brandId, BrandEntity updatedBrand) {
        updatedBrand.setBrandId(brandId);
        service.updateBrand(updatedBrand);
    }

    @DELETE
    @Path("/{brandId}")
    public void deleteBrands(@PathParam("brandId") int brandId) {
        service.deleteBrand(brandId);
    }

    public Products productDelegation() {
        return new Products();
    }
}
This is my BrandService.java file (Service layer)

package shrikant.restapp.showroon.services;

import java.util.List;

import shrikant.restapp.showroon.hibernate.DAO.BrandsDAO;
import shrikant.restapp.showroon.hibernate.entities.BrandEntity;

public class BrandsService {

    BrandsDAO DAO = new BrandsDAO();

    public List<BrandEntity> getBrands() {
        List<BrandEntity> list = DAO.getBrands();
        return list;
    }

    public void addBrand(BrandEntity brand) {
        DAO.addBrand(brand);
    }

    public void updateBrand(BrandEntity updatedBrand) {
        DAO.updateBrand(updatedBrand);
    }

    public void deleteBrand(int brandId) {
        DAO.deleteBrand(brandId);
    }

    public BrandEntity getBrand(int brandId) {
        return DAO.getBrand(brandId);
    }
}

this is my BrandsDAO.java file (DAO Layer)

package shrikant.restapp.showroon.hibernate.DAO;

import java.util.List;

import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.cfg.Configuration;

import shrikant.restapp.showroon.hibernate.entities.BrandEntity;

public class BrandsDAO {

    SessionFactory factory = new Configuration().configure("hibernate.cfg.xml").addAnnotatedClass(BrandEntity.class)
            .buildSessionFactory();

    public List<BrandEntity> getBrands() {
        Session session = factory.getCurrentSession();
        session.beginTransaction();
        List<BrandEntity> list = session.createQuery("from brands").getResultList();
        return list;
    }

    public BrandEntity getBrand(int brandId) {
        Session session = factory.getCurrentSession();
        session.beginTransaction();
        return session.get(BrandEntity.class, brandId);
    }

    public void addBrand(BrandEntity brand) {
        Session session = factory.getCurrentSession();
        session.beginTransaction();
        session.save(brand);
        session.getTransaction().commit();
    }

    public void updateBrand(BrandEntity updatedBrand) {
        Session session = factory.getCurrentSession();
        session.beginTransaction();
        int id = updatedBrand.getBrandId();
        BrandEntity brand = session.get(BrandEntity.class, id);
        // brand = updatedBrand;
        brand.setBrandName(updatedBrand.getBrandName());
        session.getTransaction().commit();
    }

    public void deleteBrand(int brandId) {
        Session session = factory.getCurrentSession();
        session.beginTransaction();
        BrandEntity brand = session.get(BrandEntity.class, brandId);
        session.delete(brand);
        session.getTransaction().commit();
    }

}

this is my entity class

package shrikant.restapp.showroon.hibernate.entities;

import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.Table;


@Entity(name="brands")
@Table(name="brands")
public class BrandEntity {
    
    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    @Column(name="brand_id")
    int brandId;
    
    @Column(name="brand_name")
    String brandName;

    public BrandEntity() {
    }

    public BrandEntity(int brandId, String brandName) {
        super();
        this.brandId = brandId;
        this.brandName = brandName;
    }

    public int getBrandId() {
        return brandId;
    }

    public void setBrandId(int brandId) {
        this.brandId = brandId;
    }

    public String getBrandName() {
        return brandName;
    }

    public void setBrandName(String brandName) {
        this.brandName = brandName;
    }

}

this is my pom.xml file

<project xmlns="http://maven.apache.org/POM/4.0.0"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">

    <modelVersion>4.0.0</modelVersion>

    <groupId>shrikant</groupId>
    <artifactId>restapp</artifactId>
    <packaging>war</packaging>
    <version>0.0.1-SNAPSHOT</version>
    <name>restapp</name>

    <build>
        <finalName>restapp</finalName>
        <plugins>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-compiler-plugin</artifactId>
                <version>2.5.1</version>
                <inherited>true</inherited>
                <configuration>
                    <source>1.8</source>
                    <target>1.8</target>
                </configuration>
            </plugin>
        </plugins>
    </build>

    <dependencyManagement>
        <dependencies>
            <dependency>
                <groupId>org.glassfish.jersey</groupId>
                <artifactId>jersey-bom</artifactId>
                <version>${jersey.version}</version>
                <type>pom</type>
                <scope>import</scope>
            </dependency>
        </dependencies>
    </dependencyManagement>

    <dependencies>
        <dependency>
            <groupId>org.glassfish.jersey.containers</groupId>
            <artifactId>jersey-container-servlet-core</artifactId>
            <!-- use the following artifactId if you don't need servlet 2.x compatibility -->
            <!-- artifactId>jersey-container-servlet</artifactId -->
        </dependency>
        <dependency>
            <groupId>org.glassfish.jersey.inject</groupId>
            <artifactId>jersey-hk2</artifactId>
        </dependency>

        <dependency>
            <groupId>org.glassfish.jersey.media</groupId>
            <artifactId>jersey-media-json-binding</artifactId>
        </dependency>


        <!-- https://mvnrepository.com/artifact/javax.servlet/javax.servlet-api -->
        <dependency>
            <groupId>javax.servlet</groupId>
            <artifactId>javax.servlet-api</artifactId>
            <version>4.0.1</version>
            <scope>provided</scope>
        </dependency>

        <!-- https://mvnrepository.com/artifact/org.hibernate/hibernate-core -->
        <dependency>
            <groupId>org.hibernate</groupId>
            <artifactId>hibernate-core</artifactId>
            <version>5.3.7.Final</version>
        </dependency>
        <!-- https://mvnrepository.com/artifact/mysql/mysql-connector-java -->
        <dependency>
            <groupId>mysql</groupId>
            <artifactId>mysql-connector-java</artifactId>
            <version>8.0.13</version>
        </dependency>
    </dependencies>
    <properties>
        <jersey.version>2.26</jersey.version>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
    </properties>
</project>

I don't understand whats the problem anyone help.




Aucun commentaire:

Enregistrer un commentaire