标签: jax-rs

RESTful Web 服务 - 多种 POST 方法。如何从android应用程序中调用特定的一个?

我使用 RESTful 方法编写了一个 Web 服务,并且有不止一种接受 POST 请求的方法,例如:

@Path("/user")
class User{
    @POST
    @Consumes(MediaType.APPLICATION_FORM_URLENCODED)
    @Produces(MediaType.APPLICATION_JSON)
    public UserMasterDto getUserDetails(MultivaluedMap<String, String> userParams) {
         // other stuff..
    }

    @POST
    @Consumes(MediaType.APPLICATION_FORM_URLENCODED)
    @Produces(MediaType.APPLICATION_JSON)
    public UserMasterDto getUserDetails2(MultivaluedMap<String, String> userParams){
         // other stuff..
    }
}
Run Code Online (Sandbox Code Playgroud)

getUserDetails2我想从 Android 应用程序中调用特定的方法。我需要做什么?

android web-services jax-rs

0
推荐指数
1
解决办法
4815
查看次数

使用拦截器设置响应头?

我正在编写 jax-rs 端点。对于某些端点集(现有代码),我想设置一个额外的响应标头,它实际上是在 @AroundInvoke 拦截器中生成的,并设置为 HttpServletRequest 属性。在@AroundInvoke 中,我可以使用@Inject 访问HttpServletRequest。但似乎我无法在同一个拦截器本身中访问 HttpServletResponse 。

似乎我可以使用 PostProcessorInterceptor 但我再次对以下文档感到困惑。

org.jboss.resteasy.spi.interception.PostProcessInterceptor 在 JAX-RS 方法被调用之后但在 MessageBodyWriters 被调用之前运行。它们只能在服务器端使用。如果您需要在可能没有调用任何 MessageBodyWriter 时设置响应标头请使用它们。

我正在使用resteasy,杰克逊。如果我使用 PostProcessorInterceptor 可以注入 HttpServletResponse 吗?或者我可以在那里设置新的 http 标头吗?

任何代码示例/方向将不胜感激。

java jax-rs interceptor resteasy jakarta-ee

0
推荐指数
1
解决办法
2000
查看次数

部署 WAR 时出现 AmbiguousResolutionException

我在部署到 WildFly 8.2.1 时遇到一个奇怪的错误,这与 Resteasy 和 maven 设置有关。我已经失去了无数个小时的谷歌搜索无济于事。
- 无法实例化 MessageBodyReader
-AmbiguousResolutionException: WELD-001318: 无法解决之间的不明确依赖关系

项目组织:

-root  
-project  
--project-ear
--project-main
--project-rest
--project-jms
Run Code Online (Sandbox Code Playgroud)

根 pom.xml

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <groupId>si.fri.liis.jmsprojekt</groupId>
    <artifactId>root</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <packaging>pom</packaging>

    <properties>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
    </properties>

    <dependencyManagement>
        <dependencies>
            <dependency>
                <groupId>org.wildfly.bom</groupId>
                <artifactId>jboss-javaee-7.0-with-all</artifactId>
                <version>8.2.1.Final</version>
                <scope>import</scope>
                <type>pom</type>
            </dependency>
            <dependency>
                <groupId>org.wildfly</groupId>
                <artifactId>wildfly-ejb-client-bom</artifactId>
                <version>8.2.0.Final</version>
                <scope>import</scope>
                <type>pom</type>
            </dependency>
            <dependency>
                <groupId>javax</groupId>
                <artifactId>javaee-api</artifactId>
                <version>7.0</version>
                <scope>provided</scope>
            </dependency>
            <dependency>
                <groupId>javax</groupId>
                <artifactId>javaee-web-api</artifactId>
                <version>7.0</version>
                <scope>provided</scope>
            </dependency>
            <dependency>
                <groupId>org.jboss.resteasy</groupId>
                <artifactId>resteasy-jaxrs</artifactId>
                <version>3.0.10.Final</version>
                <scope>provided</scope>
            </dependency>
        </dependencies>
    </dependencyManagement>

    <build>
        <pluginManagement>
            <plugins>
                <plugin>
                    <groupId>org.apache.maven.plugins</groupId>
                    <artifactId>maven-compiler-plugin</artifactId>
                    <version>3.3</version>
                    <configuration> …
Run Code Online (Sandbox Code Playgroud)

jax-rs resteasy maven jakarta-ee

0
推荐指数
1
解决办法
1604
查看次数

使用 WebLogic 12C 支持 JAX-RS 2.0

我正在阅读以下链接以获得 weblogic 支持的 Jaxrs 2.0

https://docs.oracle.com/middleware/1213/wls/WLUPG/upgrade_ws.htm#WLUPG332 https://docs.oracle.com/middleware/1213/wls/RESTF/use-jersey20-ri.htm#RESTF297

我发现 javax.ws.rs-api-2.0.jar 文件重命名为 javax.ws.rs-api-2.0.war。然后将其作为库文件部署到 weblogic 中。

但是我仍然遇到错误,因为 weblogic 仍然使用 jaxrs 1.1 API。

