我正在通过 Resteasy 学习 Web 服务
我正在做一个简单的基本示例,易于使用@FormParam。我的示例在请求方法为时有效,POST
但当我将请求方法更改为GET
@Path("/form")
public class FromParamService {
@POST
@Path("/add")
public Response addUser(
@FormParam("name") String name,
@FormParam("age") int age) {
return Response.status(200)
.entity("addUser is called, name : " + name + ", age : " + age)
.build();
}
@GET
@Path("/adduser")
public Response addUser1(
@FormParam("name") String name,
@FormParam("age") int age) {
return Response.status(200)
.entity("addUser is called, name : " + name + ", age : " + age)
.build();
}
}
Run Code Online (Sandbox Code Playgroud)
GET …
我在 Jersey (来自文档)创建了这个测试,它工作正常,但有一个问题:@WebListener ServletContextListener没有被调用。
我需要测试的 Resource 类依赖于 ServletContextListener 在 ServletContext 上设置的属性。
我可以确保它被调用,或者我可以以其他方式操作 ServletContext 吗?
public class SimpleTest extends JerseyTest {
@WebListener
public static class AppContextListener implements ServletContextListener {
@Override
public void contextInitialized(ServletContextEvent event) {
System.out.println("Context initialized");
}
@Override
public void contextDestroyed(ServletContextEvent event) {
System.out.println("Context destroyed");
}
}
@Path("hello")
public static class HelloResource {
@GET
public String getHello() {
return "Hello World!";
}
}
@Override
protected Application configure() {
return new ResourceConfig(HelloResource.class);
}
@Test
public void test() …Run Code Online (Sandbox Code Playgroud) 我有一个使用 CXF 来实现 JAX-RS 的 REST API,其中 REST 端点直接位于根上下文上。
例如,如果我的根上下文是 localhost:8080/myservice
我的端点是:
localhost:8080/myservice/resource1
localhost:8080/myservice/resource2
但我想提供这样的静态内容:
localhost:8080/myservice/docs/swagger.json
在我的 web.xml 中我想做这样的事情:
<servlet-mapping>
<servlet-name>CXFServlet</servlet-name>
<url-pattern>/*</url-pattern>
</servlet-mapping>
<servlet-mapping>
<servlet-name>default</servlet-name>
<url-pattern>/docs/*</url-pattern>
</servlet-mapping>
Run Code Online (Sandbox Code Playgroud)
但这不起作用,CXFServlet 会获取所有请求,并且我找不到配置 CXF / JAX-RS 来服务我的静态内容而不包含新库和创建字节流等的方法,这是我不想做的做。我只想映射到默认的 servlet。
CXF 文档不容易理解,我尝试执行以下操作但没有成功:
<servlet>
<servlet-name>CXFServlet</servlet-name>
<servlet-class>org.apache.cxf.transport.servlet.CXFServlet</servlet-class>
<init-param>
<param-name>static-resources-list</param-name>
<param-value>
/docs/(\S)+\.html
/docs/(\S)+\.json
</param-value>
</init-param>
</servlet>
Run Code Online (Sandbox Code Playgroud)
有任何想法吗?
https://docs.jboss.org/resteasy/docs/3.0.6.Final/userguide/html_single/
这些 RESTeasy 文档说:
“由于我们没有使用 jax-rs servlet 映射,因此我们必须定义一个使用 @ApplicationPath 注释进行注释的 Application 类。如果您通过类和单例返回任何空集,您的 WAR 将被扫描以查找 JAX-RS注释资源和提供者类。 ”
然而,有一个名为resteasy.scan的配置开关,对其进行了描述:
“自动扫描 WEB-INF/lib jar 和 WEB-INF/classes 目录中的 @Provider 和 JAX-RS 资源类(@Path、@GET、@POST 等..)并注册它们”
...所以,如果我想自动扫描我的资源,而不需要在 web.xml 中列出它们,或者必须手动将它们的类添加到扩展应用程序的类中的单例列表中...我应该遵循哪一个?因为看起来这两个重叠,除了 resteasy.scan 属性还包含 jar 之外。
另外,我认为resteasy.scan.resources与resteasy.resources完全相同,只是它不扫描jar?除了所有内容都不会返回 404 之外,我怎么知道这些扫描属性是否有效?
我在用resteasy-jaxrs - 3.0.9.FINAL。我有两个独立javax.ws.rs.core.Application的
@ApplicationPath("oauth")
public class OAuthApplication extends Application {
final Set<Class<?>> classes = new HashSet<>();
@Nonnull
@Override
public Set<Class<?>> getClasses() {
classes.add(RegisterResource.class);
classes.add(TokenResource.class);
classes.add(HelloResource.class);
return Collections.unmodifiableSet(classes);
}
}
Run Code Online (Sandbox Code Playgroud)
对于/oauth基于端点和
@ApplicationPath("rest")
public class RestApplication extends Application {
final Set<Class<?>> classes = new HashSet<>();
@Nonnull
@Override
public Set<Class<?>> getClasses() {
classes.add(CategoryResource.class);
classes.add(CategoryGroupResource.class);
classes.add(TransactionResource.class);
classes.add(MonthlySummaryResource.class);
classes.add(MemberResource.class);
return Collections.unmodifiableSet(classes);
}
}
Run Code Online (Sandbox Code Playgroud)
我的过滤器看起来像
@Provider
public class CorsFeature implements Feature {
@Override
public boolean configure(FeatureContext featureContext) {
CorsFilter corsFilter …Run Code Online (Sandbox Code Playgroud) 我希望能够在特定端点方法返回其值后处理对 Spring RestController 的 HTTP 请求的结果。例如我有:
GET /customer/{id}
Run Code Online (Sandbox Code Playgroud)
这通常只返回自定义资源。我定义的端点RestController只是返回一个客户对象。
我希望能够修改HttpEntity根据此返回结果生成的响应。特别是,我想在这个后处理器中完成所有 HATEOAS 工作并将其包装在我的父对象中。
实现这一目标的最佳方法是什么?我会包括我已经尝试过的内容,但我想不出任何可以干净地完成此操作的方法。
在实现 JAX-RS 的框架中,您所需要做的就是实现接口ContainerResponseFilter,然后可以将其添加到 REST 服务器。使用 Jersey 或 CXF 很容易做到这一点。
ContainerResponseFilterSpring REST中有一个概念吗?
我正在使用Jersey实现Restful Web Service.我想将index.jsp显示为欢迎页面.
<%@page contentType="text/html" pageEncoding="UTF-8"%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
"http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Rest Page</title>
</head>
<body>
<h1>Rest is working!</h1>
</body>
</html>
Run Code Online (Sandbox Code Playgroud)
我在web.xml中使用此代码时工作正常:
<servlet-mapping>
<servlet-name>Jersey REST Service</servlet-name>
<url-pattern>/whatever/*</url-pattern>
</servlet-mapping>
<welcome-file-list>
<welcome-file>index.jsp</welcome-file>
</welcome-file-list>
Run Code Online (Sandbox Code Playgroud)
问题是用户模式是这样的:
<url-pattern>/*</url-pattern>
Run Code Online (Sandbox Code Playgroud)
一切正常,除了欢迎页面.我感谢任何帮助.
我创建了一个SOAP拦截器,如CXF文档中所述:
public class SoapMessageInterceptor extends AbstractSoapInterceptor {
public SoapMessageInterceptor() {
super(Phase.USER_PROTOCOL);
}
public void handleMessage(SoapMessage soapMessage) throws Fault {
// ...
}
}
Run Code Online (Sandbox Code Playgroud)
并在Spring的应用程序上下文中将其注册到总线:
<cxf:bus>
<cxf:inInterceptors>
<ref bean="soapMessageInterceptor"/>
</cxf:inInterceptors>
</cxf:bus>
<jaxws:endpoint id="customerWebServiceSoap"
implementor="#customerWebServiceSoapEndpoint"
address="/customerService"/>
Run Code Online (Sandbox Code Playgroud)
一切正常,直到我添加了REST服务:
<jaxrs:server id="customerWebServiceRest" address="/rest">
<jaxrs:serviceBeans>
<ref bean="customerWebServiceRestEndpoint" />
</jaxrs:serviceBeans>
</jaxrs:server>
Run Code Online (Sandbox Code Playgroud)
问题是SOAP拦截器现在也在REST请求上被触发,这导致在调用REST服务时出现类强制转换异常.
<ns1:XMLFault xmlns:ns1="http://cxf.apache.org/bindings/xformat">
<ns1:faultstring xmlns:ns1="http://cxf.apache.org/bindings/xformat">
java.lang.ClassCastException: org.apache.cxf.message.XMLMessage
cannot be cast to org.apache.cxf.binding.soap.SoapMessage
</ns1:faultstring>
</ns1:XMLFault>
Run Code Online (Sandbox Code Playgroud)
有没有办法只通过配置将拦截器限制为SOAP消息?
更新
看起来我错过了描述这个的文档中的页面.向下滚动到JAXRS过滤器和CXF拦截器之间的差异
Apache CFX可以完成任务.但是我想在没有任何弹簧的情况下将它结合起来.谢谢你提前!
这是我想要做的:
@POST
@Path("/MyPath")
@Produces("text/xml")
@RolesAllowed({"user"})
public Output myMethod (@QueryParam("itemId") long ItemId,
@QueryParam("plannedstartdate") Calendar plannedStartDate,
@QueryParam("plannedreturndate") Calendar plannedReturnDate)
Run Code Online (Sandbox Code Playgroud)
我正在使用JBoss AS7.据我所知,resteasy已集成到JBoss AS7中.我能够运行简单的休息服务.
我找到的关于传递日期的唯一文档位于以下链接:http: //docs.jboss.org/resteasy/2.0.0.GA/userguide/html/StringConverter.html#StringParamUnmarshaller
由于说明不明确,我无法遵循此并解决问题.当我尝试创建示例中给出的注释DateFormat时,它无法识别StringParamUnmarshaller.我不知道从哪里得到它.如果resteasy已经集成到JBoss AS7中,这不应该被识别吗?
我的pom.xml具有以下依赖项:
<!-- Import the JAX-RS API, we use provided scope as the API is included
in JBoss AS 7 -->
<dependency>
<groupId>org.jboss.spec.javax.ws.rs</groupId>
<artifactId>jboss-jaxrs-api_1.1_spec</artifactId>
<scope>provided</scope>
</dependency>
Run Code Online (Sandbox Code Playgroud)
对此方法的调用失败,因为不会发生字符串到日历的转换.我不想传递String而不是Calendar,因为有其他客户端直接调用java.任何人都可以帮助我如何将日期传递给Rest Calls?
谢谢Veer