特别是在JAX-RS中(我不确定是否相关)有一些方法允许您将EntityTags添加到响应中.究竟什么是实体标签以及它们使用的实用方法?
是否可以配置GET方法来读取可变数量的URI参数并将它们解释为变量参数(数组)或集合?我知道查询参数可以作为列表/集读取,但在我的情况下我不能用它们.
例如:
@GET
@Produces("text/xml")
@Path("list/{taskId}")
public String getTaskCheckLists(@PathParam("taskId") int... taskId) {
return Arrays.toString(taskId);
}
Run Code Online (Sandbox Code Playgroud)
提前致谢
是否有Java独立实现来提取URI中模板(RFC 6570)定义的参数值?
我发现的最佳实现是ruby实现(https://github.com/sporkmonger/addressable)
通过http://code.google.com/p/uri-templates/wiki/Implementations我找到了一个Java实现:Handy-URI-Templates
它支持使用参数值解析URI模板到最终URI.不幸的是,它无法做到相反:根据URI-Template提取URI中的参数值.
JAX-RS(或Restlet)的实现在内部具有此功能.但是似乎没有一个孤立的这个功能模块可以独立使用.
有没有人有另一个想法?
这里有一个使用spring-Web的例子:
import org.springframework.web.util.UriTemplate;
public class UriParserSpringImpl implements UriParser {
private final UriTemplate uriTemplate;
private final String uriTemplateStr;
public UriParserSpringImpl(final String template) {
this.uriTemplateStr = template;
this.uriTemplate = new UriTemplate(template);
}
@Override
public Map<String, String> parse(final String uri) {
final boolean match = this.uriTemplate.matches(uri);
if (!match) {
return null;
}
return uriUtils.decodeParams(this.uriTemplate.match(uri));
}
@Override
public Set<String> getVariables() {
return Collections.unmodifiableSet(new LinkedHashSet<String>(this.uriTemplate.getVariableNames()));
}
}
Run Code Online (Sandbox Code Playgroud)
Jersey的另一个(JAX-RS实现):
import com.sun.jersey.api.uri.UriTemplate;
public class UriParserJerseyImpl implements …Run Code Online (Sandbox Code Playgroud) 我正在尝试向媒体类型设置为的jaxrs服务执行请求multipart/form-data.此请求包含实体列表(xml)和图像(png,二进制).我已经创建了BalusC 在此主题中描述的请求.
在wireshark中检查它之后,请求似乎没问题,除了ip头校验和错误.(说"可能是由IP校验和卸载引起的".)
这里我的大问题是如何在服务端处理多部分请求.我不希望包含来自apache.cxf,resteasy或任何类型的任何库.我想要依赖的是jaxrs api.
这两部分的要求有名字deliveries和signature,其中签名是发送二进制PNG图像文件.应该从xml解析交付列表(实体具有xmlrootelement注释等,因此这部分单独工作).我尝试用这种方式阅读不同的部分,但这真的是一个长期的结果;
@PUT
@Path("signOff")
@Consumes(MediaType.MULTIPART_FORM_DATA)
public void signOffDeliveries(@FormParam("deliveries") List<Delivery> deliveries, @FormParam("signature")File signature) {
//do something with the signature(image) and the list of deliveries.
}
Run Code Online (Sandbox Code Playgroud)
这当然不起作用,如果我在Websphere上运行请求,它会给我一个404 http状态代码,当我向嵌入式openejb(在我们的集成测试框架中)运行请求时,它会给我一个415.如果我删除FormParam注释,请求成功.
如何仅使用jaxrs api读取多部分请求的不同部分?
编辑
好了,所以我把它编织PUT到了POST,并@Encoding为params 添加了一个注释:
@POST
@Path("signOff")
@Consumes(MediaType.MULTIPART_FORM_DATA)
public void signOffDeliveries(
@Encoded @FormParam("deliveries") String deliveries,
@Encoded @FormParam("signature") File signature) {
}
Run Code Online (Sandbox Code Playgroud)
现在我将xml作为文本字符串,但我无法自动将其解组为交付列表,即使Content-Type有效负载的这部分设置为application/xml.另一个问题是我收到的文件长度== 0,我无法从中读取任何字节.
我在这里错过了一个基本点吗?
我试图通过遵循RESTeasy文档建议的内容,指定仅与我的一些API调用相关联的预匹配过滤器.这是我的代码的样子:
名称绑定:
@NameBinding
public @interface ValidateFoo {}
Run Code Online (Sandbox Code Playgroud)
资源:
@Path("/foo/bar")
@Produces(MediaType.APPLICATION_JSON)
public class FooBar {
@GET
@ValidateFoo
public Object doStuff() {
//do stuff
}
@POST
public Object doAnotherStuff() {
//do another stuff
}
}
Run Code Online (Sandbox Code Playgroud)
过滤:
@ValidateFoo
@Provider
@PreMatching
public class FooValidation implements ContainerRequestFilter {
@Override
public void filter(ContainerRequestContext reqContext) throws IOException {
//validate stuff
}
}
Run Code Online (Sandbox Code Playgroud)
问题是:FooValidation过滤器在每次方法调用之前运行(例如:在GETs和POST之前到/ foo/bar),而不仅仅是那些注释的@ValidateFoo(对我来说似乎是个bug).如果我@Provider从过滤器中删除注释,它将不会在任何调用之前运行(如预期的那样).
我一直在使用WebLogic或Tomcat看到这种行为.我的依赖管理是通过Maven完成的,RESTeasy版本是3.0-beta-3.
任何体验/经历过同样行为的人?我见过另一个在JBoss论坛上遇到类似问题的用户,到目前为止没有运气.
更新:RESTeasy 3.0.1-Final仍然遇到同样的问题.
是否可以使用Jackson设置Jersey以使用多个配置进行序列化/反序列化ObjectMappers?
我希望能够做的是注册一个"默认"杰克逊ObjectMapper,然后能够注册另一个功能,提供ObjectMapper一些特殊配置,在某些情况下将"覆盖""默认" ObjectMapper.
例如,这ContextResolver将是"默认"映射器:
@Provider
@Consumes(MediaType.APPLICATION_JSON)
@Produces(MediaType.APPLICATION_JSON)
public class JacksonMapperProvider implements ContextResolver<ObjectMapper> {
private final ObjectMapper mObjectMapper;
public JacksonMapperProvider() {
mObjectMapper = createMapper();
}
protected abstract ObjectMapper createMapper() {
ObjectMapper mapper = createMapper();
return mapper
.setSerializationInclusion(Include.ALWAYS)
.configure(JsonParser.Feature.ALLOW_COMMENTS, true)
.configure(JsonParser.Feature.ALLOW_UNQUOTED_FIELD_NAMES, true)
.configure(JsonParser.Feature.ALLOW_SINGLE_QUOTES, true)
.configure(JsonParser.Feature.ALLOW_UNQUOTED_CONTROL_CHARS, true);
}
@Override
public ObjectMapper getContext(Class<?> type) {
return mObjectMapper;
}
}
Run Code Online (Sandbox Code Playgroud)
这ContextResolver将覆盖"默认"映射器:
@Provider
@Consumes(MediaType.APPLICATION_JSON)
@Produces(MediaType.APPLICATION_JSON)
public class SpecializedMapperProvider implements ContextResolver<ObjectMapper> {
private final ObjectMapper mObjectMapper; …Run Code Online (Sandbox Code Playgroud) 我的webapp包含一个库,其中包含一个带注释的类@javax.ws.rs.ext.Provider.如果存在此类,则我的webapp(在EAR中部署为WAR)无法启动,并出现以下错误:
<19-Jun-2014 14:41:50 o'clock BST> <Error> <Deployer> <BEA-149265> <Failure occurred in the execution of deployment request with ID "1403185262187" for task "2". Error is: "weblogic.application.ModuleException: com.sun.jersey.api.container.ContainerException: The ResourceConfig instance does not contain any root resource classes."
weblogic.application.ModuleException: com.sun.jersey.api.container.ContainerException: The ResourceConfig instance does not contain any root resource classes.
at weblogic.application.internal.ExtensibleModuleWrapper.start(ExtensibleModuleWrapper.java:140)
at weblogic.application.internal.flow.ModuleListenerInvoker.start(ModuleListenerInvoker.java:124)
at weblogic.application.internal.flow.ModuleStateDriver$3.next(ModuleStateDriver.java:213)
at weblogic.application.internal.flow.ModuleStateDriver$3.next(ModuleStateDriver.java:208)
at weblogic.application.utils.StateMachineDriver.nextState(StateMachineDriver.java:42)
Truncated. see log file for complete stacktrace
Caused By: com.sun.jersey.api.container.ContainerException: The ResourceConfig instance does not contain any root resource classes. …Run Code Online (Sandbox Code Playgroud) 我正在向我的应用程序发送POST JSON请求.
POST /CharSetTest/Test HTTP/1.1
Host: localhost:8090
Content-Type: application/json
Cache-Control: no-cache
Postman-Token: 1637b92b-5896-4765-63c5-d04ad73ea9f1
{
"SampleRequest": {
"FullName": "???"
}
}
Run Code Online (Sandbox Code Playgroud)
我的CXF JAXRS消费者定义如下.
@POST
@Produces("application/json; charset=UTF-8")
@Consumes("application/json; charset=UTF-8")
public Response testCharSet(@Encoded String jsonBody);
Run Code Online (Sandbox Code Playgroud)
但是我作为POST请求发送的日文字符(关连当)没有编码,导致一些垃圾字符" é¢é£å½äºè "
使用SoapUI导致"?????" 字符.
这个垃圾字符在客户端与客户端之间不同,我点击了请求.我如何编码我的POST请求?
我一直在读,JAX-RS是建立在servlet之上的.这是真的,还是只是意味着它是一个更高级别的组件?如果是,那怎么办?JAX-RS是否创建了一个servlet来解析请求并手动初始化带@Path注释的类并将修改后的参数传递给它们?JSR似乎没有指定这一点,并且提及它的书籍都没有涉及任何细节.
注意:我在部署JAX或servlet方面没有问题,我只是对细节很好奇,因为它可以更好地理解Web容器的工作方式.
我正在将我的 JAX-RS REST 项目与 Swagger 集成。我阅读了许多文档和教程,我最喜欢的图如下(感谢Philipp Hauer 的博客):
该图像帮助我了解 Swagger 的工作原理。
在了解 Swagger 的工作原理后,我修改了我的pom.xml.
我swagger-jersey2-jaxrs向我的项目添加了依赖项,这使我能够使用此处描述的与 swagger 相关的注释:
<!-- for Swagger-Core Annotations -->
<dependency>
<groupId>io.swagger</groupId>
<artifactId>swagger-jersey2-jaxrs</artifactId>
<version>1.5.13</version>
</dependency>
Run Code Online (Sandbox Code Playgroud)
重要提示:我无法使用最新的 1.15.18 swagger-jersey2-jaxrs 依赖项,因为属于此依赖项的番石榴库在最新的 (v5.181) Payara应用程序服务器上造成了严重的类加载器问题:
Exception Occurred :Error occurred during deployment: Exception while loading the app : java.lang.IllegalStateException: ContainerBase.addChild: start: org.apache.catalina.LifecycleException: org.apache.catalina.LifecycleException: java.lang.NoSuchMethodError: com.google.common.collect.Sets$SetView.iterator()Lcom/google/common/collect/UnmodifiableIterator;. Please see server.log for more details. ]]
Run Code Online (Sandbox Code Playgroud)
无论如何,将以下插件添加到我的 pom.xml 以及下载swagger-ui部分并将其解压缩到 maven 目标文件夹:
<plugin>
<groupId>com.googlecode.maven-download-plugin</groupId>
<artifactId>download-maven-plugin</artifactId>
<version>1.4.0</version> …Run Code Online (Sandbox Code Playgroud) jax-rs ×10
java ×6
rest ×4
jersey ×3
cxf ×1
http ×1
jackson ×1
parsing ×1
resteasy ×1
restlet ×1
servlets ×1
swagger ×1
swagger-2.0 ×1
swagger-ui ×1
tomcat ×1
uritemplate ×1
web-services ×1
weblogic ×1
weblogic12c ×1