NutzCN Logo
问答 SwaggerModule能读到注解,但是web不能显示
发布于 2620天前 作者 pengmingxing 5007 次浏览 复制 上一个帖子 下一个帖子
标签:

参照楼主给的方法:https://github.com/nutzam/nutzmore/tree/master/nutz-integration-swagger
访问 http://localhost:8080/{项目名}/swagger/

Errors
Hide
Parser error on line 2
end of the stream or a document separator is expected
访问 http://localhost:8080/raiis/swagger/swagger.json
能看到自己加入的 @ApiOperation(value = "心跳接口", notes = "发我一个ping,回你一个pong", httpMethod="GET")

18 回复

急求大神解答,不胜感激

头部的地址, 写的是什么?

@At("/swagger")
方法都是照着 https://github.com/nutzam/nutzmore/tree/master/nutz-integration-swagger 这上面操作的

@IocBean(create = "init")
@At("/swagger")
public class SwaggerModule {

    private static final Log log = Logs.get();

    protected Swagger swagger;

    @At
    public void swagger(HttpServletRequest request, HttpServletResponse response) throws Exception {
    	if ("true".equals(request.getParameter("force")))
            init(); //强制刷新
        final String pathInfo = request.getRequestURI();
        if (pathInfo.endsWith("/swagger.json")) {
            response.setContentType("application/json");
            response.getWriter().println(Json.mapper().writeValueAsString(swagger));
        } else if (pathInfo.endsWith("/swagger.yaml")) {
            response.setContentType("application/yaml");
            response.getWriter().println(Yaml.mapper().writeValueAsString(swagger));
        } else {
            response.setStatus(404);
        }
    }

    public void init() {
        log.info("init swagger ...");
        swagger = new Swagger();
        HashSet<Class<?>> classes = new HashSet<>();
        // 把下来的package路径改成你自己的package路径
        for (Class<?> klass : Scans.me().scanPackage("com.panport.raiis")) {
            classes.add(klass);
        }
        Reader.read(swagger, classes);
    }
}

我说的是页面上, 白色框里面有个地址, 写的是什么

那个地址可以写绝对地址,你试试

url是:http://localhost:8080/{项目}/swagger/
搜索框里是: ./swagger.json
这里面Json.mapper().writeValueAsString(swagger) 是有值的,
但是返回前端,就报错了

报错内容具体一点啊, 即使是页面报错, Chrome的console也能找到点信息

报的: {"schemaValidationMessages":[{"level":"error","message":"Can't read from file ./swagger.json"}]}
console报的 : (anonymous) @ swagger-ui-bundle.js:8202
不太懂前端,麻烦大神解答下!

既然你说 http://localhost:8080/raiis/swagger/swagger.json 可以访问, 那就把这个地址贴上去,然后按Explore

贴了,还是报一样的错~
有个问题是,访问 http://localhost:8080/raiis/swagger/swagger.json 时候,返回的值,后面会多个null

