简体   繁体   中英

How to get url parameter with spring boot

I'm trying to complete the tutorial https://spring.io/guides/tutorials/react-and-spring-data-rest/ .

This tutorial create only a DatabaseLoader:

package com.vli.react_spring_tutorial;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.CommandLineRunner;
import org.springframework.stereotype.Component;

@Component
public class DatabaseLoader implements CommandLineRunner {
    private final EmployeeRepository repository;

    @Autowired
    public DatabaseLoader(EmployeeRepository repository) {
        this.repository = repository;
    }

    @Override
    public void run(String... strings) throws Exception {
        this.repository.save(new Employee("Frodo", "Baggins", "ring bearer"));
        this.repository.save(new Employee("Leandro", "Santos", "Dev"));
        this.repository.save(new Employee("Weverton", "Dias", "Dev"));
    }
}

that initialize the databases H2; the entity employee:

package com.vli.react_spring_tutorial;

import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;

import lombok.Data;

@Data
@Entity
public class Employee {

    private @Id @GeneratedValue Long id;
    private String firstName;
    private String lastname;
    private String description;

    private Employee() {}

    public Employee(String firstName, String lastname, String description) {        
        this.firstName = firstName;
        this.lastname = lastname;
        this.description = description;
    }
}

that uses Lombok @Data to create getters and setters; EmployeeRepository: package com.vli.react_spring_tutorial;

package com.vli.react_spring_tutorial;

import org.springframework.data.repository.CrudRepository;

public interface EmployeeRepository extends CrudRepository<Employee, Long> {

}

just a crud repository; and the class ReactSpringTutorialApplication:

package com.vli.react_spring_tutorial;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

@SpringBootApplication
public class ReactSpringTutorialApplication {

    public static void main(String[] args) {
        SpringApplication.run(ReactSpringTutorialApplication.class, args);
    }
}

the main class of the project. Also I have set the spring.data.rest.base-path=/api to change the endpoint for api.

When I try the URL: localhost:8080/api/employees/1 the response is:

{
  "_links" : {
    "self" : {
      "href" : "http://localhost:8080/api/employees/1"
    },
    "employee" : {
      "href" : "http://localhost:8080/api/employees/1"
    }
  }
}

instead of informations about the employee with the id 1, as expected. In the console I have gotten:

2017-08-07 13:13:29.043 ERROR 7328 --- [nio-8080-exec-1] o.s.d.r.w.RepositoryRestExceptionHandler : Failed to convert from type [java.lang.String] to type [java.lang.Long] for value 'undefined'; nested exception is java.lang.NumberFormatException: For input string: "undefined"

org.springframework.core.convert.ConversionFailedException: Failed to convert from type [java.lang.String] to type [java.lang.Long] for value 'undefined'; nested exception is java.lang.NumberFormatException: For input string: "undefined"
    at org.springframework.core.convert.support.ConversionUtils.invokeConverter(ConversionUtils.java:43) ~[spring-core-4.3.10.RELEASE.jar:4.3.10.RELEASE]
    at org.springframework.core.convert.support.GenericConversionService.convert(GenericConversionService.java:203) ~[spring-core-4.3.10.RELEASE.jar:4.3.10.RELEASE]
    at org.springframework.core.convert.support.GenericConversionService.convert(GenericConversionService.java:187) ~[spring-core-4.3.10.RELEASE.jar:4.3.10.RELEASE]
    at org.springframework.data.repository.support.ReflectionRepositoryInvoker.convertId(ReflectionRepositoryInvoker.java:277) ~[spring-data-commons-1.13.6.RELEASE.jar:na]
    at org.springframework.data.repository.support.CrudRepositoryInvoker.invokeFindOne(CrudRepositoryInvoker.java:91) ~[spring-data-commons-1.13.6.RELEASE.jar:na]
    at org.springframework.data.rest.core.support.UnwrappingRepositoryInvokerFactory$UnwrappingRepositoryInvoker.invokeFindOne(UnwrappingRepositoryInvokerFactory.java:130) ~[spring-data-rest-core-2.6.6.RELEASE.jar:na]
    at org.springframework.data.rest.webmvc.RepositoryEntityController.getItemResource(RepositoryEntityController.java:524) ~[spring-data-rest-webmvc-2.6.6.RELEASE.jar:na]...

If I use the url localhost:8080/api/employees I get this:

{
  "_embedded" : {
    "employees" : [ {
      "_links" : {
        "self" : {
          "href" : "http://localhost:8080/api/employees/1"
        },
        "employee" : {
          "href" : "http://localhost:8080/api/employees/1"
        }
      }
    }, {
      "_links" : {
        "self" : {
          "href" : "http://localhost:8080/api/employees/2"
        },
        "employee" : {
          "href" : "http://localhost:8080/api/employees/2"
        }
      }
    }, {
      "_links" : {
        "self" : {
          "href" : "http://localhost:8080/api/employees/3"
        },
        "employee" : {
          "href" : "http://localhost:8080/api/employees/3"
        }
      }
    } ]
  },
  "_links" : {
    "self" : {
      "href" : "http://localhost:8080/api/employees"
    },
    "profile" : {
      "href" : "http://localhost:8080/api/profile/employees"
    }
  }
}

As one can see, it knows how many inputs I have in the databases, but don't loads the employees details.

It seems to me that the spring boot can't extract the index from URL. Is there any configuration or absence of this that I could have forgotten?

Some possible clues:

  1. I'm trying this in a very restrictive ambient. The company where I work for have a lot of security rules. I can't be administrator in my own computer, there are many firewall restrictions, antivirus, etc. Maybe one of this things can cause problems.
  2. I open the H2 console and see that the database is filled correctly.
  3. It is interesting to see that my application knows how many employees I have but it can't retrieve information of them.
  4. I tryed to use curl POST to insert new employees in the database. It creates a new employee entry, assigns an Id to it but can't record data on it.

The reason this was not working was due to the fact the Lombok agent is required to be used with the IDE to generate the expected code when using Eclipse builds with maven2eclipse functionality.

This would have worked if it was compiled purely with maven, but builds via Eclipse and m2e do not work without the agent being there.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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