springMVC+jetty+swagger 简单配置

jetty配置:

<properties>
        <jettyVersion>9.3.0.RC1</jettyVersion>
        <swaggerVersion>2.8.5</swaggerVersion>
    </properties>
        <dependency>
            <groupId>jetty</groupId>
            <artifactId>servlet-api</artifactId>
            <version>2.5-6.0.2</version>
        </dependency>

  </dependencies>

    <build>
        <plugins>
            <plugin>
                <groupId>org.eclipse.jetty</groupId>
                <artifactId>jetty-maven-plugin</artifactId>
                <version>${jettyVersion}</version>

            </plugin>
        </plugins>

    </build>

启动用mavenbuild:
goal中配置如下:

jetty:run -Djetty.port=8085

随后为swagger

<!-- swagger -->
    <dependency>
            <groupId>com.fasterxml.jackson.core</groupId>
            <artifactId>jackson-core</artifactId>
            <version>${swaggerVersion}</version>
        </dependency>
        <dependency>
            <groupId>com.fasterxml.jackson.core</groupId>
            <artifactId>jackson-annotations</artifactId>
            <version>${swaggerVersion}</version>
        </dependency>
        <dependency>
            <groupId>com.fasterxml.jackson.core</groupId>
            <artifactId>jackson-databind</artifactId>
            <version>${swaggerVersion}</version>
        </dependency>

        <dependency>
            <groupId>io.springfox</groupId>
            <artifactId>springfox-swagger2</artifactId>
            <version>2.7.0</version>
        </dependency>
        <dependency>
            <groupId>io.springfox</groupId>
            <artifactId>springfox-swagger-ui</artifactId>
            <version>2.7.0</version>
        </dependency>

spring-mvc中

    <mvc:default-servlet-handler />
    <mvc:resources mapping="/webjars/**" location="classpath:/META-INF/resources/webjars/" />
    <mvc:resources mapping="swagger-ui.html" location="classpath:/META-INF/resources/" />

在spring-context中加入包扫描


    <!-- 扫描注解的包 -->
    <context:component-scan base-package="controller" />
    <context:component-scan base-package="util" />
    <context:component-scan base-package="service" />
    <context:component-scan base-package="service.impl" />

swaggerconfig类编写:

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurationSupport;

import springfox.documentation.builders.ApiInfoBuilder;
import springfox.documentation.builders.PathSelectors;
import springfox.documentation.service.ApiInfo;
import springfox.documentation.service.Contact;
import springfox.documentation.spi.DocumentationType;
import springfox.documentation.spring.web.plugins.Docket;
import springfox.documentation.swagger2.annotations.EnableSwagger2;

    @EnableSwagger2  
    @Configuration  
    public class SwaggerConfig extends WebMvcConfigurationSupport{

        @Bean  
        public Docket createRestApi() {  
            return new Docket(DocumentationType.SWAGGER_2)  
                    .apiInfo(apiInfo())  
                    .select()  
                    .paths(PathSelectors.any())  
                    .build();  
        }  

        private ApiInfo apiInfo() {  
            return new ApiInfoBuilder()  
                    .title("Spring 中使用Swagger2构建RESTful APIs")
                    .description("eos-tdop-app-load")
                    .termsOfServiceUrl("http://sfpp.sf-express.com/")
                    .contact(new Contact("Zhang hang", "", "xingzhang@sf-express.com"))
                    .version("1.0")  
                    .build();
        }  
    }  

注解配置:

@Controller
@Api(value="nihao",description="hello")
public class HelloController {
    @Autowired()@Qualifier("sayGoodbye") 
    Say sy;

@RequestMapping(value="hello",method=RequestMethod.GET)
@ApiOperation(value="shuo nihao",notes="say hello")
public void sayHello(HttpServletRequest request,HttpServletResponse response,MultipartFile file) throws IOException, ServletException {
    System.out.println("ok");
    String   a= sy.say(); 

      request.getSession().setAttribute("a",a);
      request.getRequestDispatcher("index.jsp").forward(request, response);  

}

}

随后,输入mvc中配置地址即可:

http://localhost:8085/swagger-ui.html

pom:

