您的位置:首页 > 编程语言 > Java开发

cxf 3.2+webservice+spring 4.2发布Rest WebService

2018-03-19 10:32 441 查看
1.接口interface:
@WebService(serviceName="ShowCaseService", endpointInterface = "com.xxx.demo.service.ShowCaseService")
@Path("/showCases")
@Api(value = "/showCases")
public interface ShowCaseService
{

  //http://localhost:8080/pis-web/services/api/showCases
    @GET
    @Produces({ MediaType.APPLICATION_JSON })
    @ApiOperation(
            value = "查询所有ShowCase记录",
            notes = "使用Mabatis进行数据库查询",
            response = ShowCase.class
        )
    List<ShowCase> getAll();
}
实现类impl:
@Service("showCaseWebService")
public class ShowCaseWebServiceImpl implements ShowCaseService
{

    @Autowired 
    private ShowCaseDao showCaseDao ;
    
    @Override
    public List<ShowCase> getAll()
    {
        return showCaseDao .getAll();
    }

}

2.web.xml配置文件:
<context-param>
        <param-name>contextConfigLocation</param-name>
        <param-value>
        <!-- Configuration Locations for Monolith Mode Begin-->
            classpath*:/applicationContext.xml
            /WEB-INF/applicationContext*.xml
            /WEB-INF/cxf-servlet.xml
            /WEB-INF/security.xml
        </param-value>

</context-param>
    <servlet>
        <servlet-name>CXFServlet</servlet-name>
        <servlet-class>org.apache.cxf.transport.servlet.CXFServlet</servlet-class>
    </servlet>
    <servlet-mapping>
        <servlet-name>CXFServlet</servlet-name>
        <url-pattern>/services/*</url-pattern>

    </servlet-mapping>
    <servlet-mapping>
        <servlet-name>CXFServlet</servlet-name>
        <url-pattern>/services/*</url-pattern>
    </servlet-mapping>

    <servlet-mapping>
        <servlet-name>dispatcher</servlet-name>
        <url-pattern>/app/*</url-pattern>
    </servlet-mapping>

3.cxf.xml配置文件:
<jaxws:endpoint id="showCaseService" implementor="#showCaseWebService" address="/ShowCaseService"/>

<jaxrs:server address="/api">
        <jaxrs:serviceBeans>
<!-- 引入实现类--!>   
         <ref bean="showCaseWebService"/>
        </jaxrs:serviceBeans>
    </jaxrs:server>
发布失败原因总结:
1.发布失败原因测试类的注入报错导致项目启动报错。
2.services接口中的方法的参数不能为接口类型,如HttpServletResponse,否则报错:
    Caused by: com.sun.xml.internal.bind.v2.runtime.IllegalAnnotationsException: 1 counts of IllegalAnnotationExceptions
javax.servlet.http.HttpServletResponse是接口, 而 JAXB 无法处理接口。
this problem is related to the following location:
at javax.servlet.http.HttpServletResponse
at private javax.servlet.http.HttpServletResponse com.purang.pis.asset.service.jaxws_asm.ExportData.arg1

at com.purang.pis.asset.service.jaxws_asm.ExportData;
services中的方法;
public String exportData(List<Deal> list, HttpServletResponse response,String assettype,String[] headers_title);

可以找到错误的地方在ExportData方法的参数;

3.http://localhost:8080/services/访问发布的服务地址
4.接口中需要加入的注解:
@GET
@Path("/getAll/")
@Produces({MediaType.APPLICATION_JSON, MediaType.APPLICATION_XML})
否则无法返回结果
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: