Xtext 和 JVM:将请求方法添加到请求映射

Xtext and JVM: Adding Request Method to Request Mapping

我在 Xtext 中遇到问题。 我需要这样:

@RequestMapping(value = "/create", method = RequestMethod.POST)
public String updatePhr(@PathVariable(value = "id") Long id)

但我越来越像这样:

@RequestMapping(value = "/create", method = "RequestMethod.POST")

RequestMethod.Post 作为字符串值。

我的代码在这里:

public static String PATHVARIABLE = "org.springframework.web.bind.annotation.PathVariable";

@Inject
private TypesFactory typesFactory;

@Inject
private TypeReferences references;



 def dispatch void infer(Controller element, 
                IJvmDeclaredTypeAcceptor acceptor, 
                boolean isPrelinkingPhase) {
    acceptor.accept(element.toClass( element.fullyQualifiedName )) [ {
                  Method : {
              members += component.toMethod(component.name,component.type) [
              annotations += element.toAnnotationRef(REQUESTMAPPING, "value" -> component.value, "method" -> component.method);        
              documentation = component.documentation
              var i = 1;
              for (p : component.params) {
                val j = i;
                parameters += p.toParameter(p.name, p.parameterType) => [
                    if(component.vars.get(j).isEmpty == false){
        annotations += toAnnotationRef(PATHVARIABLE, "value" -> component.vars.get(j));
        }
    ]
     i++;
              }
              body = component.body
            ]
          }

    }

如何将“@Pathvariable("id")”添加到参数中。

您使用的是 JvmStringAnnotationValue 而不是 JvmEnumAnnotationValue 或 JvmCustomAnnotationValue

@Inject
private TypeReferences references;

并且在推断注释时

members += entity.toMethod("create", Void.TYPE.typeRef()) [
    body = ''''''
    annotations += RequestMapping.annotationRef() => [
        val anno = it.annotation
        explicitValues += TypesFactory.eINSTANCE.createJvmStringAnnotationValue => [
            operation = anno.members.filter(JvmOperation).findFirst[simpleName=='value']
            values += "/create"
        ]
        explicitValues += TypesFactory.eINSTANCE.createJvmEnumAnnotationValue => [
            operation = anno.members.filter(JvmOperation).findFirst[simpleName=='method']

            values += (references.findDeclaredType(RequestMethod, entity) as JvmEnumerationType).literals.findFirst[simpleName == "POST"]
        ]
    ]

]

添加到参数

        members+=entity.toMethod("doSth", Void.TYPE.typeRef) [
            parameters += entity.toParameter("p1", String.typeRef) => [
                annotations += "test.MyAnno".annotationRef("id")
            ]
        ]