GET/data/search?
Вернет все записи
GET/data/search?name=jhon&add=US
Вернет конкретные записи в соответствии с запросом
GET/data/search? Amount=100
Вернет все записи, относящиеся к сумме 100
Клиент может вводить во время выше всех или некоторые параметры в соответствии с его требованиями.
Любые ссылки на ссылочный код также будут работать
ниже - моя ошибка, и реализация
, где мой метод репо не вызывается, вызывает метод сервиса и печатает sysoutдля контроллера и сервиса, но не для репо, что означает, что он не вызывает репо ниже, является выводом консоли: параметр запроса получен (sysout)
Метод внутреннего сервиса (sysout)
2019-10-15 13: 54: 30,185 ОШИБКА [http-nio-8080-exec-1] org.apache.juli.logging.DirectJDKLog: Servlet.service () для сервлета [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 file
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.