gpt4 book ai didi

java - Swagger Dropwizard 0.7 - 不显示 JSON 参数的 TextArea

转载 作者:行者123 更新时间:2023-11-30 11:02:27 26 4
gpt4 key购买 nike

问题

我找不到 Swagger 不显示带有可用文本区域“正文”的 POST 端点的原因,因此我可以将 JSON 粘贴到其中。

我希望在 PetStore Swagger 的 POST 中看到这样的页面

但我的表单只是在我点击“试用”后发布,我得到了

响应正文

    <html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8"/>
<title>Error 415 Unsupported Media Type</title>
</head>
<body><h2>HTTP ERROR 415</h2>
<p>Problem accessing /promotions. Reason:
<pre> Unsupported Media Type</pre></p><hr><i><small>Powered by Jetty://</small></i><hr/>
</body>
</html>

响应头

{
"access-control-allow-origin": "http://localhost:8080",
"date": "Thu, 11 Jun 2015 07:37:15 GMT",
"cache-control": "must-revalidate,no-cache,no-store",
"access-control-allow-credentials": "true",
"content-type": "text/html; charset=ISO-8859-1",
"content-length": "320",
"access-control-expose-headers": ""
}

你能帮我解决这个问题吗?

关于我的项目的更多信息

$ java -version
java version "1.8.0_45"
Java(TM) SE Runtime Environment (build 1.8.0_45-b14)
Java HotSpot(TM) 64-Bit Server VM (build 25.45-b02, mixed mode)

行家

parent
- pom.xml: <dropwizard.version>0.8.1</dropwizard.version>
<swagger.version>0.7.0</swagger.version>)
app
- pom.xml: <dependency>
<groupId>io.dropwizard</groupId>
<artifactId>dropwizard-core</artifactId>
<version>${dropwizard.version}</version>
</dependency>
<dependency>
<groupId>io.federecio</groupId>
<artifactId>dropwizard-swagger</artifactId>
<version>${swagger.version}</version>
</dependency>
- config.yml:
swagger:
resourcePackage: myproject.promotion.v1.resource
representation
- pom.xml

配置

package myproject.promotion.app.config;

public class PromotionServiceConfiguration extends Configuration {

@JsonProperty("swagger")
public SwaggerBundleConfiguration swaggerBundleConfiguration;

申请

package myproject.promotion.app;

public class PromotionServiceApplication extends Application<PromotionServiceConfiguration> {

public static void main(String[] args) throws Exception {
new PromotionServiceApplication().run(args);
}

@Override
public void initialize(Bootstrap<PromotionServiceConfiguration> bootstrap) {
bootstrap.addBundle(new PromotionSwaggerBundle());
}

@Override
public void run(PromotionServiceConfiguration configuration, Environment environment) {
//Deleted to make it short
}

PromotionSwaggerBundle

package myproject.promotion.app.config;

public class PromotionSwaggerBundle extends SwaggerBundle<PromotionServiceConfiguration> {

@Override
protected SwaggerBundleConfiguration getSwaggerBundleConfiguration(PromotionServiceConfiguration configuration) {
return configuration.swaggerBundleConfiguration;
}

终点

package myproject.v1.resource;

@Path("/promotions")
@Api(value = "/promotions/", description = "Promotions' possible operations", consumes = "application/json", produces = "application/json")
public class PromotionManagementResource {

private static final String PROMO_PARAM = "promotionId";

private final PromotionManagementService promotionManagementService;

@Inject
public PromotionManagementResource(PromotionManagementService promotionManagementService) {
this.promotionManagementService = promotionManagementService;
}

@POST
@Consumes(APPLICATION_JSON)
@ApiOperation(value = "Create promotion")
@ApiResponses(value = {
@ApiResponse(code = 201, message = "Promotion created. Link to it in Location HEADER"),
@ApiResponse(code = 409, message = "Promotion already exists")
})
public Response create(final Promotion promotion, @Context final UriInfo uriInfo) throws IOException {
Promotion createdPromotion = promotionManagementService.create(promotion);

URI createdInventoryURI = inventory(createdPromotion, uriInfo);
return Response.created(createdInventoryURI).build();
}

最佳答案

最后注释@ApiParam成功了。

新的 POST 方法(带有新的注解)

@POST
@Consumes(APPLICATION_JSON)
@ApiOperation(value = "Create promotion", notes = "", response = Promotion.class)
@ApiResponses(value = {
@ApiResponse(code = 201, message = "Promotion created. Link to it in Location HEADER."),
@ApiResponse(code = 409, message = "Promotion already exist.")
})
public Response create(@ApiParam final Promotion promotion, @Context final UriInfo uriInfo) throws IOException {
Promotion createdPromotion = promotionManagementService.create(promotion);

URI createdPromotionURI = uriTo(createdPromotion, uriInfo);
return Response.created(createdPromotionURI).build();
}

感谢 cyrbil 的帮助。

关于java - Swagger Dropwizard 0.7 - 不显示 JSON 参数的 TextArea,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30766313/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com