繁体   English   中英

hibernate-orm-panache-resteasy:{“错误”:“加载需要加载ID”,“代码”:500}运行本机版本

[英]hibernate-orm-panache-resteasy: {“error”:“id to load is required for loading”,“code”:500} running a native build

在尝试使用带有panache的休眠ORM和小型休息服务测试quarkus之后,在构建本机可执行文件并将其在docker容器中运行后,我得到了一个错误。 关键是,它可以完美地在开发人员模式下工作,但在构建本机之后无法正常工作。

我正在使用的代码是从水果示例复制而来的。 我在Docker容器中使用了postgres db。 通过import.sql使用一些数据初始化数据库,如果我在开发模式下运行它,我可以在http:// localhost:8080 / person上发出请求,并获取所有人员的列表,如果我在http上发出请求:// localhost:8080 / person / 2 /我得到ID为2的人。在完成本机构建之后,我在docker容器中运行了该服务。 使用相同的请求,我会得到所有人员的列表,但是如果我想获得具有例如ID 2的人员,则会得到响应代码500,错误为:{“” error“:”要加载的ID,需要加载“ ,“ code”:500}数据库中存在ID为2的人。

import java.util.List;
import javax.enterprise.context.ApplicationScoped;
import javax.json.Json;
import javax.transaction.Transactional;
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.Produces;
import javax.ws.rs.WebApplicationException;
import javax.ws.rs.core.Response;
import javax.ws.rs.ext.ExceptionMapper;
import javax.ws.rs.ext.Provider;

import org.jboss.resteasy.annotations.jaxrs.PathParam;

import io.quarkus.panache.common.Sort;

/**
 * PersonResource
 */
@Path("person")
@ApplicationScoped
@Produces("application/json")
@Consumes("application/json")
public class PersonResource {

    @GET
    public List<Person> get() {
        return Person.listAll(Sort.by("lastName"));
    }

    @GET
    @Path("{id}")
    public Person getSingle(@PathParam Long id) {
        Person entity = Person.findById(id);
        if (entity == null) {
            throw new WebApplicationException("Person with id of " + id + " does not exist.", 404);
        }
        return entity;
    }

    @POST
    @Transactional
    public Response create(Person person) {
        if (person.id != null) {
            throw new WebApplicationException("Id was invalidly set on request.", 422);
        }
        person.persist();
        return Response.ok(person).status(201).build();
    }

    @PUT
    @Path("{id}")
    @Transactional
    public Person update(@PathParam Long id, Person person) {
        if (person.firstName == null || person.lastName == null) {
            throw new WebApplicationException("First Name or Last Name was not set on request.", 422);
        }

        Person entity = Person.findById(id);

        if (entity == null) {
            throw new WebApplicationException("Person with id of " + id + " does not exist.", 404);
        }

        entity.firstName = person.firstName;
        entity.lastName = person.lastName;

        return entity;
    }

    @DELETE
    @Path("{id}")
    @Transactional
    public Response delete(@PathParam Long id) {
        Person entity = Person.findById(id);
        if (entity == null) {
            throw new WebApplicationException("Person with id of " + id + " does not exist.", 404);
        }
        entity.delete();
        return Response.status(204).build();
    }


    @Provider
    public static class ErrorMapper implements ExceptionMapper<Exception> {

        @Override
        public Response toResponse(Exception exception) {
            int code = 500;
            if (exception instanceof WebApplicationException) {
                code = ((WebApplicationException) exception).getResponse().getStatus();
            }
            return Response.status(code)
                    .entity(Json.createObjectBuilder().add("error", exception.getMessage()).add("code", code).build())
                    .build();
        }

    }

}

这里的请求和响应:

$ curl -v -X GET 'http://localhost:8080/person'
Note: Unnecessary use of -X or --request, GET is already inferred.
* Uses proxy env variable no_proxy == 'localhost,127.0.0.1,*sulzer.de'
*   Trying ::1:8080...
* TCP_NODELAY set
* Connected to localhost (::1) port 8080 (#0)
> GET /person HTTP/1.1
> Host: localhost:8080
> User-Agent: curl/7.65.3
> Accept: */*
> 
* Mark bundle as not supporting multiuse
< HTTP/1.1 200 OK
< Connection: keep-alive
< Content-Type: application/json
< Content-Length: 154
< Date: Tue, 13 Aug 2019 09:15:14 GMT
< 
* Connection #0 to host localhost left intact
[{"id":2,"firstName":"Muster","lastName":"Maxmann"},{"id":1,"firstName":"Max","lastName":"Mustermann"},{"id":3,"firstName":"Mann","lastName":"Mustermax"}]


$ curl -v -X GET 'http://localhost:8080/person/2'
Note: Unnecessary use of -X or --request, GET is already inferred.
* Uses proxy env variable no_proxy == 'localhost,127.0.0.1,*sulzer.de'
*   Trying ::1:8080...
* TCP_NODELAY set
* Connected to localhost (::1) port 8080 (#0)
> GET /person/2 HTTP/1.1
> Host: localhost:8080
> User-Agent: curl/7.65.3
> Accept: */*
> 
* Mark bundle as not supporting multiuse
< HTTP/1.1 500 Internal Server Error
< Connection: keep-alive
< Content-Type: application/json
< Content-Length: 57
< Date: Tue, 13 Aug 2019 09:15:24 GMT
< 
* Connection #0 to host localhost left intact
{"error":"id to load is required for loading","code":500}

您可以尝试使用javax.ws.rs.PathParam代替org.jboss.resteasy.annotations.jaxrs.PathParam吗?

没有考虑到org.jboss.resteasy.annotations.jaxrs.PathParam因此最终将null传递给您的方法,并且使用Panache的Hibernate ORM抱怨在尝试加载实体时未提供ID。

我将在这里看看是否可以改善可用性,因为最近我遇到了同样的问题。

RESTEasy文档中所述,如果您使用的是新注释,则需要使编译器生成参数名称。 将此添加到您的pom.xml

    <properties>
        <maven.compiler.parameters>true</maven.compiler.parameters>
    </properties>

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM