![](/img/trans.png)
[英]Can we make a GET API call request which has multiple headers as a URL in browser
[英]How to build microservice GET api in springboot which can take single or multiple parameters as insert in Request url and fetch the data as request
GET/data/search?
將返回所有記錄
GET/data/search?name=jhon&add=US
將根據請求返回特定記錄
GET/data/search? Amount=100
GET/data/search? Amount=100
將返回特定於金額 100 的所有記錄
客戶可以根據自己的要求,隨時輸入全部或部分參數。
任何指向參考代碼的鏈接也可以
下面是我的錯誤和實現
我的 repo 方法沒有被調用,它正在調用 service 方法並打印 controller 和 service 但不是 repo 的 sysout,這意味着它沒有調用 repo 下面是 consol output: Request param received (sysout)
內部服務方法(sysout)
2019-10-15 13:54:30,185 錯誤 [http-nio-8080-exec-1] org.apache.juli.logging.DirectJDKLog: Servlet.service() for servlet [dispatcherServlet] 在上下文中與路徑 [] 引發異常[請求處理失敗; 嵌套異常是 java.lang.NullPointerException] 根本原因 java.lang.NullPointerException: null
@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;
}
}
服務
@Service
public class MobileService {
public List<MobileResponse> getAllMobiles(Map<String, String> map) {
List<MobileResponse> mobilResponseList = new ArrayList<>();
System.out.println("Inside service method");
MobileRepoIntf repo=null;
mobilResponseList = repo.findAllMobiles(map);
return mobilResponseList;
}
}
回購接口
@Repository
public interface MobileRepoIntf{
List<MobileResponse> findAllMobiles(Map<String, String> map);
}
回購實施
public class MobileRepository implements MobileRepoIntf {
@PersistenceContext
private EntityManager em;
@SuppressWarnings("uncheaked")
@Override
public List<MobileResponse> findAllMobiles(Map<String, String> map) {
System.out.println("inside Repo method ");
String query = "select m.id,h.id, r.id from
com.axiomtelecom.assignment.entities.Mobile m,
com.axiomtelecom.assignment.entities.Hardware
h,com.axiomtelecom.assignment.entities.Releases r where m.hardware_id =
h.id AND m.releases_id = r.id";
List<MobileResponse> mobileList = new ArrayList();
Query qry = em.createQuery(query);
System.out.println("Query is "+qry);
return qry.getResultList();
}
}
移動實體
@Entity
@Table(name = "MOBILE")
public class Mobile implements Serializable {
//Logger logger = (Logger) LoggerFactory.getLogger(Mobile.class);
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private long id;
@Column(name = "brand")
private String brand;
@Column(name = "phone")
private String phone;
@Column(name = "picture")
private String picture;
@Column(name = "sim")
private String sim;
@Column(name = "resolution")
private String resolution;
//@OneToOne(cascade = CascadeType.ALL)
@OneToOne
@JoinColumn(name ="id")
private Hardware hardware;
@OneToOne
@JoinColumn(name="id")
private Releases releases;
//followed by setter and getter methods
}
硬件實體
@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")
public ResponseEntity<Foo> getFoo(@RequestParam(value = "name", required = false) String name,
@RequestParam(value = "amount", required = false) Integer amount) {
...
}
您可以將所有參數作為Map<String, String>
接收:
@GetMapping("/foo")
public ResponseEntity<Foo> getFoo(@RequestParam Map<String, String> parameters) {
...
}
或者您可以定義一個 class 來表示參數並接收它的實例作為方法參數:
@Data
public class FooQueryParameters {
private String name;
private Integer amount;
}
@GetMapping("/foo")
public ResponseEntity<Foo> getFoo(FooQueryParameters parameters) {
...
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.