{"swagger":"2.0","paths":{"/add":{"post":{"parameters":[],"responses":{"default":{"description":"successful operation"}}}},"/update":{"put":{"parameters":[],"responses":{"default":{"description":"successful operation"}}}},"/delete":{"delete":{"parameters":[],"responses":{"default":{"description":"successful operation"}}}},"/query":{"get":{"parameters":[],"responses":{"default":{"description":"successful operation"}}}},"/queryTemp":{"get":{"parameters":[],"responses":{"default":{"description":"successful operation"}}}},"/updateRolePermission":{"put":{"parameters":[],"responses":{"default":{"description":"successful operation"}}}},"/fetchRolePermissions":{"get":{"parameters":[],"responses":{"default":{"description":"successful operation"}}}},"/blurryQueryRegistration":{"get":{"parameters":[],"responses":{"default":{"description":"successful operation"}}}},"/getCandidate":{"get":{"parameters":[],"responses":{"default":{"description":"successful operation"}}}},"/getDevicegroupById":{"get":{"parameters":[],"responses":{"default":{"description":"successful operation"}}}},"/logo":{"get":{"parameters":[],"responses":{"default":{"description":"successful operation"}}}},"/upload":{"post":{"parameters":[],"responses":{"default":{"description":"successful operation"}}}},"/deleteFlight":{"delete":{"parameters":[],"responses":{"default":{"description":"successful operation"}}}},"/departReversal":{"put":{"parameters":[],"responses":{"default":{"description":"successful operation"}}}},"/getTableValue":{"get":{"parameters":[],"responses":{"default":{"description":"successful operation"}}}},"/newAlternate":{"put":{"parameters":[],"responses":{"default":{"description":"successful operation"}}}},"/arriveAlternate":{"put":{"parameters":[],"responses":{"default":{"description":"successful operation"}}}},"/updateFlightSegment":{"put":{"parameters":[],"responses":{"default":{"description":"successful operation"}}}},"/deleteFlightSegment":{"delete":{"parameters":[],"responses":{"default":{"description":"successful operation"}}}},"/addOrUpdateGateAllocationList":{"put":{"parameters":[],"responses":{"default":{"description":"successful operation"}}}},"/addOrUpdateSingleGateAllocation":{"put":{"parameters":[],"responses":{"default":{"description":"successful operation"}}}},"/deleteGateAllocationList":{"delete":{"parameters":[],"responses":{"default":{"description":"successful operation"}}}},"/queryMyInfo":{"get":{"parameters":[],"responses":{"default":{"description":"successful operation"}}}},"/getCheckinGroupList":{"get":{"parameters":[],"responses":{"default":{"description":"successful operation"}}}},"/out":{"post":{"parameters":[],"responses":{"default":{"description":"successful operation"}}}},"/login":{"get":{"parameters":[],"responses":{"default":{"description":"successful operation"}}},"post":{"parameters":[],"responses":{"default":{"description":"successful operation"}}}},"/loginNoAuthor":{"get":{"parameters":[],"responses":{"default":{"description":"successful operation"}}}},"/updateUserRole":{"put":{"parameters":[],"responses":{"default":{"description":"successful operation"}}}},"/batchLocked":{"put":{"parameters":[],"responses":{"default":{"description":"successful operation"}}}},"/batchUnlocked":{"put":{"parameters":[],"responses":{"default":{"description":"successful operation"}}}},"/fetchUserRoles":{"get":{"parameters":[],"responses":{"default":{"description":"successful operation"}}}},"/updateUserPermission":{"put":{"parameters":[],"responses":{"default":{"description":"successful operation"}}}},"/fetchUserPermissions":{"get":{"parameters":[],"responses":{"default":{"description":"successful operation"}}}},"/addOrUpdateStandAllocationList":{"put":{"parameters":[],"responses":{"default":{"description":"successful operation"}}}},"/addOrUpdateSingleStandAllocation":{"put":{"parameters":[],"responses":{"default":{"description":"successful operation"}}}},"/deleteStandAllocationList":{"delete":{"parameters":[],"responses":{"default":{"description":"successful operation"}}}},"/getAllocationByFlightId":{"get":{"parameters":[],"responses":{"default":{"description":"successful operation"}}}},"/addOrUpdateCheckinCounterJoint":{"put":{"parameters":[],"responses":{"default":{"description":"successful operation"}}}},"/deleteCheckinCounterTime":{"delete":{"parameters":[],"responses":{"default":{"description":"successful operation"}}}},"/addOrUpdateCheckinCounterJointList":{"put":{"parameters":[],"responses":{"default":{"description":"successful operation"}}}},"/permissions":{"get":{"parameters":[],"responses":{"default":{"description":"successful operation"}}}},"/addPermission":{"post":{"parameters":[],"responses":{"default":{"description":"successful operation"}}}},"/updatePermission":{"put":{"parameters":[],"responses":{"default":{"description":"successful operation"}}}},"/delPermission":{"delete":{"parameters":[],"responses":{"default":{"description":"successful operation"}}}},"/swagger/ping":{"get":{"summary":"心跳接口","description":"发我一个ping,回你一个pong","operationId":"ping","parameters":[],"responses":{"200":{"description":"successful operation","schema":{"type":"object"}}}}},"/queryMenus":{"get":{"parameters":[],"responses":{"default":{"description":"successful operation"}}}},"/deleteCarouselAllocationList":{"delete":{"parameters":[],"responses":{"default":{"description":"successful operation"}}}},"/addOrUpdateCarouselAllocationList":{"put":{"parameters":[],"responses":{"default":{"description":"successful operation"}}}},"/addOrUpdateSingleCarouselAllocation":{"put":{"parameters":[],"responses":{"default":{"description":"successful operation"}}}},"/get":{"get":{"parameters":[],"responses":{"default":{"description":"successful operation"}}}},"/uploadAvatar":{"put":{"parameters":[],"responses":{"default":{"description":"successful operation"}}}},"/readAvatar":{"get":{"parameters":[],"responses":{"default":{"description":"successful operation"}}}},"/activeMail":{"post":{"parameters":[],"responses":{"default":{"description":"successful operation"}}}},"/me":{"get":{"parameters":[],"responses":{"default":{"description":"successful operation"}}}},"/activeMailCallback":{"get":{"parameters":[],"responses":{"default":{"description":"successful operation"}}}},"/getData":{"get":{"parameters":[],"responses":{"default":{"description":"successful operation"}}}},"/getFlightData":{"get":{"parameters":[],"responses":{"default":{"description":"successful operation"}}}}}}
null

