I'm trying to develop a cross-platform application, with Cordova in the front-end and using Spring in the back_end. Basically, I started by the link between the database and generating the classes (models). Here's my application properties:
# Keep the connection alive if idle for a long time (needed in production)
spring.datasource.testWhileIdle = true
spring.datasource.validationQuery = SELECT 1
# Show or not log for each sql query
spring.jpa.show-sql = true
# Hibernate ddl auto (create, create-drop, update)
spring.jpa.hibernate.ddl-auto = updateUnable to find column with logical name: program_id
# Naming strategy
spring.jpa.hibernate.naming-strategy = org.hibernate.cfg.ImprovedNamingStrategy
# Use spring.jpa.properties.* for Hibernate native properties (the prefix is
# stripped before adding them to the entity manager)
# The SQL dialect makes Hibernate generate better SQL for the chosen database
spring.jpa.properties.hibernate.dialect = org.hibernate.dialect.MySQL5Dialect
# Server
server.port=8080
endpoints.cors.allowed-origins=*
I wanted to try if I'm getting the list in JSON form before I go to the backend. So I made an example to get the list of operators, the class Operator is declared like that:
/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
package com.rest.model;
import java.io.Serializable;
import java.util.List;
import javax.persistence.Basic;
import javax.persistence.CascadeType;
import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.JoinTable;
import javax.persistence.ManyToMany;
import javax.persistence.NamedQueries;
import javax.persistence.NamedQuery;
import javax.persistence.OneToMany;
import javax.persistence.Table;
import javax.validation.constraints.NotNull;
import javax.validation.constraints.Size;
import javax.xml.bind.annotation.XmlRootElement;
import javax.xml.bind.annotation.XmlTransient;
/**
*
* @author gate11
*/
@Entity
@Table(catalog = "db_suivi", schema = "")
@XmlRootElement
@NamedQueries({
@NamedQuery(name = "Operator.findAll", query = "SELECT o FROM Operator o")
, @NamedQuery(name = "Operator.findById", query = "SELECT o FROM Operator o WHERE o.id = :id")
, @NamedQuery(name = "Operator.findByOperatorFName", query = "SELECT o FROM Operator o WHERE o.operatorFName = :operatorFName")
, @NamedQuery(name = "Operator.findByOperatorLName", query = "SELECT o FROM Operator o WHERE o.operatorLName = :operatorLName")})
public class Operator implements Serializable {
private static final long serialVersionUID = 1L;
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
@Basic(optional = false)
private Long id;
@Basic(optional = false)
@NotNull
@Size(min = 1, max = 100)
private String operatorFName;
@Basic(optional = false)
@NotNull
@Size(min = 1, max = 100)
private String operatorLName;
@JoinTable(name = "BadgeOperator", joinColumns = {
@JoinColumn(name = "idOp", referencedColumnName = "id")}, inverseJoinColumns = {
@JoinColumn(name = "idBad", referencedColumnName = "id")})
@ManyToMany(fetch = FetchType.LAZY)
private List<BadgePoint> badgePointList;
@ManyToMany(mappedBy = "operatorList", fetch = FetchType.LAZY)
private List<Zone> zoneList;
@OneToMany(cascade = CascadeType.ALL, mappedBy = "operator", fetch = FetchType.LAZY)
private List<TimePass> timePassList;
public Operator() {
}
public Operator(Long id) {
this.id = id;
}
public Operator(Long id, String operatorFName, String operatorLName) {
this.id = id;
this.operatorFName = operatorFName;
this.operatorLName = operatorLName;
}
public Long getId() {
return id;
}
public void setId(Long id) {
this.id = id;
}
public String getOperatorFName() {
return operatorFName;
}
public void setOperatorFName(String operatorFName) {
this.operatorFName = operatorFName;
}
public String getOperatorLName() {
return operatorLName;
}
public void setOperatorLName(String operatorLName) {
this.operatorLName = operatorLName;
}
@XmlTransient
public List<BadgePoint> getBadgePointList() {
return badgePointList;
}
public void setBadgePointList(List<BadgePoint> badgePointList) {
this.badgePointList = badgePointList;
}
@XmlTransient
public List<Zone> getZoneList() {
return zoneList;
}
public void setZoneList(List<Zone> zoneList) {
this.zoneList = zoneList;
}
@XmlTransient
public List<TimePass> getTimePassList() {
return timePassList;
}
public void setTimePassList(List<TimePass> timePassList) {
this.timePassList = timePassList;
}
@Override
public int hashCode() {
int hash = 0;
hash += (id != null ? id.hashCode() : 0);
return hash;
}
@Override
public boolean equals(Object object) {
// TODO: Warning - this method won't work in the case the id fields are not set
if (!(object instanceof Operator)) {
return false;
}
Operator other = (Operator) object;
if ((this.id == null && other.id != null) || (this.id != null && !this.id.equals(other.id))) {
return false;
}
return true;
}
@Override
public String toString() {
return "com.rest.Operator[ id=" + id + " ]";
}
}
I added an interface (repository) implementing JpaRepository:
/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
package com.rest.repository;
import com.rest.model.Operator;
import java.util.List;
import org.springframework.data.jpa.repository.JpaRepository;
/**
*
* @author gate11
*/
public interface OperatorRepository extends JpaRepository<Operator, Long>{
@Override
List<Operator> findAll();
}
And as needed, also a controller for the class Operator:
package com.rest.rest;
import com.rest.model.Operator;
import com.rest.service.OperatorService;
import java.util.List;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.RequestMapping;
import static org.springframework.web.bind.annotation.RequestMethod.GET;
import org.springframework.web.bind.annotation.RestController;
@RestController
@RequestMapping(value = "/api", produces = MediaType.APPLICATION_JSON_VALUE)
public class OperatorController {
@Autowired
private OperatorService operatorService;
@CrossOrigin
@RequestMapping(method = GET, value = "/operator/all")
public List<Operator> loadAll() {
return this.operatorService.findAll();
}
}
The interface service:
package com.rest.service;
import com.rest.model.Operator;
import java.util.List;
/**
*
* @author gate11
*/
public interface OperatorService {
List<Operator> findAll();
}
The service Implement also:
package com.rest.serviceImp;
import com.rest.model.Operator;
import com.rest.repository.OperatorRepository;
import com.rest.service.OperatorService;
import java.util.List;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
/**
*
* @author gate11
*/
@Service
public class OperatorServiceImp implements OperatorService{
@Autowired
private OperatorRepository operatorRepository;
@Override
public List<Operator> findAll() {
List<Operator> operator = operatorRepository.findAll();
return operator;
}
}
When I launch the application, the log:
2018-03-05 14:34:47.427 INFO 10768 --- [ restartedMain] o.s.s.web.DefaultSecurityFilterChain : Creating filter chain: OrRequestMatcher [requestMatchers=[Ant [pattern='/css/**'], Ant [pattern='/js/**'], Ant [pattern='/images/**'], Ant [pattern='/webjars/**'], Ant [pattern='/**/favicon.ico'], Ant [pattern='/error']]], []
2018-03-05 14:34:47.565 INFO 10768 --- [ restartedMain] o.s.s.web.DefaultSecurityFilterChain : Creating filter chain: Ant [pattern='/h2-console/**'], [org.springframework.security.web.context.request.async.WebAsyncManagerIntegrationFilter@183bb809, org.springframework.security.web.context.SecurityContextPersistenceFilter@593fdbbe, org.springframework.security.web.header.HeaderWriterFilter@2d784ff4, org.springframework.security.web.authentication.logout.LogoutFilter@55ef8d65, org.springframework.security.web.authentication.www.BasicAuthenticationFilter@3ba7a547, org.springframework.security.web.savedrequest.RequestCacheAwareFilter@12e1efcb, org.springframework.security.web.servletapi.SecurityContextHolderAwareRequestFilter@57449117, org.springframework.security.web.authentication.AnonymousAuthenticationFilter@5cba96fc, org.springframework.security.web.session.SessionManagementFilter@5403d62a, org.springframework.security.web.access.ExceptionTranslationFilter@4817a878, org.springframework.security.web.access.intercept.FilterSecurityInterceptor@36126980]
2018-03-05 14:34:47.573 INFO 10768 --- [ restartedMain] o.s.s.web.DefaultSecurityFilterChain : Creating filter chain: OrRequestMatcher [requestMatchers=[Ant [pattern='/**']]], [org.springframework.security.web.context.request.async.WebAsyncManagerIntegrationFilter@70c855ab, org.springframework.security.web.context.SecurityContextPersistenceFilter@1eef0aa6, org.springframework.security.web.header.HeaderWriterFilter@537d0675, org.springframework.security.web.authentication.logout.LogoutFilter@7d1011c1, org.springframework.security.web.authentication.www.BasicAuthenticationFilter@1b4c8eff, org.springframework.security.web.savedrequest.RequestCacheAwareFilter@73c2d36e, org.springframework.security.web.servletapi.SecurityContextHolderAwareRequestFilter@48372dcd, org.springframework.security.web.authentication.AnonymousAuthenticationFilter@3cfcbfa3, org.springframework.security.web.session.SessionManagementFilter@4befcfad, org.springframework.security.web.access.ExceptionTranslationFilter@4ed4729f, org.springframework.security.web.access.intercept.FilterSecurityInterceptor@dffb1e0]
2018-03-05 14:34:47.807 INFO 10768 --- [ restartedMain] o.s.b.d.a.OptionalLiveReloadServer : LiveReload server is running on port 35729
2018-03-05 14:34:47.900 INFO 10768 --- [ restartedMain] o.s.j.e.a.AnnotationMBeanExporter : Registering beans for JMX exposure on startup
2018-03-05 14:34:48.000 INFO 10768 --- [ restartedMain] s.b.c.e.t.TomcatEmbeddedServletContainer : Tomcat started on port(s): 8080 (http)
2018-03-05 14:34:48.008 INFO 10768 --- [ restartedMain] com.rest.Application : Started Application in 10.365 seconds (JVM running for 11.067)
2018-03-05 14:42:28.257 INFO 10768 --- [nio-8080-exec-1] o.a.c.c.C.[Tomcat].[localhost].[/] : Initializing Spring FrameworkServlet 'dispatcherServlet'
2018-03-05 14:42:28.257 INFO 10768 --- [nio-8080-exec-1] o.s.web.servlet.DispatcherServlet : FrameworkServlet 'dispatcherServlet': initialization started
2018-03-05 14:42:28.279 INFO 10768 --- [nio-8080-exec-1] o.s.web.servlet.DispatcherServlet : FrameworkServlet 'dispatcherServlet': initialization completed in 21 ms
Then, I should go to localhost:8080/api/operator/all to get the list of operator as I used to do, but it's asking me to use a username password, I tried everything from db user (root, root) but doesn't work. I don't even have a connexion page in the application.
If you have any idea, please feel free to suggest, I'm using a Mac as you see. Thanks.