简体   繁体   English

调用rest api时,无论是否使用lomboks @Data,方法均未定义

[英]Method is undefined regardless using lomboks @Data when calling rest api

I have Spring web application in Maven with rest api. 我在Maven中具有带有REST API的Spring Web应用程序。 Project compiles and Tomcat server starts without problems, but when I call some rest service from browser I get error that getter method is undefined, the method that shoud be generated by Lombok. 项目进行编译并且Tomcat服务器启动没有问题,但是当我从浏览器中调用某些rest服务时,我收到错误消息,指出getter方法未定义,该方法应该由Lombok生成。

I delombok-ed the class with this getter to see if Lombok have generated getter correctly and it did, the getter is there, but when the method is invoked by rest api call, then I get exception. 我使用此吸气剂对类进行了调试,以查看Lombok是否正确生成了吸气剂,并且确实做到了,该吸气剂已经存在,但是当该方法由rest api调用调用时,我得到了异常。

Lombok-ed class: 龙目岛课程:

@XmlRootElement
@Data
@AllArgsConstructor
@NoArgsConstructor
@Entity
@Table
public class HeroEntity
{
    @Id
    @GeneratedValue(strategy = GenerationType.AUTO)
    private long id;

    @Version
    private int version;

    @Column
    private String name;

    @ManyToMany(mappedBy = "heroes")
    private List<ComicBookEntity> comicBooks;
}

Method that throws an exception: 引发异常的方法:

@Override
    public HeroEntity add(HeroEntity heroEntity) throws DuplicateResourceException
    {
        try {
            return _heroRepository.save(heroEntity);
        }catch(final DataIntegrityViolationException e) {
            throw new DuplicateResourceException(ResourceType.HERO,
                    "Hero with id: "+heroEntity.getId()+" already exists");
        }
    }

Exception is thrown in the add method on the heroEntity.getId() call. heroEntity.getId()调用的add方法中引发了异常。 That getter should be generated by Lombok, which it is as I delombok-ed the class and everything is fine, but I still get an exception. 该吸气剂应由Lombok生成,这是我在课堂上delombok编辑的,一切都很好,但我仍然遇到异常。

pom.xml: pom.xml中:

             <plugin>
                <groupId>org.projectlombok</groupId>
                <artifactId>lombok-maven-plugin</artifactId>
                <version>1.18.0.0</version>
                <executions>
                    <execution>
                        <phase>generate-sources</phase>
                        <goals>
                            <goal>delombok</goal>
                        </goals>
                    </execution>
                </executions>
            </plugin>



<dependencies>
        <dependency>
            <groupId>org.projectlombok</groupId>
            <artifactId>lombok</artifactId>
            <version>1.16.20</version>
            <scope>provided</scope>
        </dependency>

AFAIK Lombok "works" (ie generates methods, etc.) only during the compilation phase. AFAIK Lombok仅在编译阶段才能“运行”(即生成方法等)。 All its annotations have a retention policy "SOURCE". 其所有注释均具有保留策略“ SOURCE”。 Once lombok finishes method generation, its impossible in runtime to "distinguish" between native methods and the methods generated by lombok. 一旦lombok完成了方法的生成,就不可能在运行时“区分”本机方法和由lombok生成的方法。

On the other hand, other annotations (JPA, JAXB here) get processed during runtime, so they won't interfere or something. 另一方面,其他注释(此处为JPA,JAXB)在运行时得到处理,因此它们不会造成干扰。

So I assume that, if in runtime the JVM complains that the method doesn't exist, it indeed not there. 因此,我假设,如果在运行时JVM抱怨该方法不存在,则实际上不存在。

Now if in runtime the method is not there, it's probably indeed not there :) The real question is whether Lombok didn't work at all or it generated something that JPA/Hibernate doesn't recognize as a valid method. 现在,如果在运行时该方法不存在,则可能实际上不存在:)真正的问题是Lombok是否根本不起作用,还是生成了JPA / Hibernate无法识别为有效方法的东西。

How to check this? 如何检查?

I suggest just to physically open your war file (again, I implicitly assume you have a WAR file in tomcat), locate the HeroEntity.class inside WEB-INF/lib/one-of-your-jars.jar and run it through some sort of Disassembler, like JAD or javap - just to see whats get generated. 我建议只是实际打开您的WAR文件(再次,我含蓄地假设你在Tomcat的WAR文件),找到HeroEntity.class内部WEB-INF/lib/one-of-your-jars.jar并通过一些运行类似于JADjavap的反汇编程序-只是看看生成了什么。 If you see that there is some getter, maybe its return type doesn't match Hibernate expectations (Long vs long, and so forth) 如果您发现有某种吸气剂,则它的返回类型可能与Hibernate的预期不符(长vs长,依此类推)

Alternatively, if there is no any getter at all, then Lombok didn't work and you should check your build 或者,如果根本没有任何吸气剂,那么龙目岛(Lombok)无法正常工作,您应该检查构建情况

I know this is more a speculation rather than areal and precise answer, but It looks like there is not enough information here, not because the question is bad or something, but because that the chances are that there are much more configurations under the hood of your project that can influence the answer. 我知道这更多是推测,而不是准确的答案,但似乎这里没有足够的信息,这不是因为问题不好或什么原因,而是因为有可能在您的项目可能会影响答案。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM