Specification and correct use of (boolean) URI matrix parameters (and making them optional when using CXF/JAXB)?

℡╲_俬逩灬. 提交于 2021-02-09 11:52:09

问题


I was wondering if the "proper" use of URI/URL matrix parameters was ever defined in a specification, such as an RFC or a W3 recommendation?

In particular, I just joined a project where we use matrix parameters and a Java framework to implement a REST service. One of the matrix parameters we have for our REST service is a boolean one, much like ;sortByDate=true

What bugged me about this one is that the Java framework we use apparently insists that boolean parameters are always passed in (i.e. you cannot make them optional/omit them; probably because they are converted to Java boolean type). I think that's a bit odd...

I have to doublecheck what framework we use tomorrow (I think it is JAXB), but in the meantime I wondered if matrix parameters were defined in an official specification somewhere, and if such a specification made any mention of boolean parameters.

So far I found a hint (though no mention of boolean matrix parameters) in Appendix B 2.2 of the W3's "HTML 4.01 Specification":

We recommend that HTTP server implementors, and in particular, CGI implementors support the use of ";" in place of "&" to save authors the trouble of escaping "&" characters in this manner.

And the "Web Application Description Language" specification specifies:

Boolean matrix parameters are represented as: ';' name when value is true and are omitted from identifier when value is false

What I haven't found is "the" specification for matrix parameters. Is there any? Does it mention how boolean matrix parameters should be used? If not, is there an established best practice?

And, as a bonus question: can you omit boolean URL matrix parameters when using CXF (JAXB), or do you always have to specify them?

Cheers! :)

Update: We're using CXF (which apparently uses JAXB under the hood...)


回答1:


RFC3986 describes Matrix Parameters without explicitly naming them. Quoting http://tools.ietf.org/html/rfc3986#section-3.3:

For example, the semicolon (";") and equals ("=") reserved characters are often used to delimit parameters and parameter values applicable to that segment. The comma (",") reserved character is often used for similar purposes. For example, one URI producer might use a segment such as "name;v=1.1" to indicate a reference to version 1.1 of "name", whereas another might use a segment such as "name,1.1" to indicate the same.

I hope this helps.




回答2:


I think that this answer does a good job of explaining the purpose of matrix parameters:

https://stackoverflow.com/a/5602678

You can use the Boolean wrapper class to support an optional boolean value. The values true and false will be mapped to the correct boolean values.

@MatrixParam("sortByDate") Boolean sortByDate

It will be null if the param is not present. Note that JAXB doesn't apply when dealing with JAX-RS parameters.



来源:https://stackoverflow.com/questions/20182364/specification-and-correct-use-of-boolean-uri-matrix-parameters-and-making-the

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!