Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/390.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/svg/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 使用json和json验证的Swagger API_Java_Json_Rest_Swagger - Fatal编程技术网

Java 使用json和json验证的Swagger API

Java 使用json和json验证的Swagger API,java,json,rest,swagger,Java,Json,Rest,Swagger,我正在使用Swagger生成Restful API: @POST @Consumes({ "application/json" }) @Produces({ "application/json" }) @io.swagger.annotations.ApiOperation(value = "Create a task", notes = "", response = Tasks.class) @io.swagger.annotations.ApiResponses(value = {

我正在使用Swagger生成Restful API:

@POST
@Consumes({ "application/json" })
@Produces({ "application/json" })
@io.swagger.annotations.ApiOperation(value = "Create a task", notes = "", response = Tasks.class)
@io.swagger.annotations.ApiResponses(value = { 
    @io.swagger.annotations.ApiResponse(code = 200, message = "created task", response = Tasks.class),
    @io.swagger.annotations.ApiResponse(code = 404, message = "task not found", response = Tasks.class),
    @io.swagger.annotations.ApiResponse(code = 200, message = "unexpected error", response = Tasks.class) })
public Response createTask(@ApiParam(value = "The task to create" ,required=true ) NewTask newTask)
throws NotFoundException {
    return delegate.createTask(newTask);
}
此API接受json字符串并从中生成java对象。除了一个例外,这种方法运行得很好: API接受任何正确格式的json字符串,但忽略json的内容,这意味着我得到一个使用默认值创建的对象

所以我的问题是:
如何在生成实际java对象之前验证传入的jsonstring(针对json模式)?

由于您不希望在方法中接收json字符串,并且该字符串不是验证的选项,因此可以尝试以下方法

JAX-RS用于解析传入请求。既然你想要非常具体的东西,考虑一下。 请参见以下示例:

@Provider
@Produces("application/json")
public class CustomMessageBodyWriter implements MessageBodyWriter<Object> {

    @Override
    public boolean isWriteable(Class<?> type, Type genericType,
                               Annotation[] annotations, MediaType mediaType) {
        return true;
    }

    @Override
    public long getSize(MyBean myBean, Class<?> type, Type genericType,
                        Annotation[] annotations, MediaType mediaType) {

        // Deprecated by JAX-RS 2.0
        return 0;
    }

    @Override
    public void writeTo(Object object, Class<?> type, Type genericType,
                        Annotation[] annotations, MediaType mediaType,
                        MultivaluedMap<String, Object> httpHeaders,
                        OutputStream entityStream) throws IOException {

        // Read the entityStream
        // Perform the validation against your schema
        // Write to the object
    }
}
@Provider
@生成(“应用程序/json”)
公共类CustomMessageBodyWriter实现MessageBodyWriter{
@凌驾
公共布尔值可写(类类型、类型genericType、,
注释[]注释,MediaType(MediaType){
返回true;
}
@凌驾
公共长getSize(MyBean、MyBean、类类型、类型genericType、,
注释[]注释,MediaType(MediaType){
//被JAX-RS 2.0弃用
返回0;
}
@凌驾
public void writeTo(对象、类类型、类型genericType、,
注释[]注释,MediaType MediaType,
多值MAP HttpHeader,
OutputStream entityStream)引发IOException{
//阅读entityStream
//对您的模式执行验证
//写入对象
}
}
注释使JAX-RS运行时能够在提供者扫描阶段自动发现该类。

有一种方法可以为各种框架提供多个适配器,例如:

它能够根据Swagger/OpenAPI 2或OpenAPI 3方案验证请求和/或响应


请参阅此处:获取更详细的答案。

如何对架构执行验证?您可以将JSON作为字符串接收,验证它,然后使用创建一个对象,例如。但是,由于您正在执行验证,您考虑过吗?问题是,我必须更改API以接收json字符串,而不是像
public Response createTask(string jsonTask)这样的对象
在methods parameters afaik中,但如果可能的话,我更愿意将对象放在那里。你可以…一个问题:我使用的是Swagger,它自带MessageBodyWriter。我需要改变招摇过市的来源吗?或者有没有其他方法可以强制使用我自己的实现(无反射)?@Gobliins用
@Provider
注释你的
MessageBodyWriter
,或者应该足够让Jersey/JAX-RS.Err识别。。我不需要另一种方式(反序列化器)吗?因为我想在将传入的json映射到模型之前验证它,而不是在json流中编写模型?@Gobliins Jersey/JAX-RS已经提供了一个用于json的方法。如果提供的不符合您的需要,您可以自己编写。