我正在使用Jax-rs,并且有一种方法可以在其帖子中接收复杂对象。
所以我试图让BeanParam para接收它。
这是服务POST方法:
@POST
public void create(@BeanParam BeanNewPack newPack) {
newPack.getPacks().stream().forEach(pack::insert);
}
在我的BeanNewPack中,我正在放置字段并使用FormParam进行注释(因为我正在接收POST)。我正在制作两个Long字段和另一个BeanParam的List。检查以下内容:
@FormParam("id_from")
private Long idFrom;
@FormParam("id_store")
private Long idStore = -1L;
@FormParam("products_to")
private List<ProductsTo> productsTo;
所以在我的产品中我有以下字段:
@FormParam("id")
private Long id;
@FormParam("amount")
private double amount;
@FormParam("percent")
private double pricePercent;
我的意图是发布一个像这样的json:
{
"id_from": 1,
"id_store": -1,
"products_to": [
{"id": 2, "amount": 1, "porcent": 0.5},
{"id": 3, "amount": 1, "porcent": 0.5}
]
}
现在我正在使用Wildfly 10,所以当我尝试部署应用程序时,请获取以下错误:
11:32:47,881 ERROR [org.jboss.msc.service.fail] (ServerService Thread Pool -- 63) MSC000001: Failed to start service jboss.undertow.deployment.default-server.default-host./: org.jboss.msc.service.StartException in service jboss.undertow.deployment.default-server.default-host./: java.lang.RuntimeException: RESTEASY003875: Unable to find a constructor that takes a String param or a valueOf() or fromString() method for javax.ws.rs.FormParam("produtos_para") on private java.util.List ....services.pack.pack.BeanNovoPack.produtosPara for basetype: ....services.pack.servicos.pack.ProdutoPara
at org.wildfly.extension.undertow.deployment.UndertowDeploymentService$1.run(UndertowDeploymentService.java:85)
at java.util.concurrent.Executors$RunnableAdapter.call(Executors.java:511)
at java.util.concurrent.FutureTask.run(FutureTask.java:266)
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1142)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:617)
at java.lang.Thread.run(Thread.java:745)
at org.jboss.threads.JBossThread.run(JBossThread.java:320)
Caused by: java.lang.RuntimeException: RESTEASY003875: Unable to find a constructor that takes a String param or a valueOf() or fromString() method for javax.ws.rs.FormParam("produtos_para") on private java.util.List br.com.softbox.ivendas.servicos.pack.BeanNovoPack.produtosPara for basetype: br.com.softbox.ivendas.servicos.pack.ProdutoPara
at org.jboss.resteasy.core.StringParameterInjector.initialize(StringParameterInjector.java:220)
at org.jboss.resteasy.core.StringParameterInjector.<init>(StringParameterInjector.java:64)
at org.jboss.resteasy.core.FormParamInjector.<init>(FormParamInjector.java:28)
at org.jboss.resteasy.core.InjectorFactoryImpl.createParameterExtractor(InjectorFactoryImpl.java:176)
at org.jboss.resteasy.cdi.CdiInjectorFactory.createParameterExtractor(CdiInjectorFactory.java:124)
at org.jboss.resteasy.core.PropertyInjectorImpl.getParameterExtractor(PropertyInjectorImpl.java:118)
at org.jboss.resteasy.core.PropertyInjectorImpl.populateMap(PropertyInjectorImpl.java:66)
at org.jboss.resteasy.core.PropertyInjectorImpl.<init>(PropertyInjectorImpl.java:54)
at org.jboss.resteasy.core.InjectorFactoryImpl.createPropertyInjector(InjectorFactoryImpl.java:65)
at org.jboss.resteasy.cdi.CdiInjectorFactory.createPropertyInjector(CdiInjectorFactory.java:113)
at org.jboss.resteasy.core.FormInjector.<init>(FormInjector.java:37)
at org.jboss.resteasy.core.InjectorFactoryImpl.createParameterExtractor(InjectorFactoryImpl.java:119)
at org.jboss.resteasy.cdi.CdiInjectorFactory.createParameterExtractor(CdiInjectorFactory.java:52)
at org.jboss.resteasy.core.MethodInjectorImpl.<init>(MethodInjectorImpl.java:44)
at org.jboss.resteasy.core.InjectorFactoryImpl.createMethodInjector(InjectorFactoryImpl.java:77)
at org.jboss.resteasy.cdi.CdiInjectorFactory.createMethodInjector(CdiInjectorFactory.java:58)
at org.jboss.resteasy.core.ResourceMethodInvoker.<init>(ResourceMethodInvoker.java:99)
at org.jboss.resteasy.core.ResourceMethodRegistry.processMethod(ResourceMethodRegistry.java:281)
at org.jboss.resteasy.core.ResourceMethodRegistry.register(ResourceMethodRegistry.java:252)
at org.jboss.resteasy.core.ResourceMethodRegistry.addResourceFactory(ResourceMethodRegistry.java:222)
at org.jboss.resteasy.core.ResourceMethodRegistry.addResourceFactory(ResourceMethodRegistry.java:194)
at org.jboss.resteasy.core.ResourceMethodRegistry.addResourceFactory(ResourceMethodRegistry.java:180)
at org.jboss.resteasy.core.ResourceMethodRegistry.addResourceFactory(ResourceMethodRegistry.java:157)
at org.jboss.resteasy.core.ResourceMethodRegistry.addPerRequestResource(ResourceMethodRegistry.java:76)
at org.jboss.resteasy.spi.ResteasyDeployment.registration(ResteasyDeployment.java:404)
at org.jboss.resteasy.spi.ResteasyDeployment.start(ResteasyDeployment.java:245)
at org.jboss.resteasy.plugins.server.servlet.ServletContainerDispatcher.init(ServletContainerDispatcher.java:113)
at org.jboss.resteasy.plugins.server.servlet.HttpServletDispatcher.init(HttpServletDispatcher.java:36)
at io.undertow.servlet.core.LifecyleInterceptorInvocation.proceed(LifecyleInterceptorInvocation.java:117)
at org.wildfly.extension.undertow.security.RunAsLifecycleInterceptor.init(RunAsLifecycleInterceptor.java:78)
at io.undertow.servlet.core.LifecyleInterceptorInvocation.proceed(LifecyleInterceptorInvocation.java:103)
at io.undertow.servlet.core.ManagedServlet$DefaultInstanceStrategy.start(ManagedServlet.java:231)
at io.undertow.servlet.core.ManagedServlet.createServlet(ManagedServlet.java:132)
at io.undertow.servlet.core.DeploymentManagerImpl.start(DeploymentManagerImpl.java:526)
at org.wildfly.extension.undertow.deployment.UndertowDeploymentService.startContext(UndertowDeploymentService.java:101)
at org.wildfly.extension.undertow.deployment.UndertowDeploymentService$1.run(UndertowDeploymentService.java:82)
... 6 more
PS:我已经翻译了上面的类名,但是没有在最后一个日志中,我相信没有问题需要理解,请告诉我是否如此。
我得到了以下内容:
Unable to find a constructor that takes a String param or a valueOf() or fromString() method for javax.ws.rs.FormParam("products_from") on private java.util.List ....services.pack.pack.BeanNewPack.productsTo for basetype: ....services.pack.ProducsTo
有没有办法在另一个BeanParam下创建BeanParam列表?
答案 0 :(得分:1)
您不能将@BeanParam
或@FormParam
用于JSON。如果您不使用任何注释并遵循正确的JavaBean约定,则不需要执行任何其他操作。它会自动为您转换。
@POST
@Consumes("application/json")
public void create(BeanNewPack newPack) {
并删除所有@FormParam
。
如果您的属性不遵循JavaBean约定,或者如果您的JSON中有一些不太符合命名约定的JSON属性名称,那么您可以使用@JsonProperty
@JsonProperty("id_from")
private Long idFrom;
@JsonProperty("id_store")
private Long idStore = -1L;
@JsonProperty("products_to")
private List<ProductsTo> productsTo;
你需要确保你有杰克逊编译。杰克逊已经包含在Wildfly中,所以你可以添加一个provided
依赖
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-annotations</artifactId>
<version>2.8.4</version>
<scope>provided</scope>
</dependency>
如果您不想添加任何额外的依赖项,那么您需要遵循JavaBean命名约定,例如。
"id_from": 1
// class
public void setId_from() {}
我知道,这很丑陋。但是如果你想在你的JSON中使用下划线,那就是它需要的东西。有一种方法可以让蛇盒自动进行驼峰式转换,但这可能超出了这个问题的范围。只是熟悉使用@JsonProperty
。如果您想要了解有关自动转换的更多信息,只需进行搜索或发布其他问题。