简体   繁体   English

REST批注注入以及在一个请求中注入自定义XML对象

[英]REST annotations injection together with injection of custom XML objects in one request

1) I'm dealing with similar situation like at How can I pass complex objects as arguments to a RESTful service? 1)我正在处理类似的情况,例如如何将复杂对象作为参数传递给RESTful服务? , but actually the injection of my custom XML objects if injected all right, IF i do not annotate method parameter with @Form. ,但实际上注入了我的自定义XML对象(如果可以的话),前提是我没有使用@Form注释方法参数。

This is wrapper request object to injection: 这是注入的包装请求对象:

@XmlRootElement(name = "request")
@XmlAccessorType(XmlAccessType.NONE)
@XmlType
@Consumes({"application/xml", "application/json"})
public class TestRequest {
    @PathParam(value = "value")
    private String value;  // this is injected only when @Form param is added to the method parameter definition

    @XmlElement(type = Test.class)
    private Test test; // this is XML object I want to inject from the REST request

    @XmlElement
    private String text; // or inject other XML element like this
}

So this would inject me REST parameters (eg {value} - @PathParam("value") annotated in TestRequest). 因此,这将为我注入REST参数(例如,在TestRequest中注释的{value}-@PathParam(“ value”))。 BUT this doesn't unmarshall the XML object Test in wrapper object TestRequested. 但是这不会在包装对象TestRequested中解组XML对象Test。

@POST
@Path("tests/{value}")
@Consumes("application/xml")
@Produces("application/xml")    
public void addTest(**@Form** TestRequest req);

And following definition would inject only the XML object Test but doesn't inject the REST annotations (eg {value} from URI): 并且以下定义将仅注入XML对象Test而不会注入REST注释(例如,来自URI的{value}):

public void addTest(TestRequest req);  // without @Form annotation now

2) I also tried another approach by catching request to custom MessageBodyReader implementation but have been lost in the details where to find code, method or class of JAX-RS or RESTEasy that actually does this parsing/injecting of REST annotations(@PathParam, @QueryParam,...). 2)我还尝试了另一种方法,即捕获对自定义MessageBodyReader实现的请求,但在实际执行REST注释的此解析/注入的JAX-RS或RESTEasy的代码,方法或类的详细信息中迷路了, QueryParam,...)。

I also noticed that when there is @Form annotation in method definition, then custom MessageBodyReader isn't even catched (propably built-in one for REST parameters catches that request and custom reader is then ignored). 我还注意到,当方法定义中包含@Form批注时,甚至都不会捕获自定义MessageBodyReader(针对REST参数的内置内置捕获该请求,然后将忽略自定义阅读器)。

I could in this custom message body reader solution somehow call that built-in injection provider but i didn't find suitable documentation and it seems I'm doing something wrong and all can be done more simplier some other way. 我可以在此自定义消息正文阅读器解决方案中以某种方式调用该内置的注入提供程序,但我找不到合适的文档,而且看来我做错了什么,而所有其他操作都可以更简单地完成。


To summarise the goal: inject both REST parameters (@PathParam, @QueryParam etc.) and custom XML/JSON objects somehow in one request in ONE wrapper object. 总结目标:将REST参数(@ PathParam,@ QueryParam等)和自定义XML / JSON对象注入到一个包装对象中的一个请求中。 (It works with one wrapper object annotated @Form and the other parameter to be without @Form annotation, but I would like to have all in one wrapper object). (它与一个带有@Form批注的包装对象一起工作,而另一个参数没有@Form批注,但我希望将全部包装在一个包装对象中)。

Thank you for any insights or help. 感谢您的任何见解或帮助。

You are mixing JAX-RS and JAXB annotations. 您正在混合JAX-RS和JAXB批注。 That's a bad idea. 那是个坏主意。 Use JAX-RS annotations on resource classes and JAXB annotations on represenation classes. 在资源类上使用JAX-RS批注,在表示类上使用JAXB批注。

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

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