Dropwizard/Jersey 在 GET 请求中给出 "Unable to process JSON" 消息

Dropwizard/Jersey is giving "Unable to process JSON" message on GET request

我正在尝试使用 Dropwizard 制作我的第一个简单项目。我有一个 MySQL 数据库,我的想法是从那里获取数据(公司)并将其表示为 JSON。我已按照 Getting started page by Dropwizard and this 教程使用 Hibernate 连接到数据库。

想法是 URL“/companies”作为 JSON 为所有公司服务,并且工作正常。

URL “/companies/{id}” 应该提供给定 id 的单个公司,但每个请求都提供代码 400 和消息 "Unable to process JSON"。响应中的详细信息字段显示

"No serializer found for class jersey.repackaged.com.google.common.base.Present and no properties discovered to create BeanSerializer (to avoid exception, disable SerializationFeature.FAIL_ON_EMPTY_BEANS) )"

如果我提供数据库中不存在的公司 ID,上述消息中的 class 将更改为

jersey.repackaged.com.google.common.base.Absent

公司class在这里:

public class Company {
    @ApiModelProperty(required = true)
    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    private long id;

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

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

    @Column(name = "zipcode")
    private String zipCode;

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

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

    public Company() {

    }

    public Company (String name, String address, String zipCode, String eMail, String mobile) {
        this.name = name;
        this.address = address;
        this.zipCode = zipCode;
        this.eMail = eMail;
        this.mobile = mobile;
    }

    @JsonProperty
    public long getId() {
        return id;
    }

    @JsonProperty
    public void setId(long id) {
        this.id = id;
    }

    @JsonProperty
    public String getName() {
        return name;
    }

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

    @JsonProperty
    public String getAddress() {
        return address;
    }

    @JsonProperty
    public void setAddress(String address) {
        this.address = address;
    }

    @JsonProperty
    public String getZipCode() {
        return zipCode;
    }

    @JsonProperty
    public void setZipCode(String zipCode) {
        this.zipCode = zipCode;
    }

    @JsonProperty
    public String geteMail() {
        return eMail;
    }

    @JsonProperty
    public void seteMail(String eMail) {
        this.eMail = eMail;
    }

    @JsonProperty
    public String getMobile() {
        return mobile;
    }

    @JsonProperty
    public void setMobile(String mobile) {
        this.mobile = mobile;
    }

 }

DAO 在这里:

public class CompanyDAO extends AbstractDAO<Company> {

    public CompanyDAO(SessionFactory sessionFactory) {
        super(sessionFactory);
    }
    public List<Company> findAll() {
        return list(namedQuery("com.webapp.project.core.Company.findAll"));
    }

    public Optional<Company> findById(long id) {
        return Optional.fromNullable(get(id));
    }
}

申请class:

public class HelloWorldApplication extends Application<HelloWorldConfiguration> {
    public static void main(String[] args) throws Exception {
        new HelloWorldApplication().run(args);
    }

    @Override
    public String getName() {
        return "hello-world";
    }
    /**
     * Hibernate bundle.
     */
    private final HibernateBundle<HelloWorldConfiguration> hibernateBundle
            = new HibernateBundle<HelloWorldConfiguration>(
                    Company.class
            ) {
                @Override
                public DataSourceFactory getDataSourceFactory(
                        HelloWorldConfiguration configuration
                ) {
                    return configuration.getDataSourceFactory();
                }
            };



    @Override
    public void initialize(Bootstrap<HelloWorldConfiguration> bootstrap) {
        bootstrap.addBundle(new SwaggerBundle<HelloWorldConfiguration>() {
            @Override
            protected SwaggerBundleConfiguration getSwaggerBundleConfiguration(HelloWorldConfiguration sampleConfiguration) {
                return sampleConfiguration.getSwaggerBundleConfiguration();
            }
        });
        bootstrap.addBundle(hibernateBundle);
    }

    @Override
    public void run(HelloWorldConfiguration configuration,
                    Environment environment) {

        final CompanyDAO companyDAO = new CompanyDAO(hibernateBundle.getSessionFactory());
        environment.jersey().register(new CompaniesResource(companyDAO));
        environment.jersey().register(new JsonProcessingExceptionMapper(true));
    }

}

配置class:

public class HelloWorldConfiguration extends Configuration {

    @Valid
    @NotNull
    private DataSourceFactory database = new DataSourceFactory();

    @NotNull
    private SwaggerBundleConfiguration swaggerBundleConfiguration;


    @JsonProperty("swagger")
    public void setSwaggerBundleConfiguration (SwaggerBundleConfiguration conf) {
        this.swaggerBundleConfiguration = conf;
    }

    @JsonProperty("swagger")
    public SwaggerBundleConfiguration getSwaggerBundleConfiguration () {
        return swaggerBundleConfiguration;
    }

    @JsonProperty("database")
    public void setDataSourceFactory(DataSourceFactory factory) {
        this.database = factory;
    }

    @JsonProperty("database") 
    public DataSourceFactory getDataSourceFactory() {
        return database;
    }

}

资源class:

@Path("/companies")
@Api("Companies")
@Produces(MediaType.APPLICATION_JSON)
public class CompaniesResource {
    private CompanyDAO companyDAO;

    public CompaniesResource(CompanyDAO companyDAO) {
        this.companyDAO = companyDAO;
    }

    @GET
    @ApiOperation(
            value = "Gives list of all companies",
            response = Company.class,
            code = HttpServletResponse.SC_OK
    )
    @UnitOfWork
    public List<Company> findAll () {
        return companyDAO.findAll();
    }

    @GET
    @Path("/{id}")
    @UnitOfWork
    public Optional<Company> getById(@PathParam("id") LongParam id) {
        return companyDAO.findById(id.get());
    }
}

我很乐意收到任何回复!

看起来您的 json 编组器无法编组 google 的可选 class。尝试 return 来自控制器的公司,而不是可选的:

@GET
@Path("/{id}")
@UnitOfWork
public Company getById(@PathParam("id") LongParam id) {
    return companyDAO.findById(id.get()).get();
}

我收到错误 无法处理 JSON。 排查了4个多小时才发现问题。

错误是因为Enum getter.

如果您在 POJO 中使用 Enum fields/getters/setters,Jackson 将无法将您的 JSON 映射到 Java 对象,并且它会崩溃,导致上述错误。