java Dropwizard/Jersey 在 GET 请求中给出“无法处理 JSON”消息
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/36773254/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Dropwizard/Jersey is giving "Unable to process JSON" message on GET request
提问by ConeSpook
I'm trying to make my first simple project with Dropwizard. I have a MySQL-database, and the idea is to get the data (companies) from there and represent it as JSON. I have followed the Getting startedpage by Dropwizard and thistutorial to get connected to database with Hibernate.
我正在尝试使用 Dropwizard 制作我的第一个简单项目。我有一个 MySQL 数据库,想法是从那里获取数据(公司)并将其表示为 JSON。我已经按照Dropwizard的入门页面和本教程使用 Hibernate 连接到数据库。
The idea is that URL "/companies" serves all the companies as JSON, and it is working fine.
这个想法是 URL "/companys" 作为 JSON 为所有公司提供服务,并且它工作正常。
URL "/companies/{id}" is supposed to give a single company with given id, but every request gives code 400 and message "Unable to process JSON". The details field in the response says
URL "/companyes/{id}" 应该给出一个具有给定 id 的公司,但每个请求都会给出代码 400 和消息“无法处理 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) )"
“没有找到 jersey.repackaged.com.google.common.base.Present 类的序列化程序,也没有发现创建 BeanSerializer 的属性(为了避免异常,禁用 SerializationFeature.FAIL_ON_EMPTY_BEANS)”
If I give an id of company that does not exist in database, the class in mentioned message changes to
如果我给出数据库中不存在的公司 id,则上述消息中的类将更改为
jersey.repackaged.com.google.common.base.Absent
jersey.repackaged.com.google.common.base.Absent
The company class is here:
公司类在这里:
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 is here:
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));
}
}
Application 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));
}
}
Configuration 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;
}
}
Resource 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());
}
}
I would be happy for any responses!
我会很高兴有任何回应!
采纳答案by gmaslowski
Looks like your json marshaller is not able to marshall google's Optional class. Try to return Company from the controller, and not Optional:
看起来您的 json marshaller 无法编组 google 的 Optional 类。尝试从控制器返回 Company,而不是 Optional:
@GET
@Path("/{id}")
@UnitOfWork
public Company getById(@PathParam("id") LongParam id) {
return companyDAO.findById(id.get()).get();
}
回答by jmojico
I was getting the error Unable to Process JSON. Troubleshooted more than 4 hours until I found the problem.
我收到错误Unable to Process JSON。排了4个多小时才发现问题。
The error is caused because of Enum getter.
该错误是由 Enum getter 引起的。
I you are using Enum fields/getters/setters in your POJO, Hymanson will fail to map your JSON to Java Object, and it will crash, leading to the mentioned error.
如果您在 POJO 中使用 Enum 字段/getter/setter,Hymanson 将无法将您的 JSON 映射到 Java 对象,并且会崩溃,从而导致上述错误。