繁体   English   中英

泽西岛Web服务返回204无内容

[英]Jersey web service returns 204 no content

环境:

  • 春天4.2.2
  • 泽西岛1.19

描述:

AOP配置

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:aop="http://www.springframework.org/schema/aop"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xsi:schemaLocation="http://www.springframework.org/schema/beans
       http://www.springframework.org/schema/beans/spring-beans.xsd
       http://www.springframework.org/schema/aop
       http://www.springframework.org/schema/aop/spring-aop-4.2.xsd
       ">
    <!-- Aop Bean -->
    <bean id="wsChecker" class="xn.safephone.webapp.aop.AopWebServiceChecker"/>
    <!-- Aop Configuration -->
    <aop:config>
        <aop:aspect id="aopWsChecker" ref="wsChecker">
            <aop:pointcut id="aopWs" expression="execution (* xn.safephone.webapp.webservices.*.*(..))"/>
            <aop:around method="doAround" pointcut-ref="aopWs"/>
        </aop:aspect>
    </aop:config>
</beans>

web.xml中

<servlet>
    <servlet-name>REST-Servlet</servlet-name>
    <servlet-class>com.sun.jersey.spi.spring.container.servlet.SpringServlet</servlet-class>
    <init-param>
        <param-name>com.sun.jersey.config.property.packages</param-name>
        <param-value>xn.safephone.webapp.webservices</param-value>
    </init-param>
    <load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
    <servlet-name>REST-Servlet</servlet-name>
    <url-pattern>/ws/*</url-pattern>
</servlet-mapping>

泽西岛网络服务类

@GET
@Path("test")
@Produces("text/plain")
public String test() {
    return "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx";
}

AOP类

public class AopWebServiceChecker {
    public void doAround(ProceedingJoinPoint joinPoint) throws Throwable {
        System.out.println("1");
        joinPoint.proceed();
        System.out.println("2");
    }
}

结果:

如果我使用AOP运行Web服务,则没有错误,但是客户端返回“ 204 No Content”。 如果我禁用AOP,一切都会很好。 那为什么呢?

非常感谢!

问题在于,在有建议的情况下,您将希望返回一个值。 该值是实际建议方法调用的返回值。 原因是,围绕建议可以让您根据需要修改返回值。

当前,您正在返回void,这会影响资源方法调用返回null。 返回nul时,Jersey的默认行为是仅发送204 No Content,不包含任何数据,因为没有数据。

在方法上调用joinPoint.proceed()的结果将返回一个值,该结果将是该方法调用的返回值。 因此,只需获取该值的引用并返回它即可。

public Object doAround(ProceedingJoinPoint joinPoint) throws Throwable {
    System.out.println("1");
    Object retValue = joinPoint.proceed();
    System.out.println("2");
    return retValue;
}

也可以看看:

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM