简体   繁体   English

具有动态JSON的Jax-RS Restful POST

[英]Jax-RS Restful POST with dynamic JSON

I have a REST service. 我有一个REST服务。 One of the endpoints on that service is a POST that looks like 'path/pathParam/{pathParamVal}'. 该服务的端点之一是看起来像“ path / pathParam / {pathParamVal}”的POST。 Let's say {pathParamVal} can be between 1 and 6. Usually, all I have to do is specify how the JSON should be marshaled on the calling function's parameter of the @POST request like: 假设{pathParamVal}可以在1到6之间。通常,我要做的就是指定如何在@POST请求的调用函数的参数上编组JSON,例如:

@POST
@Path("/path/pathParam/{pathParamVal}")
public void processPost(List<SomeJsonObject> someJsonObject) {
}

And that works assuming SomeJsonObject is defined correctly. 假设SomeJsonObject的定义正确,那行得通。 However, my problem is that the JSON being passed in changes based on the value of {pathParamVal}. 但是,我的问题是传入的JSON根据{pathParamVal}的值进行更改。 To be specific, the JSON structure is always the same when {pathParamVal} is either 1,3,4,5, or 6. However, it changes when {pathParamVal} is 2. Likewise, since the JSON changes, the class definition for that JSON changes as well. 具体来说,当{pathParamVal}为1、3、4、5或6时,JSON结构始终相同。但是,当{pathParamVal}为2时,JSON结构将更改。同样,由于JSON更改,因此JSON也随之改变。 Something like: 就像是:

@POST
@Path("/path/pathParam/{pathParamVal}") // == 1,3,4,5,6
public void processPost(List<SomeJsonObject> someJsonObject) {
}

@POST
@Path("/path/pathParam/{pathParamVal}") // == 2
public void processPost(List<SomeOtherJsonObject> someOtherJsonObject) {
}

is what I'm wanting. 是我想要的 I know for a fact that these are the only two Lists that the JSON coming in will be mapped to, and there's a .0001% chance that it will ever change. 对于一个事实,我知道这是传入的JSON将被映射到的仅有的两个列表,并且它永远都会改变的可能性为.0001%。 So is what I'm trying to achieve possible? 那我想达到的目标是可能的吗? I tried using generics and wildcards but I'm not too familiar with them so I may have not been implementing them correctly. 我尝试使用泛型和通配符,但我对它们不太熟悉,因此可能没有正确实现它们。

If push comes to shove and this isn't possible, I guess I could always just combine the class definitions of SomeJsonObject and SomeOtherJsonObject into something like CombinedJsonObject , then accept List<CombinedJsonObject> combinedJsonObject as a parameter, but the main reason I made this question was to avoid going that route. 如果推不动了,这是不可能的,我想我总是可以将SomeJsonObjectSomeOtherJsonObject的类定义CombinedJsonObjectCombinedJsonObject之类的东西,然后接受List<CombinedJsonObject> combinedJsonObject作为参数,但是我提出这个问题的主要原因是为了避免走那条路。

In my opinion, it is a confusing API. 我认为这是一个令人困惑的API。 I think that instead of numbers, maybe some other kind of literal should be used. 我认为应该用数字代替其他数字。 I mean that pathParam/{pathParamValue} in the URI should be changed by a literal like pathParamMeaning , but, anyway, if you change your url pattern with the number "2" it will be matched for only those requests. 我的意思是URI中的pathParam/{pathParamValue}应该通过类似pathParamMeaning的文字进行更改,但是,无论如何,如果您将URL模式更改为数字“ 2”,则仅将这些请求匹配。

@POST
@Path("/path/pathParam/{pathParamVal}") // == 2
public void processPost(List<SomeOtherJsonObject> someOtherJsonObject) {
}

should be replaced by 应该由

@POST
@Path("/path/pathParam/2") // == 2
public void processPost(List<SomeOtherJsonObject> someOtherJsonObject) {
}

You can even go further using regular expression patterns. 您甚至可以使用正则表达式模式走得更远。 This could be the matching pattern for the first case: others than "2": 这可能是第一种情况的匹配模式:“ 2”以外的其他模式:

@Path("path/pathParam/{pathParamVal : [0-1,3-9]}")

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

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