springswagger:在post方法的@requestbody中传递实体时,swagger无法识别控制器

5sxhfpxr  于 2021-07-03  发布在  Java
关注(0)|答案(0)|浏览(345)

我使用的是swagger2,下面是我在pom.xml中添加的依赖项。
java版本1.8
pom.xml文件

<dependency>
            <groupId>io.springfox</groupId>
            <artifactId>springfox-swagger2</artifactId>
            <version>2.9.2</version>
        </dependency>
<dependency>
    <groupId>io.springfox</groupId>
    <artifactId>springfox-swagger-ui</artifactId>
    <version>2.9.2</version>
</dependency>

我的申请代码是:(我试过)

@SpringBootApplication(exclude = { org.springframework.boot.autoconfigure.gson.GsonAutoConfiguration.class })
@EnableJpaAuditing(auditorAwareRef = "auditorProvider")
@EnableScheduling
@EnableConfigurationProperties
@EnableSwagger2
public class MyApplication extends SpringBootServletInitializer
{

    public static void main(String[] args)
    {
        SpringApplication.run(MyApplication.class, args);
    }

    private static Class<MyApplication> applicationClass = MyApplication.class;

    @Override
    protected SpringApplicationBuilder configure(SpringApplicationBuilder application)
    {
        return application.sources(applicationClass);
    }

    @Bean
    AuditorAware<Long> auditorProvider()
    {
        return new AuditorAwareImpl();
    }

    @Bean
      public Docket productApi() {
        System.out.println("api conf:::");

        return new Docket(DocumentationType.SWAGGER_2).select()
        //    .apis(RequestHandlerSelectors.basePackage("com.package.controller.*"))
            .apis(RequestHandlerSelectors.any())
            .apis(RequestHandlerSelectors.withClassAnnotation(Api.class))
                .paths(PathSelectors.any()).build();
      }
    }

当添加控制器test1.java时,swaggerui工作得很好

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

import io.swagger.annotations.Api;

@RestController
@Api
public class Test1 {

    @RequestMapping(value = "/checkdomain", method = RequestMethod.GET)
    public ResponseEntity<?> checkUniqueItemNumber(@RequestHeader(value = "Origin") String origin) {
        return new ResponseEntity<>(origin, HttpStatus.OK);
    }

}

output:swagger_ui 输出图像
当我在控制器i,e test2.java中添加实体请求体(testentity.java)时,应用程序失败

import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestHeader;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import io.swagger.annotations.Api;

@RestController
@Api
public class Test2 {

    @RequestMapping(value = "/check", method = RequestMethod.POST)
    public ResponseEntity<?> checkUniqueItemNumber2(@RequestBody TestEntity origin) {
        return new ResponseEntity<>(origin, HttpStatus.OK);
    }

}

测试实体.java

import java.io.Serializable;

import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.EntityListeners;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
import javax.persistence.Table;

import org.springframework.data.jpa.domain.support.AuditingEntityListener;

import com.fasterxml.jackson.annotation.JsonView;

@Entity
@EntityListeners(AuditingEntityListener.class)
@Table(name = "TestEntity")
public class TestEntity implements Serializable {

    /**
     * 
     */
    private static final long serialVersionUID = 1L;

    @Id
    @GeneratedValue(strategy = javax.persistence.GenerationType.IDENTITY)
    @Column(name = "RecordID")
    private Integer recordID;

    @Column(name="Url")
    private String url;

// generate getter and setter

}

我得到以下输出,应用程序运行失败

我认为它不能启动tomcat或者不能提供任何登录控制台。
谢谢你,任何帮助都将不胜感激。
经过研究我发现,当实体包含任何多对一或一对一关系时,应用程序就会失败。

暂无答案!

目前还没有任何答案,快来回答吧!

相关问题