-2

i'm facing issue while working with ControllerLinkBuilder when i provide method name it throw error when i don't it run and show me result.

error seems jar issue but i tried with other versions too still same ' spring-boot-starter-hateoas' it doesn;t recognize the packages for RepresentationModel & ControllerLinkBuilderlinkTo

@GetMapping("/{businessclass}/lists/_generic")
public  ResponseEntity<POLineResponse>  businessClass(@PathVariable final String businessclass,
        @RequestParam String _fields, @RequestParam("company") String company) {
     logger.info("Entering into businessClass {} params{} ",businessclass, _fields);

     Page<PurchaseOrderLineDTO> page= mockFsmService.generateMockPurchaseOrderLines();

     POLineResponse polR = new POLineResponse (page.getContent(),page.getTotalElements());
     //polR.add(linkTo(LoadTestController.class).businessClass(businessclass,_fields,company)).withSelfRel();
    //polR.add(ControllerLinkBuilderlinkTo(LoadTestController.class).withSelfRel());

      Link selfLink = ControllerLinkBuilder.linkTo(methodOn(LoadTestController.class).businessClass(businessclass, _fields, company))
      .withSelfRel();

    polR.add(selfLink);
     return new ResponseEntity<>(polR, HttpStatus.OK);
}

refer

dependency

<dependency>
            <groupId>org.springframework.data</groupId>
            <artifactId>spring-data-commons</artifactId>
            <version>2.2.0.RELEASE</version>
        </dependency>


        <!-- https://mvnrepository.com/artifact/org.springframework.hateoas/spring-hateoas -->
        <dependency>
            <groupId>org.springframework.hateoas</groupId>
            <artifactId>spring-hateoas</artifactId>
            <version>1.0.3.RELEASE</version>
        </dependency>

maven pom

    <?xml version="1.0" encoding="UTF-8"?>
    <project xmlns="http://maven.apache.org/POM/4.0.0"
        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
        xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
        <modelVersion>4.0.0</modelVersion>
        <parent>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-parent</artifactId>
            <version>2.1.4.RELEASE</version>
            <relativePath /> <!-- lookup parent from repository -->
        </parent>
        <groupId>com.demo.cloudproj</groupId>
        <artifactId>cloudproj-loadtest</artifactId>
        <version>0.0.1-SNAPSHOT</version>
        <name>cloudprojloadtest</name>
        <description>cloudprojLoadTest</description>

        <properties>
            <java.version>12</java.version>
        </properties>

        <dependencies>
            <dependency>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-starter-web</artifactId>
            </dependency>
            <dependency>
                <groupId>com.opencsv</groupId>
                <artifactId>opencsv</artifactId>
                <version>4.1</version>
            </dependency>

            <dependency>
                <groupId>com.googlecode.json-simple</groupId>
                <artifactId> json-simple</artifactId>
                <version>1.1</version>
            </dependency>

            <dependency>
                <groupId>org.apache.poi</groupId>
                <artifactId>poi-ooxml</artifactId>
                <version>3.9</version>
            </dependency>

            <dependency>
                <groupId>com.fasterxml.jackson.core</groupId>
                <artifactId>jackson-databind</artifactId>
            </dependency>

            <dependency>
                <groupId>com.google.code.gson</groupId>
                <artifactId>gson</artifactId>
            </dependency>

            <dependency>
                <groupId>com.fasterxml.jackson.core</groupId>
                <artifactId>jackson-databind</artifactId>
            </dependency>
            <dependency>
                <groupId>io.codearte.jfairy</groupId>
                <artifactId>jfairy</artifactId>
                <version>0.5.0</version>
            </dependency>
  <dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-hateoas</artifactId>
    <version>1.2.2.RELEASE</version>
