Java 如何在springboot中构建microserviceget-api,它可以将单个或多个参数作为请求url中的insert,并将数据作为请求获取
获取/数据/搜索?将返回所有记录 GET/data/search?name=jhon&add=US将根据请求返回特定记录 获取/数据/搜索?Amount=100将返回特定于Amount 100的所有记录 客户可以根据自己的要求随时输入以上全部或部分参数 任何指向参考代码的链接也可以 下面是我的错误和实现 当我的repo方法没有被调用时,它调用服务方法并打印控制器和服务的sysout,但不是repo的sysout,这意味着它没有调用repo,下面是控制台输出: 请求参数已收到sysout 内部服务方法SysOut 2019-10-15 13:54:30185错误[http-nio-8080-exec-1]org.apache.juli.logging.DirectJDKLog:Servlet[dispatcherServlet]的Servlet.service在路径[]的上下文中引发异常[Request processing failed;嵌套异常为java.lang.NullPointerException],并带有根本原因 java.lang.NullPointerException:nullJava 如何在springboot中构建microserviceget-api,它可以将单个或多个参数作为请求url中的insert,并将数据作为请求获取,java,spring-boot,Java,Spring Boot,获取/数据/搜索?将返回所有记录 GET/data/search?name=jhon&add=US将根据请求返回特定记录 获取/数据/搜索?Amount=100将返回特定于Amount 100的所有记录 客户可以根据自己的要求随时输入以上全部或部分参数 任何指向参考代码的链接也可以 下面是我的错误和实现 当我的repo方法没有被调用时,它调用服务方法并打印控制器和服务的sysout,但不是repo的sysout,这意味着它没有调用repo,下面是控制台输出: 请求参数已收到sysout 内部服务
@RestController
public class MobileController {
@GetMapping(value="/mobile/search")
public List<MobileResponse> getMobile(@RequestParam Map<String, String>
map) {
MobileService mobileService =new MobileService();
System.out.println("Request param received");
map.forEach((k,v)->System.out.println(k+":"+v));
List<MobileResponse> mobiles = mobileService.getAllMobiles(map);
mobiles.forEach(mobile-> System.out.println(mobile));
return mobiles;
}
}
硬件实体
@Entity
@Table(name="Hardware")
public class Hardware implements Serializable {
//Logger logger = (Logger) LoggerFactory.getLogger(Hardware.class);
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private long id;
@Column(name="audioJacks")
private String audioJacks;
@Column(name = "gps")
private String gps;
@Column(name = "battery")
private String battery;
//followed by setter and getter methods
}
释放实体
@Entity
@Table(name="Releases")
public class Releases implements Serializable {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private long id;
@Column(name="priceEuro")
private long priceEuro;
@Column(name="announceDate")
private String announceDate;
//followed by setter and getter methods
}
data.sql文件
enter code here
drop table if exists Hardware;
CREATE table Hardware (
id INT AUTO_INCREMENT PRIMARY KEY,
audioJacks varchar(200),
gps varchar(100),
battery varchar(200),
);
drop table if exists Releases;
create table If Not exists Releases (
id INT AUTO_INCREMENT PRIMARY KEY,
priceEuro int,
announceDate varchar(100),
);
drop table if exists Mobile;
CREATE TABLE If Not exists Mobile (
id INT AUTO_INCREMENT PRIMARY KEY,
brand VARCHAR(250),
phone VARCHAR(250),
picture VARCHAR(250),
sim VARCHAR(250),
resolution VARCHAR(250),
hardware_id int references Hardware(id),
releases_id int references Releases(id)
);
//followed by insert query first for releases,hardware and then for mobile
data is inserted as expected in db.
应该是这样的:
@GetMapping("/data/search")
public YourObject getData(@RequestParam(value = "name", required = false) String name,
@RequestParam(value = "amount", required = false) Integer amount){
...
}
实现这一目标有多种方法 您可以将每个查询参数作为方法参数接收: @GetMapping/foo 公共责任getFoo@RequestParamvalue=名称,必需=假字符串名称, @RequestParamvalue=金额,required=假整数金额{ ... } 您可以将所有参数作为映射接收: @GetMapping/foo 公共责任getFoo@RequestParam映射参数{ ... } 或者,您可以定义一个用于表示参数的类,并将其作为方法参数接收: @资料 公共类FooQueryParameters{ 私有字符串名称; 私人整数金额; } @GetMapping/foo 公共响应属性GetFooQueryParameters参数{ ... }
您使用的是JAX-RS或Spring之类的框架吗?请为您的问题添加更多细节。我正在使用springboot java 8 postman测试我的api和H2作为数据库,我想,我们可以创建一个包含参数的对象,而不是编写变量函数。@M.R.K我不知道你的意思。我已经编辑了我的帖子,并按照使用建议实现了,但我无法按预期调用我的repo类,我得到了空指针exception@M.R.K您的新版本与中描述的原始版本不同问题如果此答案有助于您提取查询参数,请接受我的答案,然后提出新问题。
@Entity
@Table(name="Hardware")
public class Hardware implements Serializable {
//Logger logger = (Logger) LoggerFactory.getLogger(Hardware.class);
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private long id;
@Column(name="audioJacks")
private String audioJacks;
@Column(name = "gps")
private String gps;
@Column(name = "battery")
private String battery;
//followed by setter and getter methods
}
@Entity
@Table(name="Releases")
public class Releases implements Serializable {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private long id;
@Column(name="priceEuro")
private long priceEuro;
@Column(name="announceDate")
private String announceDate;
//followed by setter and getter methods
}
enter code here
drop table if exists Hardware;
CREATE table Hardware (
id INT AUTO_INCREMENT PRIMARY KEY,
audioJacks varchar(200),
gps varchar(100),
battery varchar(200),
);
drop table if exists Releases;
create table If Not exists Releases (
id INT AUTO_INCREMENT PRIMARY KEY,
priceEuro int,
announceDate varchar(100),
);
drop table if exists Mobile;
CREATE TABLE If Not exists Mobile (
id INT AUTO_INCREMENT PRIMARY KEY,
brand VARCHAR(250),
phone VARCHAR(250),
picture VARCHAR(250),
sim VARCHAR(250),
resolution VARCHAR(250),
hardware_id int references Hardware(id),
releases_id int references Releases(id)
);
//followed by insert query first for releases,hardware and then for mobile
data is inserted as expected in db.
@GetMapping("/data/search")
public YourObject getData(@RequestParam(value = "name", required = false) String name,
@RequestParam(value = "amount", required = false) Integer amount){
...
}