在入口方法上加个

@Ok("void")

嗯,确实是可以了,谢谢大神

swagger-ui-bundle.js:7950 
r {name: "YAMLException", reason: "end of the stream or a document separator is expected", mark: r, message: "end of the stream or a document separator is expec…  box-sizing: border-box;↵                      ^", stack: "YAMLException: end of the stream or a document sep…/llp/assets/swagger/swagger-ui-bundle.js:52:68055"}
mark: r {name: null, buffer: "<!-- HTML for static distribution bundle build -->…↵↵  window.ui = ui↵}↵</script>↵</body>↵↵</html>↵", position: 603, line: 13, column: 18}
message: "end of the stream or a document separator is expected at line 14, column 19:↵            box-sizing: border-box;↵                      ^"
name: "YAMLException"
reason: "end of the stream or a document separator is expected"
stack: "YAMLException: end of the stream or a document separator is expected at line 14, column 19:↵            box-sizing: border-box;↵                      ^↵    at h (http://localhost:8080/llp/assets/swagger/swagger-ui-bundle.js:73:51760)↵    at d (http://localhost:8080/llp/assets/swagger/swagger-ui-bundle.js:73:51858)↵    at j (http://localhost:8080/llp/assets/swagger/swagger-ui-bundle.js:73:64439)↵    at N (http://localhost:8080/llp/assets/swagger/swagger-ui-bundle.js:73:64794)↵    at F (http://localhost:8080/llp/assets/swagger/swagger-ui-bundle.js:73:64944)↵    at Object.L [as safeLoad] (http://localhost:8080/llp/assets/swagger/swagger-ui-bundle.js:73:65199)↵    at http://localhost:8080/llp/assets/swagger/swagger-ui-bundle.js:12:73295↵    at http://localhost:8080/llp/assets/swagger/swagger-ui-bundle.js:6:1902↵    at Object.parseToJson (http://localhost:8080/llp/assets/swagger/swagger-ui-bundle.js:79:164825)↵    at http://localhost:8080/llp/assets/swagger/swagger-ui-bundle.js:52:68055"
__proto__: Error

我的也遇到类似问题,这个是前端报的错

加了@Ok("void")也一样,后台是这样的

[2020-12-07 07:58:39,982] Artifact llp:war exploded: Artifact is deployed successfully
[2020-12-07 07:58:39,983] Artifact llp:war exploded: Deploy took 16,769 milliseconds
2020-12-07 19:58:52,232 org.nutz.mvc.impl.UrlMappingImpl.get(UrlMappingImpl.java:101) DEBUG - Found mapping for [GET] path=/swagger : SwaggerModule.index(SwaggerModule.java:75)
2020-12-07 19:58:52,237 org.nutz.ioc.impl.NutIoc.get(NutIoc.java:166) DEBUG - Get 'swaggerModule'<class com.ysu.llp.proxy.module.SwaggerModule>
2020-12-07 19:58:52,238 org.nutz.ioc.impl.NutIoc.get(NutIoc.java:192) DEBUG - 	 >> Load definition name=swaggerModule
2020-12-07 19:58:52,238 org.nutz.ioc.loader.combo.ComboIocLoader.printFoundIocBean(ComboIocLoader.java:226) DEBUG - Found IocObject(swaggerModule) in AnnotationIocLoader(packages=[com.ysu.llp.proxy])
2020-12-07 19:58:52,239 org.nutz.ioc.impl.NutIoc.get(NutIoc.java:223) DEBUG - 	 >> Make...'swaggerModule'<class com.ysu.llp.proxy.module.SwaggerModule>
2020-12-07 19:58:52,241 org.nutz.ioc.impl.ScopeContext.save(ScopeContext.java:65) DEBUG - Save object 'swaggerModule' to [app] 
2020-12-07 19:58:52,242 org.nutz.ioc.aop.impl.DefaultMirrorFactory.getMirror(DefaultMirrorFactory.java:76) DEBUG - Load class com.ysu.llp.proxy.module.SwaggerModule without AOP
2020-12-07 19:58:52,259 com.ysu.llp.proxy.module.SwaggerModule.init(SwaggerModule.java:78) INFO  - init swagger ...
2020-12-07 19:58:52,268 org.nutz.resource.Scans.scan(Scans.java:280) DEBUG - Found 0 resource by src( com/yus/llp/proxy/ ) , regex( ^.+[.]class$ )
2020-12-07 19:58:52,287 org.nutz.mvc.impl.UrlMappingImpl.get(UrlMappingImpl.java:110) DEBUG - Search mapping for [GET] path=/assets/swagger/index : NOT Action match
2020-12-07 19:58:52,292 com.ysu.llp.shiro.LogTimeProcessor.process(LogTimeProcessor.java:23) DEBUG - [ GET]URI=/llp/swagger 59ms
2020-12-07 19:58:53,039 org.nutz.mvc.impl.UrlMappingImpl.get(UrlMappingImpl.java:101) DEBUG - Found mapping for [GET] path=/swagger : SwaggerModule.index(SwaggerModule.java:75)
2020-12-07 19:58:53,039 org.nutz.ioc.impl.NutIoc.get(NutIoc.java:166) DEBUG - Get 'swaggerModule'<class com.ysu.llp.proxy.module.SwaggerModule>
2020-12-07 19:58:53,040 org.nutz.mvc.impl.UrlMappingImpl.get(UrlMappingImpl.java:110) DEBUG - Search mapping for [GET] path=/assets/swagger/index : NOT Action match
2020-12-07 19:58:53,041 com.ysu.llp.shiro.LogTimeProcessor.process(LogTimeProcessor.java:23) DEBUG - [ GET]URI=/llp/swagger.json 2ms

