SpringBoot集成Swagger2实现Restful(类型转换错误解决办法)
程序员文章站
2023-12-13 15:47:22
pom.xml增加依赖包
io.springfox ...
pom.xml增加依赖包
<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>
编写swapper2配置类
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中试用swagger2构建的restful apis") .description("更多spring boot相关文章请关注:http://blog.didispace.com/") .termsofserviceurl("http://blog.didispace.com/") .contact("leo") .version("1.0") .build(); } }
controller内使用
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="获取用户列表",notes="") @requestmapping(value={""},method=requestmethod.get) public list<user> getuserlist(){ list<user> r=new arraylist<user>(users.values()); return r; } @apioperation(value="创建用户", notes="根据user对象创建用户") @apiimplicitparam(name = "user", value = "用户详细实体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="获取用户详细信息", notes="根据url的id来获取用户详细信息") @apiimplicitparam(name = "id", value = "用户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="更新用户详细信息", notes="根据url的id来指定更新对象,并根据传过来的user信息来更新用户详细信息") @apiimplicitparams({ @apiimplicitparam(name = "id", value = "用户id", required = true, paramtype="path", datatype = "long"), @apiimplicitparam(name = "user", value = "用户详细实体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="删除用户", notes="根据url的id来指定删除对象") @apiimplicitparam(name = "id", value = "用户id", required = true, datatype = "long") @requestmapping(value="/{id}", method=requestmethod.delete) public string deleteuser(@pathvariable long id) { users.remove(id); return "success"; } }
如果上诉代码没有写paramtype = “path”
会提示类型转换string convert to long
错误。
以上所述是小编给大家介绍的springboot集成swagger2实现restful(类型转换错误解决办法),希望对大家有所帮助