swagger实现api接口可视化

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/qq_16855077/article/details/83820885

1.pom.xml

 <properties>
		<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
		<spring.framework.version>4.3.6.RELEASE</spring.framework.version>
	</properties>

	<dependencies>
		<!--引入Spring依赖包 -->
		<dependency>
			<groupId>org.springframework</groupId>
			<artifactId>spring-core</artifactId>
			<version>${spring.framework.version}</version>
		</dependency>
		<dependency>
			<groupId>org.springframework</groupId>
			<artifactId>spring-context</artifactId>
			<version>${spring.framework.version}</version>
		</dependency>
		<dependency>
			<groupId>org.springframework</groupId>
			<artifactId>spring-webmvc</artifactId>
			<version>${spring.framework.version}</version>
		</dependency>
		
		<!-- swagger -->  
		<dependency>    
		    <groupId>com.mangofactory</groupId>    
		    <artifactId>swagger-springmvc</artifactId>    
		    <version>1.0.2</version>    
		</dependency>  
		<dependency>  
		    <groupId>com.fasterxml.jackson.core</groupId>  
		    <artifactId>jackson-core</artifactId>  
		    <version>2.5.1</version>  
		</dependency>  
		<dependency>  
		    <groupId>com.fasterxml.jackson.core</groupId>  
		    <artifactId>jackson-databind</artifactId>  
		    <version>2.5.1</version>  
		</dependency>  
		<dependency>  
		    <groupId>com.fasterxml.jackson.core</groupId>  
		    <artifactId>jackson-annotations</artifactId>  
		    <version>2.5.1</version>  
		</dependency>   
	</dependencies>
	
	<build>
		<finalName>springfox-swagger-demo</finalName>
	</build>

2.spring-mvc.xml

<?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" xmlns:p="http://www.springframework.org/schema/p"
	xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.0.xsd
		http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.0.xsd
		http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-4.0.xsd">

	<!-- 默认的注解映射的支持 ,它会自动注册DefaultAnnotationHandlerMapping 与AnnotationMethodHandlerAdapter -->
	<mvc:annotation-driven />

	<!-- enable autowire 向容器自动注册 -->
	<context:annotation-config />

	<!-- 设置使用注解的类所在的jar包 -->
	<context:component-scan base-package="com.parwa.test" />
	<bean
		class="org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter" />

</beans>

3.web.xml

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
	xmlns="http://java.sun.com/xml/ns/javaee"
	xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd"
	id="WebApp_ID" metadata-complete="true" version="3.0">
	<display-name>Spring MVC</display-name>
	<servlet>
		<servlet-name>spring-mvc</servlet-name>
		<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
		<init-param>
			<param-name>contextConfigLocation</param-name>
			<param-value>classpath:spring-mvc.xml</param-value>
		</init-param>
		<load-on-startup>1</load-on-startup>
		<async-supported>true</async-supported>
	</servlet>
	<servlet-mapping>
		<servlet-name>spring-mvc</servlet-name>
		<url-pattern>/</url-pattern>
	</servlet-mapping>
	<filter>
		<filter-name>encodingFilter</filter-name>
		<filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class>
		<init-param>
			<param-name>encoding</param-name>
			<param-value>UTF-8</param-value>
		</init-param>
		<init-param>
			<param-name>forceEncoding</param-name>
			<param-value>true</param-value>
		</init-param>
	</filter>
	<filter-mapping>
		<filter-name>encodingFilter</filter-name>
		<url-pattern>/*</url-pattern>
	</filter-mapping>
</web-app>

4.controller类

package com.parwa.test.controll;

import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import com.parwa.test.entity.Product;


@RestController
@RequestMapping(value = { "/api/product/"})
public class ProductController {

	@RequestMapping(value = "/{id}", method = RequestMethod.GET)
	public ResponseEntity<Product> get(@PathVariable Long id) {
		Product product = new Product();
		product.setName("测试spring mvc是否搭建完成1!");
		product.setId(1L);
		product.setProductClass("seven_filters");
		product.setProductId("T12345");
		return ResponseEntity.ok(product);
	}
}

5.实体类

package com.parwa.test.entity;

import java.io.Serializable;

/**
 * 
 * @author wude
 * @date 2018��5��11��
 * @version 2.0
 */
public class Product implements Serializable {

	private static final long serialVersionUID = 1L;

	/**ID*/
	private Long id;

	/**��Ʒ����*/
	private String name;

	/**��Ʒ�ͺ�*/
	private String productClass;

	/**��ƷID*/
	private String productId;

	/**
	 * @return the id
	 */
	public Long getId() {
		return id;
	}

	/**
	 * @param id
	 *            the id to set
	 */
	public void setId(Long id) {
		this.id = id;
	}

	/**
	 * @return the name
	 */
	public String getName() {
		return name;
	}

	/**
	 * @param name
	 *            the name to set
	 */
	public void setName(String name) {
		this.name = name;
	}

	/**
	 * @return the productClass
	 */
	public String getProductClass() {
		return productClass;
	}

	/**
	 * @param productClass
	 *            the productClass to set
	 */
	public void setProductClass(String productClass) {
		this.productClass = productClass;
	}

	/**
	 * @return the productId
	 */
	public String getProductId() {
		return productId;
	}

	/**
	 * @param productId
	 *            the productId to set
	 */
	public void setProductId(String productId) {
		this.productId = productId;
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see java.lang.Object#toString()
	 */
	@Override
	public String toString() {
		return "Product [id=" + id + ", name=" + name + ", productClass=" + productClass + ", productId=" + productId
				+ "]";
	}
}

 6.访问地址

http://localhost:6080/test/api/product/2

猜你喜欢

转载自blog.csdn.net/qq_16855077/article/details/83820885