我正在使用JAX-RS 示例。此示例包含两个 RESTful Web 服务,一个带有注释,另一个没有。
我使用 maven 构建它并将其与 CXF DOSGi 单包分发一起部署到 Felix 中。没有注释的一个工作正常,但是另一个有注释的似乎完全被忽略了。我在日志中收到以下消息:
WARNING: No resource methods have been found for resource class org.apache.cxf.dosgi.samples.greeter.rest.GreeterService
Jun 15, 2011 10:34:17 PM org.apache.cxf.jaxrs.AbstractJAXRSFactoryBean checkResources
SEVERE: No resource classes found
Exception in thread "pool-1-thread-1" org.apache.cxf.service.factory.ServiceConstructionException
at org.apache.cxf.jaxrs.JAXRSServerFactoryBean.create(JAXRSServerFactoryBean.java:122)
at org.apache.cxf.dosgi.dsw.handlers.JaxRSPojoConfigurationTypeHandler.createServer(JaxRSPojoConfigurationTypeHandler.java:135)
at org.apache.cxf.dosgi.dsw.service.RemoteServiceAdminCore.exportService(RemoteServiceAdminCore.java:244)
at org.apache.cxf.dosgi.dsw.service.RemoteServiceAdminInstance$1.run(RemoteServiceAdminInstance.java:78)
at org.apache.cxf.dosgi.dsw.service.RemoteServiceAdminInstance$1.run(RemoteServiceAdminInstance.java:71)
at java.security.AccessController.doPrivileged(Native Method)
at org.apache.cxf.dosgi.dsw.service.RemoteServiceAdminInstance.exportService(RemoteServiceAdminInstance.java:71)
at org.apache.cxf.dosgi.dsw.service.RemoteServiceAdminInstance.exportService(RemoteServiceAdminInstance.java:40)
at org.apache.cxf.dosgi.topologymanager.TopologyManager$2.run(TopologyManager.java:254)
at java.util.concurrent.ThreadPoolExecutor$Worker.runTask(Unknown Source)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(Unknown Source)
at java.lang.Thread.run(Unknown Source)
Caused by: javax.ws.rs.WebApplicationException
at org.apache.cxf.jaxrs.AbstractJAXRSFactoryBean.checkResources(AbstractJAXRSFactoryBean.java:238)
at org.apache.cxf.jaxrs.JAXRSServerFactoryBean.create(JAXRSServerFactoryBean.java:85)
... 11 …Run Code Online (Sandbox Code Playgroud) 我定义了一个 Book 类,我想创建一个 JAXBElement 对象,该对象将包含与来自 String 对象的 XML 对应的信息。
例如,我可以有类似的东西:
String code = "<book><title>Harry Potter</title></book>";
Run Code Online (Sandbox Code Playgroud)
现在,我想从该字符串开始创建一个 JAXBElement。我需要该字符串来执行一些使用 JAXBElement 无法执行的验证。
那么,我可以做我想做的吗?如果是,如何?
谢谢!
索林
我正在尝试使用 JAX-RS 2.0 提供并由 RESTeasy 3.0.5Final 支持的 ContainerRequestFilter 在我的网络服务器中实施安全措施。我使用 JBoss 7.1.1 作为我的平台。
我在我的项目中添加了以下类:
@Provider
@Priority(Priorities.AUTHENTICATION)
public class ServiceInterceptor implements ContainerRequestFilter, ContainerResponseFilter {
@Override
public void filter(ContainerRequestContext arg0) throws IOException {
System.out.println("request filter");
}
@Override
public void filter(ContainerRequestContext arg0, ContainerResponseContext arg1) throws IOException {
System.out.println("response filter");
}
}
Run Code Online (Sandbox Code Playgroud)
但是,当我尝试访问我的 RESTful API 时,这两种方法都没有被调用,例如:
@Path("/users")
@RequestScoped
public class UserRESTService {
@Inject
private UserRepository userRepository;
@GET
@DenyAll
@Produces(MediaType.APPLICATION_JSON)
public List<User> getAll() {
return userRepository.getAll();
}
}
Run Code Online (Sandbox Code Playgroud)
我尝试在 web.xml 文件中添加@Provider:
<context-param>
<param-name>resteasy.providers</param-name>
<param-value>com.package.my.rest.ServiceInterceptor</param-value>
</context-param>
<context-param> …Run Code Online (Sandbox Code Playgroud) 在 Spring 3.1 中,如果我包含以下 BeanFactoryPostProcessor,我可以将具有 `@Scope("request") 的 Jax-RS 资源自动装配到我的单元测试中:
@Component
public class MockRequestBeanFactoryPostProcessor implements BeanFactoryPostProcessor {
public void postProcessBeanFactory(
ConfigurableListableBeanFactory beanFactory) throws BeansException {
beanFactory.registerScope("request", new RequestScope());
MockHttpServletRequest request = new MockHttpServletRequest();
ServletRequestAttributes attributes = new ServletRequestAttributes(request);
RequestContextHolder.setRequestAttributes(attributes);
}
Run Code Online (Sandbox Code Playgroud)
}
在 Spring 3.2 中,第一个运行的测试方法有效,但所有后续的测试方法都得到
java.lang.IllegalStateException: No thread-bound request found: 您是指实际 Web 请求之外的请求属性,还是在原始接收线程之外处理请求?如果您实际上是在 Web 请求中操作并且仍然收到此消息,则您的代码可能在 DispatcherServlet/DispatcherPortlet 之外运行:在这种情况下,请使用 RequestContextListener 或 RequestContextFilter 来公开当前请求。
我怎样才能让我的测试重新工作?
我正在尝试让自动重定向在 Jersey Client 2.0 中工作。这是我的代码:
ClientConfig cc = new ClientConfig().property(ClientProperties.FOLLOW_REDIRECTS, true);
Client c = ClientBuilder.newClient(cc);
WebTarget wt = c.target("some_path");
SystemInfo info = wt.request(MediaType.APPLICATION_XML_TYPE).get(SystemInfo.class);
Run Code Online (Sandbox Code Playgroud)
服务器按预期在位置标头中发送带有另一个 URL 的 HTTP 302。我假设根据Jersey JAXRS 客户端 API,客户端将自动重定向到新的指定 URL,但我收到的是RedirectionException。
这是适当的行为吗?如何在不手动在 try-catch-block 中实现重定向的情况下使客户端重定向工作?
提前致谢!
更新:
我发现了奇怪行为的问题点。如果以编程方式在服务器上进行重定向,例如:
return Response.seeOther(another_uri).build();
Run Code Online (Sandbox Code Playgroud)
一切安好。但就我而言,由于部署描述符中的 security-constraint 元素而进行了重定向:
<security-constraint>
...
<user-data-constraint>
<transport-guarantee>CONFIDENTIAL</transport-guarantee>
</user-data-constraint>
</security-constraint>
Run Code Online (Sandbox Code Playgroud)
因此,客户端从重定向http:// localhost:8080/some_path到https:// localhost:8181/another_path自动servlet容器。在浏览器中它工作正常,但 Jersey 客户端似乎忽略了 FOLLOW_REDIRECTS 属性并抛出一个 RedirectionException 。
在这种情况下是否有机会使重定向正常工作?谢谢!
我试图反序列化来自Javascript UI的JSON对象,但我一直得到400 - "客户端发送的请求语法不正确"错误.注意,我知道REST服务有效,因为如果我对JSON进行硬编码,我可以成功地使用该服务,但现在我正在尝试从实际的JS对象生成JSON.
Spring是否会生成可以帮助解决这个问题的日志消息?如果是这样,那我该怎么设置呢?
使用给定的应用程序
@ApplicationPath("/api")
public class MyApplication {
}
Run Code Online (Sandbox Code Playgroud)
UriInfo#getBaseUri给了我一个应用程序路径。
@Context
private UriInfo uriInfo
uriInfo.getBaseUri(); // http://address/<context-path>/api
Run Code Online (Sandbox Code Playgroud)
我怎样才能获得上下文路径?
如何获取上下文路径的完整 URL?
http://address/<context-path>
Run Code Online (Sandbox Code Playgroud)
更新
我目前使用这个答案中的代码。
@Context
private HttpServletRequest servletRequest;
final URI contextUri
= URI.create(servletRequest.getRequestURL().toString())
.resolve(servletRequest.getContextPath());
Run Code Online (Sandbox Code Playgroud)
还有其他建议吗?
如何使用JAX-RS Client流畅的API为外部世界REST服务创建休息客户端?
例如,假设服务返回具有两个字段名称和年龄的人物对象.
无论我遇到什么示例/教程,它们都会执行与下面代码段相同的操作,或者他们在同一个项目中开发客户端,以便用person.class替换String.class.我应该如何创建独立的客户端,让我回归人.
String entity = client.target("http://example.com/rest")
.path("resource/helloworld")
.queryParam("greeting", "Hi World!")
.request(MediaType.TEXT_PLAIN_TYPE)
.header("some-header", "true")
.get(String.class);
Run Code Online (Sandbox Code Playgroud) 我正在使用Payara 4.1.1.161。我有一个Jersey @Path JAX-RS资源,我要做的就是使用CDI @将一个bean注入其中。我已经尝试了许多不同的组合来使其工作,但是到目前为止,我成功使它成功的唯一方法是在beans.xml中设置bean-discovery-mode =“ all”。
我知道“带注释”是没有bean.xml的首选方式。但是,每次尝试使用“带注释的”时,调用JAX-RS资源都会失败,如下所示:
MultiException stack 1 of 1
org.glassfish.hk2.api.UnsatisfiedDependencyException:
There was no object available for injection at
SystemInjecteeImpl(requiredType=InjectMe, parent=InjectResource,
qualifiers={}, position=-1, optional=false, self=false,
unqualified=null, 1000687916))
Run Code Online (Sandbox Code Playgroud)
或者我在部署如下所示的应用程序时遇到了故障:
Exception during lifecycle processing
java.lang.Exception: java.lang.IllegalStateException:
ContainerBase.addChild: start: org.apache.catalina.LifecycleException:
org.apache.catalina.LifecycleException:
org.jboss.weld.exceptions.DeploymentException: WELD-001408:
Unsatisfied dependencies for type InjectMe with qualifiers @Default
at injection point [BackedAnnotatedField]
@Inject private org.thoth.jaspic.web.InjectResource.me
Run Code Online (Sandbox Code Playgroud)
这是我的应用程序设置。
beans.xml
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://xmlns.jcp.org/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/beans_1_1.xsd"
bean-discovery-mode="all">
</beans>
Run Code Online (Sandbox Code Playgroud)
JAX-RS应用
@javax.ws.rs.ApplicationPath("webresources")
public class ApplicationResourceConfig extends org.glassfish.jersey.server.ResourceConfig {
public ApplicationResourceConfig() …Run Code Online (Sandbox Code Playgroud) 如果我的回答如下:
{
"values": [ "1", "2" ]
}
Run Code Online (Sandbox Code Playgroud)
我应该如何使用readEntity来填充List<String>的价值观:1,2?