繁体   English   中英

如何使用Spring Boot获取URL参数

[英]How to get url parameter with spring boot

我正在尝试完成教程https://spring.io/guides/tutorials/react-and-spring-data-rest/

本教程仅创建一个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"));
    }
}

初始化数据库H2; 实体员工:

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;
    }
}

使用Lombok @Data创建getter和setter的方法; EmployeeRepository:包com.vli.react_spring_tutorial;

package com.vli.react_spring_tutorial;

import org.springframework.data.repository.CrudRepository;

public interface EmployeeRepository extends CrudRepository<Employee, Long> {

}

只是一个原始库; 和类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);
    }
}

项目的主要类别。 我也设置了spring.data.rest.base-path=/api来更改spring.data.rest.base-path=/api的端点。

当我尝试使用URL:localhost:8080 / api / employees / 1时,响应为:

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

而不是预期的有关ID为1的员工的信息。 在控制台中,我得到了:

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]...

如果我使用url localhost:8080 / api / employees,则会得到以下信息:

{
  "_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"
    }
  }
}

可以看到,它知道我在数据库中有多少输入,但不加载员工详细信息。

在我看来,Spring Boot无法从URL中提取索引。 有没有我可能会忘记的配置?

一些可能的线索:

  1. 我正在非常严格的环境中尝试此操作。 我工作的公司有很多安全规则。 我无法在自己的计算机上成为管理员,有很多防火墙限制,防病毒等。也许其中之一可能会引起问题。
  2. 我打开H2控制台,看到数据库已正确填充。
  3. 有趣的是,我的应用程序知道我有多少员工,但无法检索他们的信息。
  4. 我尝试使用curl POST在数据库中插入新员工。 它创建一个新的员工条目,为其分配ID,但不能在其上记录数据。

之所以不起作用,是因为在使用具有maven2eclipse功能的Eclipse构建时,要求Lombok代理与IDE一起使用以生成预期的代码。

如果它是纯粹用maven编译的,那会行得通,但是如果没有代理,通过Eclipse和m2e进行的构建将无法工作。

暂无
暂无

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

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