</dependency>

    </dependencies>

    <build>
        <!--resources> <resource> <directory>src/main/resources</directory> <filtering>true</filtering> 
            <excludes> <exclude>**/*.properties</exclude> </excludes> </resource> </resources -->
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
                <configuration>
                    <addResources>false</addResources>
                </configuration>
            </plugin>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-resources-plugin</artifactId>
                <executions>

                </executions>
            </plugin>
        </plugins>
        <pluginManagement>
            <plugins>
                <!--This plugin's configuration is used to store Eclipse m2e settings 
                    only. It has no influence on the Maven build itself. -->
                <plugin>
                    <groupId>org.eclipse.m2e</groupId>
                    <artifactId>lifecycle-mapping</artifactId>
                    <version>1.0.0</version>
                    <configuration>
                        <lifecycleMappingMetadata>
                            <pluginExecutions>
                                <pluginExecution>
                                    <pluginExecutionFilter>
                                        <groupId>
                                            org.apache.maven.plugins
                                        </groupId>
                                        <artifactId>
                                            maven-antrun-plugin
                                        </artifactId>
                                        <versionRange>
                                            [1.8,)
                                        </versionRange>
                                        <goals>
                                            <goal>run</goal>
                                        </goals>
                                    </pluginExecutionFilter>
                                    <action>
                                        <ignore></ignore>
                                    </action>
                                </pluginExecution>
                            </pluginExecutions>
                        </lifecycleMappingMetadata>
                    </configuration>
                </plugin>
            </plugins>
        </pluginManagement>
    </build>
</project>
Himanshi
  • 84
  • 1
  • 2
  • 13
  • Don't do a "tag" spam and don't post an error as image, just include it as a text to the question. Do you have any parent specified in your pom? It looks like the MergedAnnotations are in the spring-core dependency, so if the spring-boot is not the parent then you'll need to include core by your self. – itwasntme Mar 02 '20 at 08:34
  • If you are using Spring Boot why manually add depndencies? This will lead to incompatible versions of frameworks. – M. Deinum Mar 02 '20 at 08:50
  • @M.Deinum yes you are right but when i'm adding https://mvnrepository.com/artifact/org.springframework.boot/spring-boot-starter-hateoas it shows compile issue and the RepresentationModel and ControllerLinkBuilder showing compile time error – Himanshi Mar 02 '20 at 09:30
  • @M.Deinum any suggestions? – Himanshi Mar 02 '20 at 09:30
  • Add the one that belongs to the spring boot version you are using. The fact that you also manually add `spring-data-commons` makes me wonder what else is there. Looks like your dependencies are messy. One thing why have 3 json libraries on your classpath? – M. Deinum Mar 02 '20 at 09:34
  • Your pom looks incomplete in the question. Just the dependencies section should be enough. – M. Deinum Mar 02 '20 at 09:36
  • already tried with org.springframework.data spring-data-commons 2.2.0.RELEASE it worked but as say shows this error – Himanshi Mar 02 '20 at 09:39
  • You shouldn't add those dependencies. Just add the `spring-boot-starter-hateoas` WITHOUT a version all those are managed by Sprig Boot. – M. Deinum Mar 02 '20 at 09:45
  • @M.Deinum after doing so it shows compile error on my code on methodOn function Link selfLink = ControllerLinkBuilder.linkTo(methodOn(LoadTestController.class).businessClass(businessclass, _fields, company)) .withSelfRel(); – Himanshi Mar 02 '20 at 09:48
  • Then that method doesn't exists and you are using code samples from a different version of Spring Hateaos. Nonentheless please add the additional pom parts (currently it is broken off). – M. Deinum Mar 02 '20 at 10:05
  • @M.Deinum just update the pom – Himanshi Mar 02 '20 at 10:11

2 Answers2

2

You should be on at least Spring Boot 2.2. With that, you can use spring-boot-starter-hateoas which brings in Spring HATEOAS 1.0, the version with RepresentationModel etc.

That version also depends upon Spring Framework 5.2, which needs to leverage MergedAnnotations. Using Boot 2.1 will put you on an older version of Framework, hence causing all kinds of issues.

gregturn
  • 2,625
  • 3
  • 25
  • 40
0

Can you add below dependency again and run the application

<dependency>
    <groupId>org.springframework.hateoas</groupId>
    <artifactId>spring-hateoas</artifactId>
    <version>0.25.1.RELEASE</version> //you can add latest version or down 
</dependency>
<dependency>
    <groupId>org.springframework.plugin</groupId>
    <artifactId>spring-plugin-core</artifactId>
  <version>1.0.3.RELEASE</version> //you can add latest version or down 
</dependency>
Dulaj Kulathunga
  • 1,248
  • 2
  • 9
  • 19
  • found answer why RepresentationModel is not working As In case you are using HATEOAS v1.0 and above (Spring boot >= 2.2.0), do note that the classnames have changed. Notably the below classes have been renamed: ResourceSupport changed to RepresentationModel Resource changed to EntityModel Resources changed to CollectionModel PagedResources changed to PagedModel ResourceAssembler changed to RepresentationModelAssembler – Himanshi Mar 02 '20 at 10:44
  • `1.0.3.RELEASE` OR `0.25.1.RELEASE` You can change as you wish , I have added only and for getting idea and what kind of things it should be added – Dulaj Kulathunga Mar 02 '20 at 11:03