请大神指教!

http://localhost:8080/llp/swagger/swagger.json能返回数据;
http://localhost:8080/llp/swagger就报错,前端console错误是14楼,页面报错:

Errors
Hide
Parser error on line 14
end of the stream or a document separator is expected
No operations defined in spec!
Online validator badge

点击 INVALID 之后页面返回:
{"schemaValidationMessages":[{"level":"error","message":"Can't read from file ./swagger.json"}]}

这是我的SwaggerModule

package com.ysu.llp.proxy.module;

import java.util.HashSet;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.nutz.ioc.loader.annotation.IocBean;
import org.nutz.lang.util.NutMap;
import org.nutz.log.Log;
import org.nutz.log.Logs;
import org.nutz.mvc.Mvcs;
import org.nutz.mvc.annotation.At;
import org.nutz.mvc.annotation.GET;
import org.nutz.mvc.annotation.Ok;
import org.nutz.mvc.annotation.POST;
import org.nutz.mvc.annotation.Param;
import org.nutz.resource.Scans;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import io.swagger.models.Info;
import io.swagger.models.Swagger;
import io.swagger.servlet.Reader;
import io.swagger.util.Json;
import io.swagger.util.Yaml;

@Api(value = "swagger")
@IocBean(create = "init")
@At("/swagger")
public class SwaggerModule {

    private static final Log log = Logs.get();

    protected Swagger swagger;

    @GET
    @ApiOperation(value = "Swagger本身的数据", notes = "Swagger的swagger.(json|yaml)数据接口")
    @At
    @Ok("void")
    public void swagger(HttpServletRequest request, HttpServletResponse response) throws Exception {
        final String pathInfo = request.getRequestURI();
        if (pathInfo.endsWith("/swagger.json")) {
            response.setContentType("application/json");
            response.getWriter().println(Json.mapper().writeValueAsString(swagger));
        } else if (pathInfo.endsWith("/swagger.yaml")) {
            response.setContentType("application/yaml");
            response.getWriter().println(Yaml.mapper().writeValueAsString(swagger));
        } else {
            response.setStatus(404);
        }
    }

    @GET
    @ApiOperation(value = "心跳接口", notes = "发我一个ping,回你一个pong")
    @At
    @Ok("json:full")
    public Object ping() {
        return new NutMap("ok", true).setv("data", "pong");
    }

    @POST
    @ApiOperation(value = "回显接口", notes = "发我一个字符串,原样回复一个字符串")
    @ApiImplicitParams({@ApiImplicitParam(name = "text", paramType="form", value = "想发啥就发啥", dataType="string", required = true)})
    @At
    @Ok("raw")
    public String echo(@Param("text") String text) {
        return text;
    }

    @At("/")
    @Ok("->:/assets/swagger/index.html")
    public void index() {}

    public void init() {
        log.info("init swagger ...");
        Info info = new Info();
        info.title("NutzCN");
        swagger = new Swagger();
        swagger.setInfo(info);
        swagger.setBasePath(Mvcs.getServletContext().getContextPath());
        HashSet<Class<?>> classes = new HashSet<>();
        for (Class<?> klass : Scans.me().scanPackage("com.ysu.llp.proxy")) {
            classes.add(klass);
        }
        Reader.read(swagger, classes);
    }
}

@wendal 兽总看到麻烦帮忙看下,蟹蟹!!

添加回复
请先登陆
回到顶部