Java HttpResponse.notFound在Micronaut中显示错误消息
我有一个简单的RESTAPI,它检查数据库中是否存在类别id,如下所示Java HttpResponse.notFound在Micronaut中显示错误消息,java,micronaut,micronaut-client,Java,Micronaut,Micronaut Client,我有一个简单的RESTAPI,它检查数据库中是否存在类别id,如下所示 @Controller("/sub-category") public class SubCategoryController implements ISubCategoryOperation { @Post public Maybe<HttpResponse> post(@Valid SubCategoryViewModel categoryViewModel) {
@Controller("/sub-category")
public class SubCategoryController implements ISubCategoryOperation
{
@Post
public Maybe<HttpResponse> post(@Valid SubCategoryViewModel categoryViewModel) {
LOG.info(String.format("Controller --> Updating the specified category"));
return iCategoryManager.Count(categoryViewModel.categoryId()).flatMap(item -> {
if (item > 0) {
iSubCategoryManager.Create(categoryViewModel);
return Maybe.just(HttpResponse.created(categoryViewModel));
} else
return Maybe.just(HttpResponse.notFound("Category id not found"));
});
}
}
您的请求是否命中了
return Maybe.just(HttpResponse.notFound(“Category id not found”)代码>或者你是因为不同的原因得到404?你是否用@controller(“/api/v1/sub-category”)注释了你的控制器?是的,它是用@controller(“/api/v1/sub-category”)注释的@controller@JeffScottBrown它将到达该代码并返回值,但无法看到来自客户端的值。正如@AmitB10所说,您的控制器被注释为“/子类别”,但邮递员屏幕截图显示“/api/v1/sub-category”。如果没有更多的源代码,就不清楚您是否在application.yaml
中将web根目录配置为不同的,或者您是否从另一个微服务调用此控制器,并且微服务正在使用“未找到页面”处理HTTP/404。我能够使用您的代码获得所需的响应(必须进行更改),因此需要更多的代码或复制程序才能更好地理解
@Controller("/api/${api.version:v1}/sub-category")
public class ApiGatewaySubCategoryController implements ISubCategoryOperation{
private final ISubCategoryClient iSubCategoryClient;
public ApiGatewaySubCategoryController(ISubCategoryClient iSubCategoryClient) {
this.iSubCategoryClient = iSubCategoryClient;
}
@Override
public Maybe<HttpResponse<?>> post(@NotBlank String id, @Valid SubCategoryViewModel model) {
return this.iSubCategoryClient.post(id, model);
}
}
@Client(id="feteBirdProduct", path = "/sub-category")
public interface ISubCategoryClient extends ISubCategoryOperation{
}