SpringBoot integrated Swagger2 to implement Restful of type conversion error resolution
- 2020-08-22 22:05:25
- OfStack
pom.xml adds dependency packages
<dependency>
<groupId>io.springfox</groupId>
<artifactId>springfox-swagger2</artifactId>
<version>2.2.2</version>
</dependency>
<dependency>
<groupId>io.springfox</groupId>
<artifactId>springfox-swagger-ui</artifactId>
<version>2.2.2</version>
</dependency>
Write the swapper2 configuration class
package com.zyank;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import springfox.documentation.builders.ApiInfoBuilder;
import springfox.documentation.builders.PathSelectors;
import springfox.documentation.builders.RequestHandlerSelectors;
import springfox.documentation.service.ApiInfo;
import springfox.documentation.spi.DocumentationType;
import springfox.documentation.spring.web.plugins.Docket;
import springfox.documentation.swagger2.annotations.EnableSwagger2;
@Configuration
@EnableSwagger2
public class Swagger2 {
@Bean
public Docket createRestApi() {
return new Docket(DocumentationType.SWAGGER_2)
.apiInfo(apiInfo())
.select()
.apis(RequestHandlerSelectors.basePackage("com.zyank.web"))
.paths(PathSelectors.any())
.build();
}
private ApiInfo apiInfo(){
return new ApiInfoBuilder()
.title("Spring Boot In the trial Swagger2 To build the RESTful APIs")
.description(" More and more Spring Boot Please pay attention to related articles: http://blog.didispace.com/")
.termsOfServiceUrl("http://blog.didispace.com/")
.contact("leo")
.version("1.0")
.build();
}
}
Controller used within
package com.zyank.web;
import java.util.ArrayList;
import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;
import com.zyank.domain.User;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
@RestController
@RequestMapping(value="/users")
public class UserContrller {
static Map<Long, User> users=Collections.synchronizedMap(new HashMap<Long,User>());
@ApiOperation(value=" Getting a list of users ",notes="")
@RequestMapping(value={""},method=RequestMethod.GET)
public List<User> getUserList(){
List<User> r=new ArrayList<User>(users.values());
return r;
}
@ApiOperation(value=" Create a user ", notes=" According to the User Object creation user ")
@ApiImplicitParam(name = "user", value = " User detail entity user", required = true, dataType = "User")
@RequestMapping(value="", method=RequestMethod.POST)
public String postUser(@RequestBody User user) {
users.put(user.getId(), user);
return "success";
}
@ApiOperation(value=" Get user details ", notes=" According to the url the id To get user details ")
@ApiImplicitParam(name = "id", value = " The user ID", required = true, paramType="path", dataType = "Long")
@RequestMapping(value="/{id}", method=RequestMethod.GET)
public User getUser(@PathVariable Long id) {
return users.get(id);
}
@ApiOperation(value=" Update user details ", notes=" According to the url the id To specify the update object, and based on the user Information to update user details ")
@ApiImplicitParams({
@ApiImplicitParam(name = "id", value = " The user ID", required = true, paramType="path", dataType = "Long"),
@ApiImplicitParam(name = "user", value = " User detail entity user", required = true, dataType = "User")
})
@RequestMapping(value="/{id}", method=RequestMethod.PUT)
public String putUser(@PathVariable Long id, @RequestBody User user) {
User u = users.get(id);
u.setName(user.getName());
u.setAge(user.getAge());
users.put(id, u);
return "success";
}
@ApiOperation(value=" Delete user ", notes=" According to the url the id To specify the delete object ")
@ApiImplicitParam(name = "id", value = " The user ID", required = true, dataType = "Long")
@RequestMapping(value="/{id}", method=RequestMethod.DELETE)
public String deleteUser(@PathVariable Long id) {
users.remove(id);
return "success";
}
}
If the appeal code is not written
paramType = “path”
A type cast is prompted
String convert to Long
Error.