Points of Cxf-rs and swagger

Source: Internet
Author: User

1. Speaking from web.
    <servlet>        <Servlet-name>Cxfservlet</Servlet-name>        <Servlet-class>Org.apache.cxf.transport.servlet.CXFServlet</Servlet-class>        <Load-on-startup>1</Load-on-startup>    </servlet>    <servlet-mapping>        <Servlet-name>Cxfservlet</Servlet-name>        <Url-pattern>/*</Url-pattern>    </servlet-mapping>

If this is configured, all requests go cxf (meaning that a request is made as a resource) and the swagger UI cannot be accessed, so it cannot be configured AS/*

Should be configured as follows:

<?XML version= "1.0" encoding= "Utf-8"?><Web-appxmlns= "HTTP://JAVA.SUN.COM/XML/NS/J2EE"Xmlns:xsi= "Http://www.w3.org/2001/XMLSchema-instance"xsi:schemalocation= "Http://java.sun.com/xml/ns/j2ee http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd"version= "2.4">    <Display-name>Jax-rs Simple Service</Display-name>    <Description>Jax-rs Simple Service</Description>    <Context-param>        <Param-name>Contextconfiglocation</Param-name>        <Param-value>Web-inf/beans.xml</Param-value>    </Context-param>    <Listener>        <Listener-class>Org.springframework.web.context.ContextLoaderListener</Listener-class>    </Listener>    <Listener>        <Listener-class>Org.springframework.web.context.request.RequestContextListener</Listener-class>    </Listener>    <servlet>        <Servlet-name>Cxfservlet</Servlet-name>        <Servlet-class>Org.apache.cxf.transport.servlet.CXFServlet</Servlet-class>        <Load-on-startup>1</Load-on-startup>    </servlet>    <servlet-mapping>        <Servlet-name>Cxfservlet</Servlet-name>        <Url-pattern>/api/*</Url-pattern>    </servlet-mapping></Web-app>

Do this {protocol}{host}{port}{project} {resource}

Supplemental Welcome Files:http://docs.oracle.com/javaee/5/tutorial/doc/bnaeo.html#bnaer

The welcome files mechanism allows you to specify a list of files that the Web container would use for appending to a reque The St for a URL (called a valid partial request), which is not mapped to a Web component.

Example: Access http://localhost:8080/cxf-rs/

will automatically access the index.xxx under WebApp

2. Swagger UI

You can use the Swagger-ui code as-is! As is

Copy the file under ' Dist ' folder to WebApp

or (if you feel messy) copy to the specified folder under WebApp for example: Webapp/swagger

Maven WebApp eventually generates a war package, there is no webapp, and the project directory is WebApp

As a general rule:

For security reasons, non-static resources should be placed inside the web-inf.

If you want to achieve these results, you need:

① Configure welcome file and modify the resource relative path (src or href) index.html in ' dist ' to swagger/....

② Configuration Welcome List

    < welcome-file-list >        < Welcome-file >swagger/index.html</welcome-file>    </  Welcome-file-list>

Then visit http://localhost:8080/cxf-rs/

There's another Swagger UI page.

If you want to modify to index.jsp, you need to do the following:

① creating a new JSP file

<%@ Page Language="Java"ContentType="text/html; Charset=utf-8"pageencoding="Utf-8"%><!DOCTYPE HTML PUBLIC "-//w3c//dtd HTML 4.01 transitional//en" "Http://www.w3.org/TR/html4/loose.dtd "><HTML><Head><Metahttp-equiv= "Content-type"content= "text/html; charset=utf-8"><title>Swagger UI JSP</title>

② change welcome list to Swagger/index.jsp

3. Web Service and Web project

URLs that are intercepted by CXF are accessed as restful resources and generally return JSON

Non-CXF intercept URL is access to WebApp resources, page jump

Both types of access must be clearly divided.

4.swagger Configuration

https://github.com/swagger-api/swagger-core/wiki/Swagger-Core-RESTEasy-2.X-Project-Setup-1.5

Spring mode

    <BeanID= "Swaggerconfig"class= "Io.swagger.jaxrs.config.BeanConfig">        < Propertyname= "Resourcepackage"value= "Cn.zno.resource" />        < Propertyname= "Version"value= "1.0.0" />        < Propertyname= "Host"value= "Localhost:8080/cxf-rs" />        < Propertyname= "BasePath"value= "/api" />        < Propertyname= "title"value= "Swagger petstore" />        < Propertyname= "description"value= "This is a app." />        < Propertyname= "Contact"value= "[email protected]" />        < Propertyname= "License"value= "Apache 2.0" />        < Propertyname= "Licenseurl"value= "http://www.apache.org/licenses/LICENSE-2.0.html" />        < Propertyname= "Scan"value= "true" />    </Bean>

Important attributes:

Resource bundle for Resourcepackage:jax-rs

Host: Access document address

BasePath: All resource URL pattern is appended, for example: Resource @path ("/user") in a Java class, then this resource is ultimately api/user

Other properties:

Title: This set of API headers

Contact information:

Front-facing:

    <!-- /*  -    <!--* Resource -    <!-- * /  -    <BeanID= "Swaggerresource"class= "Io.swagger.jaxrs.listing.ApiListingResource" />    <!-- /*  -    <!--* Provider -    <!-- * /  -    <BeanID= "Swaggerwriter"class= "Io.swagger.jaxrs.listing.SwaggerSerializers" />

The first is to receive a URL request (SWAGGER.JSON/SWAGGER.YAML)

The second is to print out the Swagger object, which is presented to the Swagger UI

Points of Cxf-rs and swagger

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.