最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

SpringBoot集成Swagger2實(shí)現(xiàn)Restful(類型轉(zhuǎn)換錯誤解決辦法)

 更新時間:2017年07月05日 14:33:15   作者:那小子很拽  
這篇文章主要介紹了SpringBoot集成Swagger2實(shí)現(xiàn)Restful(類型轉(zhuǎn)換錯誤解決辦法),需要的朋友可以參考下

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構(gòu)建的RESTful APIs")
        .description("更多Spring Boot相關(guān)文章請關(guān)注:http://blog.didispace.com/")
        .termsOfServiceUrl("http://blog.didispace.com/")
        .contact("leo")
        .version("1.0")
        .build();
  }
}

Controller內(nèi)使用

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="創(chuàng)建用戶", notes="根據(jù)User對象創(chuàng)建用戶")
    @ApiImplicitParam(name = "user", value = "用戶詳細(xì)實(shí)體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="獲取用戶詳細(xì)信息", notes="根據(jù)url的id來獲取用戶詳細(xì)信息")
    @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="更新用戶詳細(xì)信息", notes="根據(jù)url的id來指定更新對象,并根據(jù)傳過來的user信息來更新用戶詳細(xì)信息")
    @ApiImplicitParams({
        @ApiImplicitParam(name = "id", value = "用戶ID", required = true, paramType="path", dataType = "Long"),
        @ApiImplicitParam(name = "user", value = "用戶詳細(xì)實(shí)體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="根據(jù)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” 會提示類型轉(zhuǎn)換String convert to Long錯誤。

以上所述是小編給大家介紹的SpringBoot集成Swagger2實(shí)現(xiàn)Restful(類型轉(zhuǎn)換錯誤解決辦法),希望對大家有所幫助,如果大家有任何疑問請給我留言,小編會及時回復(fù)大家的。在此也非常感謝大家對腳本之家網(wǎng)站的支持!

相關(guān)文章

  • java匿名內(nèi)部類實(shí)例簡析

    java匿名內(nèi)部類實(shí)例簡析

    匿名類是不能有名稱的類,所以沒辦法引用它們,必須在創(chuàng)建時,作為new語句的一部分來聲明它們,需要了解更多的可以參考本文
    2012-11-11
  • 如何用注解的方式實(shí)現(xiàn)Mybatis插入數(shù)據(jù)時返回自增的主鍵Id

    如何用注解的方式實(shí)現(xiàn)Mybatis插入數(shù)據(jù)時返回自增的主鍵Id

    這篇文章主要介紹了如何用注解的方式實(shí)現(xiàn)Mybatis插入數(shù)據(jù)時返回自增的主鍵Id,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教
    2022-07-07
  • java Lombok之@Accessors用法及說明

    java Lombok之@Accessors用法及說明

    這篇文章主要介紹了java Lombok之@Accessors用法及說明,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教
    2023-03-03
  • SpringBoot2 整合FreeMarker實(shí)現(xiàn)頁面靜態(tài)化示例詳解

    SpringBoot2 整合FreeMarker實(shí)現(xiàn)頁面靜態(tài)化示例詳解

    這篇文章主要介紹了SpringBoot2 整合FreeMarker實(shí)現(xiàn)頁面靜態(tài)化示例詳解,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2020-07-07
  • SpringBoot配置線程池的實(shí)現(xiàn)示例

    SpringBoot配置線程池的實(shí)現(xiàn)示例

    本文主要介紹了SpringBoot配置線程池的實(shí)現(xiàn)示例,主要包括在Spring Boot中創(chuàng)建和配置線程池,包括設(shè)置線程池的大小、隊(duì)列容量、線程名稱等參數(shù),感興趣的可以了解一下
    2023-09-09
  • SpringBoot 使用jwt進(jìn)行身份驗(yàn)證的方法示例

    SpringBoot 使用jwt進(jìn)行身份驗(yàn)證的方法示例

    這篇文章主要介紹了SpringBoot 使用jwt進(jìn)行身份驗(yàn)證的方法示例,小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧
    2018-12-12
  • 基于SpringBoot的SSMP的整合案例

    基于SpringBoot的SSMP的整合案例

    這篇文章主要介紹了SpringBoot整合SSMP的詳細(xì)教程,文中通過代碼示例介紹的非常詳細(xì),需要的朋友可以參考下
    2023-05-05
  • Spring?Security中使用authorizeRequests遇到的問題小結(jié)

    Spring?Security中使用authorizeRequests遇到的問題小結(jié)

    Spring?是非常流行和成功的?Java?應(yīng)用開發(fā)框架,Spring?Security?正是?Spring?家族中的成員,這篇文章主要介紹了Spring?Security中使用authorizeRequests遇到的問題,需要的朋友可以參考下
    2023-02-02
  • 解析Java中的默認(rèn)方法

    解析Java中的默認(rèn)方法

    這篇文章主要介紹了Java中的默認(rèn)方法,包括繼承和調(diào)用等Java入門學(xué)習(xí)中的基礎(chǔ)知識,需要的朋友可以參考下
    2015-07-07
  • 使用Java實(shí)現(xiàn)獲取文件MD5值工具類

    使用Java實(shí)現(xiàn)獲取文件MD5值工具類

    我們在工作中通常使用MD5對文件進(jìn)行校驗(yàn)完整性,比較,提高安全性等,這篇文章主要為大家詳細(xì)介紹了Java如何編寫一個實(shí)現(xiàn)獲取文件MD5值的工具,需要的可以參考下
    2023-12-12

最新評論

铁力市| 满洲里市| 吴旗县| 永仁县| 禄丰县| 榆中县| 巢湖市| 灵璧县| 蚌埠市| 磐石市| 香河县| 莱芜市| 阳山县| 白玉县| 商水县| 栾城县| 渭源县| 炉霍县| 青冈县| 阜城县| 昭觉县| 巩义市| 栾城县| 常德市| 纳雍县| 兰溪市| 房产| 施甸县| 灌阳县| 尤溪县| 奇台县| 远安县| 九龙城区| 剑阁县| 龙川县| 神农架林区| 龙门县| 武鸣县| 克什克腾旗| 都兰县| 井研县|