我想通过xtextservlet创建API:
我使用 Ecslipse DSL工具然后覆盖到 MyDslServlet.xtend 文件:
@WebServlet(name = 'XtextServices', urlPatterns = '/xtext-service/*')
class MyDslServlet extends XtextServlet {
DisposableRegistry disposableRegistry
Pattern _pattern2 = Pattern.compile("/parser")
@Inject
private ParseHelper<Model> parseHelper
.....
override doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
val url = req.getRequestURL().toString()
val _matcher = _pattern2.matcher(url)
if (_matcher.find()) {
resp.setContentType("text/x-json")
val myDslParser = new MyDslParser()
val iParseResult = myDslParser.doParse(text)
val gson = new Gson()
gson.toJson(iParseResult.toString(), resp.writer)
} else {
val service = getService(req)
if (!service.hasConflict && (service.hasSideEffects || service.hasTextInput)) {
// Send error 405 (method not allowed)
super.doGet(req, resp)
} else {
doService(service, resp)
}
}
}
}
但是,总是错误:
[qtp1650967483-11] WARN org.eclipse.jetty.servlet.ServletHandler - /xtext-service/parser
java.lang.NullPointerException
at org.eclipse.xtext.parser.antlr.AbstractAntlrParser.parse(AbstractAntlrParser.java:84)
at org.eclipse.xtext.parser.antlr.AbstractAntlrParser.doParse(AbstractAntlrParser.java:62)
at org.eclipse.xtext.parser.antlr.AbstractAntlrParser.doParse(AbstractAntlrParser.java:70)
at org.xtext.example.mydsl.web.MyDslServlet.doGet(MyDslServlet.java:72)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:687)
at