<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/maven-v4_0_0.xsd">
  <modelVersion>4.0.0</modelVersion>
  <groupId>sf</groupId>
  <artifactId>myhelloweb</artifactId>
  <packaging>war</packaging>
  <version>0.0.1-SNAPSHOT</version>
  <name>myhelloweb Maven Webapp</name>
  <url>http://maven.apache.org</url>

    <properties>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
        <jettyVersion>9.3.0.RC1</jettyVersion>
        <swaggerVersion>2.8.5</swaggerVersion>
    </properties>


  <dependencies>

    <dependency>
      <groupId>junit</groupId>
      <artifactId>junit</artifactId>
      <version>3.8.1</version>
      <scope>test</scope>
    </dependency>

    <!-- servlet -->
    <dependency>
    <groupId>javax.servlet</groupId>
    <artifactId>javax.servlet-api</artifactId>
    <version>3.1.0</version>
    </dependency>

     <dependency>  
            <groupId>javax.servlet.jsp</groupId>  
            <artifactId>javax.servlet.jsp-api</artifactId>  
            <version>2.3.1</version>  
        </dependency> 

        <dependency>  
            <groupId>javax.servlet</groupId>  
            <artifactId>jstl</artifactId>  
            <version>1.2</version>  
        </dependency>  

    <!-- spring -->
        <!-- spring -->
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-webmvc</artifactId>
            <version>4.3.8.RELEASE</version>
        </dependency>

    <dependency>
            <groupId>com.fasterxml.jackson.core</groupId>
            <artifactId>jackson-databind</artifactId>
            <version>2.8.0</version>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-test</artifactId>
            <version>4.3.8.RELEASE</version>
        </dependency>
        <dependency>
            <groupId>org.hamcrest</groupId>
            <artifactId>hamcrest-core</artifactId>
            <version>1.3</version>
        </dependency>
        <dependency>
            <groupId>org.hamcrest</groupId>
            <artifactId>hamcrest-parent</artifactId>
            <version>1.3</version>
            <type>pom</type>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-context-support</artifactId>
            <version>4.3.2.RELEASE</version>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-tx</artifactId>
            <version>4.3.8.RELEASE</version>
        </dependency>

            <!-- https://mvnrepository.com/artifact/commons-fileupload/commons-fileupload -->
    <dependency>
        <groupId>commons-fileupload</groupId>
        <artifactId>commons-fileupload</artifactId>
        <version>1.2.1</version>
    </dependency>
    <!-- https://mvnrepository.com/artifact/commons-io/commons-io -->
    <dependency>
        <groupId>commons-io</groupId>
        <artifactId>commons-io</artifactId>
        <version>2.4</version>
    </dependency>

    <dependency>
    <groupId>com.google.code.gson</groupId>
    <artifactId>gson</artifactId>
    <version>2.8.1</version>
</dependency>


<!-- swagger -->
    <dependency>
            <groupId>com.fasterxml.jackson.core</groupId>
            <artifactId>jackson-core</artifactId>
            <version>${swaggerVersion}</version>
        </dependency>
        <dependency>
            <groupId>com.fasterxml.jackson.core</groupId>
            <artifactId>jackson-annotations</artifactId>
            <version>${swaggerVersion}</version>
        </dependency>
        <dependency>
            <groupId>com.fasterxml.jackson.core</groupId>
            <artifactId>jackson-databind</artifactId>
            <version>${swaggerVersion}</version>
        </dependency>

        <dependency>
            <groupId>io.springfox</groupId>
            <artifactId>springfox-swagger2</artifactId>
            <version>2.7.0</version>
        </dependency>
        <dependency>
            <groupId>io.springfox</groupId>
            <artifactId>springfox-swagger-ui</artifactId>
            <version>2.7.0</version>
        </dependency>

        <dependency>
            <groupId>jetty</groupId>
            <artifactId>servlet-api</artifactId>
            <version>2.5-6.0.2</version>
        </dependency>

  </dependencies>

    <build>
        <plugins>
            <plugin>
                <groupId>org.eclipse.jetty</groupId>
                <artifactId>jetty-maven-plugin</artifactId>
                <version>${jettyVersion}</version>

            </plugin>
        </plugins>

    </build>
</project>

mvc:

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:context="http://www.springframework.org/schema/context"
    xmlns:mvc="http://www.springframework.org/schema/mvc"
    xsi:schemaLocation="
        http://www.springframework.org/schema/beans
        http://www.springframework.org/schema/beans/spring-beans.xsd
        http://www.springframework.org/schema/context 
        http://www.springframework.org/schema/context/spring-context.xsd
        http://www.springframework.org/schema/mvc
        http://www.springframework.org/schema/mvc/spring-mvc.xsd">


    <!-- 把标记了@Controller注解的类转换为bean -->
    <context:component-scan base-package="controller" />



    <!-- 启动Spring MVC的注解功能,完成请求和注解POJO的映射 -->
    <bean
        class="org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter" />


    <mvc:default-servlet-handler />
    <mvc:resources mapping="/webjars/**" location="classpath:/META-INF/resources/webjars/" />
    <mvc:resources mapping="swagger-ui.html" location="classpath:/META-INF/resources/" />
    <mvc:resources mapping="/pic/**" location="/pic/" />
    <mvc:resources location="/" mapping="/*.html" />
    <!-- WEB-INF是Java的WEB应用的安全目录。所谓安全就是客户端无法访问,只有服务端可以访问的目录。 如果想在页面中直接访问其中的文件,必须通过web.xml文件对要访问的文件进行相应映射才能访问。 -->
    <mvc:annotation-driven />

</beans>  

spring context:

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:context="http://www.springframework.org/schema/context"
    xmlns:mvc="http://www.springframework.org/schema/mvc"
    xsi:schemaLocation="
        http://www.springframework.org/schema/beans
        http://www.springframework.org/schema/beans/spring-beans.xsd
        http://www.springframework.org/schema/context 
        http://www.springframework.org/schema/context/spring-context.xsd
        http://www.springframework.org/schema/mvc
        http://www.springframework.org/schema/mvc/spring-mvc.xsd">


    <!-- 扫描注解的包 -->
    <context:component-scan base-package="controller" />
    <context:component-scan base-package="util" />
    <context:component-scan base-package="service" />
    <context:component-scan base-package="service.impl" />



</beans>

springmvc+jetty+swagger简单配置完成

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值