有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

java如何在swaggerui中获取POST数据?

我已经在SpringBoot应用程序中配置了swagger ui

下面是我的代码

public class Application extends SpringBootServletInitializer {

public static void main(String[] args) {
    SpringApplication.run(Application.class, args);
    openAppUrl("8080/swagger-ui.html");
}

public static void openAppUrl(String port) {
    String url = "http://localhost:" + port;
    String os = System.getProperty("os.name").toLowerCase();
    Runtime rt = Runtime.getRuntime();

    try {

        if (os.indexOf("win") >= 0) {
            rt.exec("rundll32 url.dll,FileProtocolHandler " + url);
        } else if (os.indexOf("mac") >= 0) {
            try {
                rt.exec("open " + url);
            } catch (IOException e) {
                System.out.println(e);
            }
        } else if (os.indexOf("nix") >= 0 || os.indexOf("nux") >= 0) {

            String[] browsers = { "epiphany", "firefox", "mozilla", "konqueror", "netscape", "opera", "links",
                    "lynx" };
            StringBuffer cmd = new StringBuffer();
            for (int i = 0; i < browsers.length; i++)
                cmd.append((i == 0 ? "" : " || ") + browsers[i] + " \"" + url + "\" ");

            rt.exec(new String[] { "sh", "-c", cmd.toString() });

        } else {
            return;
        }
    } catch (Exception e) {
        return;
    }
    return;
}
}

我的控制器

package com. server.spring.controller;

    import java.util.List;

    import org.springframework.beans.factory.annotation.Autowired;
    import org.springframework.http.MediaType;
    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. server.spring.domain.User;
    import com. server.spring.service.UserService;


    @RestController
    @RequestMapping(UsersController.ROOT_RESOURCE_PATH)
    public class UsersController {

        public static final String ROOT_RESOURCE_PATH = "/rest/secure/v1/users";

        @Autowired
        private UserService userService;


        @RequestMapping(value = "/list", method = RequestMethod.GET, produces = MediaType.APPLICATION_JSON_VALUE)
        public List<User> getUsers() {
            return userService.getAllUsers();
        }

        @RequestMapping(value = "/create", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_VALUE, consumes = MediaType.APPLICATION_JSON_VALUE)
        public User createUser(@RequestBody User user) {
            return userService.saveUser(user);
        }
    }

enter image description here

这里显示了RESTAPI URL、HTTP方法POST、响应JSON对象。但是我没有看到API调用中预期的POST数据Obj。因此,没有这一点,前端开发人员就无法使用相应的API

所以我希望向POST Data JSON对象展示RESTAPI对前端应用程序的要求

这是正确的方法还是我需要修改它以获得预期的方法


共 (2) 个答案

  1. # 1 楼答案

    通过在模型上使用示例(技术上是API模型属性),我获得了正确附加到主体的模型属性。然而,我在这方面的经验不包括Spring,我确信文档是类似的。您需要将@ApiModelProperty注释添加到User类中

    public class User {
      private String firstName;
      private String lastName;
    
      @ApiModelProperty(value = "User's first name.", example = "John")
      public String getFirstName(){}
      //...setters
    
      @ApiModelProperty(value = "User's last name.", example = "Smith")
      public String getLastName(){}
      //...setters
    
      // etc
    }
    

    这将用example字符串填充swagger文档POST/PUT/etc正文

  2. # 2 楼答案

    您使用的是一个旧版本的Swagger ui,看起来像2。x
    最新版本更容易理解,请在此处查看:
    http://petstore.swagger.io/#/pet/addPet
    这听起来像是你给了其他开发人员(前端开发人员),在这种情况下,我强烈建议你寻找升级的方法,新版本有更好的用户体验,也是2。不再支持x UI版本

    所以要回答你的问题:

    How can I get POST data in swagger-ui?

    您可以通过Swigger ui的[试用]按钮获得实际响应
    API所期望的POST数据Obj就是您在示例中看到的