org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'openStackService': Invocation of init method failed;
hodError: javax.ws.rs.core.MultivaluedMap.addAll(Ljava/lang/Object;[Ljava/lang/Object;)V
Run Code Online (Sandbox Code Playgroud)

这是我的所有配置:

pom.xml

<dependency>
        <groupId>org.glassfish.jersey.core</groupId>
        <artifactId>jersey-client</artifactId>
        <version>2.10.1</version>
    </dependency> 

        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-jdbc</artifactId>
            <version>${org.springframework.version}</version>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-core</artifactId>
            <version>${org.springframework.version}</version>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-web</artifactId>
            <version>${org.springframework.version}</version>
        </dependency>

        <dependency>
            <groupId>org.pacesys</groupId>
            <artifactId>openstack4j</artifactId>
            <version>2.0.9</version>
        </dependency>
Run Code Online (Sandbox Code Playgroud)

weblogic.xml

<?xml version="1.0" encoding="UTF-8"?>
<wls:weblogic-web-app xmlns:wls="http://xmlns.oracle.com/weblogic/weblogic-web-app"
        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
        xsi:schemaLocation="http://xmlns.oracle.com/weblogic/weblogic-web-app 
           http://xmlns.oracle.com/weblogic/weblogic-web-app/1.7/weblogic-web-app.xsd">

    <context-root>jaxrsrest</context-root>
    <wls:library-ref> 
       <library-name>javax.ws.rs</library-name>
       <specification-version>2.0</specification-version>
       <exact-match>false</exact-match>
    </wls:library-ref>
</wls:weblogic-web-app>
Run Code Online (Sandbox Code Playgroud)

在此处输入图片说明

java spring weblogic jax-rs

0
推荐指数
1
解决办法
5809
查看次数

Jax-RS 过滤器将对象传递给资源

我想将我在过滤器中用于身份验证的用户对象传递给资源。是否可以?

我正在使用wildfly 10(resteasy 3)

@Secured
@Provider
@Priority(Priorities.AUTHENTICATION)
public class AuthenticationFilter implements ContainerRequestFilter {

  @Inject
  private UserDao userDao;

  @Override
  public void filter(ContainerRequestContext requestContext) throws IOException {

    logger.warn("Filter");
    String uid = requestContext.getHeaderString("Authorization");
    User user;
    if((user = validateUser(uid)) == null) {
        requestContext.abortWith(
                Response.status(Response.Status.UNAUTHORIZED).build());
    }
  }

  private User validateUser(String uid) {
    return userDao.getById(uid);
  }
}
Run Code Online (Sandbox Code Playgroud)

java jax-rs

0
推荐指数
1
解决办法
1212
查看次数

Java Rest API 在不等待响应的情况下调用另一个 Rest - 在 JAX-RS 中

我有一个案例要在我的项目中实施。下面是一个必须实施的示例休息服务

    @GET
    @Path("/test/{id}")
    @Consumes(MediaType.APPLICATION_JSON)
    @Produces(MediaType.APPLICATION_JSON)
    public String getData(@PathParam("id") String id) {
        //Some processing to get value of String
        String result = doSomeProcessing();

        //I want to return this result to GUI and call one more rest api  
        // and end this process without waiting for response from second 
        //call

       new Thread(){
       //call second rest api
       }.start();

       return result;      

    }
Run Code Online (Sandbox Code Playgroud)

这是使用新线程调用第二个休息 API 并返回结果而不等待第二个休息 API 响应的好方法吗?我还研究了异步 Rest 调用,但它并不完全符合我的要求。请指教。提前致谢

java rest web-services jax-rs

0
推荐指数
1
解决办法
6076
查看次数

如何在 CXF 中的 ContainerRequestFilter 中注入 ResourceInfo

我正在尝试使用 Apache CXF 和 Spring 构建基于令牌的身份验证和授权系统。我完全按照这个很棒的帖子来做这件事。但是我一开始就遇到了 AuthorizationFilter 的问题。我看过很多关于它的帖子,apache JIRA,github 评论,但还没有找到解决这个看似 CXF 问题的方法。

@PreMatching
@Priority(Priorities.AUTHORIZATION)
public class AuthorizationFilter implements ContainerRequestFilter {
private ResourceInfo resourceInfo;

@Context
public void setResourceInfo(ResourceInfo resourceInfo) {
    this.resourceInfo = resourceInfo;
}

@Override
public void filter(final ContainerRequestContext requestContext) throws IOException {

    Method method = resourceInfo.getResourceMethod();
Run Code Online (Sandbox Code Playgroud)

在上面的代码中,注入的 resourceInfo 是一个代理对象,它没有任何关联的属性存在。因此,来自该resourceInfo对象的任何内容都将返回null,更具体地说resourceInfo.getResourceMethod()null,导致 NPE。这是关于这个问题的JIRA帖子,说:

在某些情况下(例如使用 @ApplicationScoped 注释),CDI 运行时将为特定 bean 创建代理类。结果,CXF 端将把特定的提供者元数据绑定到这个代理实例。它看起来合乎逻辑且毫不含糊。

然而,当 CXF 尝试将上下文代理(@Context 注释)注入提供程序实例时,有趣的事情正在发生。注入成功,但它们的目标对象将是代理实例(而不是其背后的真实实例)。因此,在运行时,当代理将调用委托给支持实例时,所有上下文代理在那里都是空的(简单地说,没有设置)。

参考最近与 Sergey Beryozkin 的讨论,最好的解决方案是将 @Context …

rest ws-security spring cxf jax-rs

0
推荐指数
1
解决办法
854
查看次数