I am trying submit json data to a Spring MVC controller mapped with a model. Instead of getting the json values, the values of the fields of the model are all NULL.
IDE debugger:
Chrome:
Exception:
org.springframework.dao.InvalidDataAccessApiUsageException: The given id must not be null!; nested exception is java.lang.IllegalArgumentException: The given id must not be null!
Controller:
#RequestMapping(value = "/update", method = RequestMethod.POST)
#ResponseBody
public PostResponse update(Setting setting, BindingResult bindingResult) {
return settingService.processUpdate(setting, bindingResult, messageSource);
}
JSON data:
{
"updatedAt":1460600207000,
"id":1,
"createdBy":null,
"description":"This is a setting",
"code":"MY_SETTING",
"value":"{\"id\":\"1018\",\"title\":\"Another setting\",\"code\":\"220-203-10-101\"}"
}
Model:
#Entity
#JsonIgnoreProperties(ignoreUnknown = true)
public class Setting {
#Id
#GeneratedValue
#Column
private Integer id;
#Column(unique = true)
private String code;
#Column
private String description;
#Column
private String value;
#Temporal(TemporalType.TIMESTAMP)
#Column(nullable = false)
private Date createdAt;
#Temporal(TemporalType.TIMESTAMP)
#Column(nullable = false)
private Date updatedAt;
#NotFound(action = NotFoundAction.IGNORE)
#ManyToOne(fetch = FetchType.LAZY)
#JoinColumn(name="FK_createdByUserId")
private User createdBy;
public Setting() {}
public Setting(String code, String description, String value, Date createdAt, Date updatedAt, User createdBy) {
this.code = code;
this.description = description;
this.value = value;
this.createdAt = createdAt;
this.updatedAt = updatedAt;
this.createdBy = createdBy;
}
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
public String getCode() {
return code;
}
public void setCode(String code) {
this.code = code;
}
public String getDescription() {
return description;
}
public void setDescription(String description) {
this.description = description;
}
public String getValue() {
return value;
}
public void setValue(String value) {
this.value = value;
}
public Date getCreatedAt() {
return createdAt;
}
public void setCreatedAt(Date createdAt) {
this.createdAt = createdAt;
}
public Date getUpdatedAt() {
return updatedAt;
}
public void setUpdatedAt(Date updatedAt) {
this.updatedAt = updatedAt;
}
public User getCreatedBy() {
return createdBy;
}
public void setCreatedBy(User createdBy) {
this.createdBy = createdBy;
}
I guess that the setting bean is not mapped at all.
You need to tell spring how to map the http request to the method arguments. If you're posting data, the best way is to add #RequestBody annotation to the relevant method argument (setting in your case)
Modify your controller method like this:
#RequestMapping(value = "/update", method = RequestMethod.POST)
#ResponseBody
public PostResponse update(#RequestBody Setting setting, BindingResult bindingResult) {
return settingService.processUpdate(setting, bindingResult, messageSource);
}
Related
I have a one to one relation between two classes. I want to create phase items without having to insert candidate Id with it, because I get candidates afterwards so they basically don't exist.
Right now I'm getting the error:
could not execute statement; SQL [n/a]; constraint [null];
because i'm not sending candidateId with it.
This is the first class :
public class PhaseItems {
#Id
#GeneratedValue(strategy = GenerationType.IDENTITY)
#Column(name = "phaseI_id")
private long id;
private String PhaseItem;
#ManyToMany(fetch = FetchType.LAZY,
mappedBy = "items")
#JsonIgnore
private List<PhaseTemplate> template = new ArrayList<>();
#OneToOne(fetch = FetchType.LAZY, optional = true)
#JoinColumn(name = "candidate_id", nullable = true)
private Candidate candidate;
public PhaseItems() {
super();
}
public PhaseItems(long id, String phaseItem) {
super();
this.id = id;
PhaseItem = phaseItem;
}
public long getId() {
return id;
}
public void setId(long id) {
this.id = id;
}
public String getPhaseItem() {
return PhaseItem;
}
public void setPhaseItem(String phaseItem) {
PhaseItem = phaseItem;
}
public List<PhaseTemplate> getTemplate() {
return template;
}
public void setTemplate(List<PhaseTemplate> template) {
this.template = template;
}
public Candidate getCandidate() {
return candidate;
}
public void setCandidate(Candidate candidate) {
this.candidate = candidate;
}
#Override
public String toString() {
return "PhaseItems [id=" + id + ", PhaseItem=" + PhaseItem + ", template=" + template + "]";
}
This is the second class:
#Entity
#Table(name= "candidats")
public class Candidate {
#Id
#GeneratedValue(strategy = GenerationType.IDENTITY)
#Column(name = "candidate_id")
private long id;
private String fullname;
private String username;
#Column(nullable = false, unique = true, length = 45)
private String email;
private String adress;
private String phoneNumber;
private String password;
#Column(name = "status")
private String status;
#OneToOne(fetch = FetchType.LAZY,
cascade = CascadeType.ALL,
mappedBy = "candidate")
private PhaseItems phases;
public Candidate(){
}
public Candidate(String fullname,String username,String email, String adress, String phoneNumber, String password,
List<JobApplication> appliedJobs) {
super();
this.fullname = fullname;
this.username = username;
this.email = email;
this.adress = adress;
this.phoneNumber = phoneNumber;
this.password = password;
this.appliedJobs = appliedJobs;
}
public Candidate(String fullname,String username, String email, String adress, String phoneNumber, String password) {
super();
this.fullname = fullname;
this.username = username;
this.email = email;
this.adress = adress;
this.phoneNumber = phoneNumber;
this.password = password;
}
public long getId() {
return id;
}
public void setId(long id) {
this.id = id;
}
public String getFullname() {
return fullname;
}
public void setFullname(String fullname) {
this.fullname = fullname;
}
public String getUsername() {
return username;
}
public void setUsername(String username) {
this.username = username;
}
public String getEmail() {
return email;
}
public void setEmail(String email) {
this.email = email;
}
public String getAdress() {
return adress;
}
public void setAdress(String adress) {
this.adress = adress;
}
public String getPhoneNumber() {
return phoneNumber;
}
public void setPhoneNumber(String phoneNumber) {
this.phoneNumber = phoneNumber;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
public List<JobApplication> getAppliedJobs() {
return appliedJobs;
}
public void setAppliedJobs(List<JobApplication> appliedJobs) {
this.appliedJobs = appliedJobs;
}
public String getStatus() {
return status;
}
public void setStatus(String status) {
this.status = status;
}
Set column candidate_id as nullable in your database. Java attribute nullable = true of #JoinColumn annotation is ignored.
Is your column « candidate_id » from table phase_items nullable in your schema ?
When I try to build a table in Azure, it says "com.microsoft.sqlserver.jdbc.SQLServerException: Incorrect syntax near 'auto_increment'."
However, when I build in my local database, it's fine.
Can anyone help me with this? Thank you so much.
And I don't know where to fix the auto_increment rule, I didn't write it.
This is local driver I used before
spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver
This is my MS JDBC driver
spring.datasource.driver-class-name=com.microsoft.sqlserver.jdbc.SQLServerDriver
This is my table class
#Entity
#Table(name = "notes")
#EntityListeners(AuditingEntityListener.class)
#JsonIgnoreProperties(value = {"createdAt", "updatedAt"},
allowGetters = true)
public class Note implements Serializable {
#Id
#GeneratedValue(strategy = GenerationType.IDENTITY)
private Long id;
#NotBlank
private String title;
#NotBlank
private String content;
#Column(nullable = false, updatable = false)
#Temporal(TemporalType.TIMESTAMP)
#CreatedDate
private Date createdAt;
#Column(nullable = false)
#Temporal(TemporalType.TIMESTAMP)
#LastModifiedDate
private Date updatedAt;
public Long getId() {
return id;
}
public void setId(Long id) {
this.id = id;
}
public String getTitle() {
return title;
}
public void setTitle(String title) {
this.title = title;
}
public String getContent() {
return content;
}
public void setContent(String content) {
this.content = content;
}
public Date getCreatedAt() {
return createdAt;
}
public void setCreatedAt(Date createdAt) {
this.createdAt = createdAt;
}
public Date getUpdatedAt() {
return updatedAt;
}
public void setUpdatedAt(Date updatedAt) {
this.updatedAt = updatedAt;
}
}
spring.jpa.hibernate.dialect=org.hibernate.dialect.SQLServer2012Dialect
After I used this driver, it works. Thank you Greg!
angular controller
$http({
method: 'POST',
url: '/Eatery/save',
contentType:'application/json',
dataType:'json',
data:resvnCtrl.user
})
Spring mvc controller
#RequestMapping(value="/save",method=RequestMethod.POST,consumes=MediaType.APPLICATION_JSON_VALUE,produces=MediaType.APPLICATION_JSON_VALUE)
#ResponseBody
public int save(#RequestBody Reservation reservation) {
System.out.println(reservation.getTime());
return reservationRepo.save(reservation);
}
Java model
#Entity
#Table(name="reservations")
public class Reservation implements Serializable{
#Id
#GeneratedValue(strategy=GenerationType.AUTO)
private int id;
private String cnf;
private String name;
private String email;
private String phone;
#JsonDeserialize(using=CustomJsonDateDeserializer.class)
private LocalDateTime time;
private int seats;
private String note;
public Reservation() { }
public Reservation(String cnf, String name, String email, String phone,
LocalDateTime time, int seats, String note) {
this.cnf = cnf;
this.name = name;
this.email = email;
this.phone = phone;
this.time = time;
this.seats = seats;
this.note = note;
}
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getCnf() {
return cnf;
}
public void setCnf(String cnf) {
this.cnf = cnf;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getEmail() {
return email;
}
public void setEmail(String email) {
this.email = email;
}
public String getPhone() {
return phone;
}
public void setPhone(String phone) {
this.phone = phone;
}
public LocalDateTime getTime() {
return time;
}
public void setTime(LocalDateTime time) {
this.time = time;
}
public int getSeats() {
return seats;
}
public void setSeats(int seats) {
this.seats = seats;
}
public String getNote() {
return note;
}
public void setNote(String note) {
this.note = note;
}
}
From browser console
email: "kerhb#regerg.e"
name: "kjergk"
note: "wefwef"
phone: "1234567899"
seats: 2
time: "10/23/2015 5:53 PM"
Custom date deserializer
public class CustomJsonDateDeserializer extends JsonDeserializer<Date>
{
#Override
public Date deserialize(JsonParser jsonparser,
DeserializationContext deserializationcontext) throws IOException, JsonProcessingException {
SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm");
String date = jsonparser.getText();
try {
return format.parse(date);
} catch (ParseException e) {
throw new RuntimeException(e);
}
}
}
I have a bootstrap datetimepicker on UI and a java REST webservice at the backend. when i send date select, i got "The request sent by the client was syntactically incorrect.". the datetime string which is sent did not map to the java model. can someone spot my error
#Marged is rigth saying that you didn't cover AM/PM in your date pattern. The proper patter would be yyyy-MM-dd HH:mm a. Note also that you don't need a custom deserializer for this, can rather use #DateTimeFormat
#DateTimeFormat(pattern = "yyyy-MM-dd HH:mm a")
private LocalDateTime time;
I'm trying to post a simple model using Restful controllers in Spring Boot, but when I'd like my relationship objects it won't work.
I'm using the latest version of Spring
The Controller Method:
#RequestMapping(value= "rest/person/create", method = RequestMethod.POST)
public ResponseEntity<Person> createOrUpdate(#RequestBody Person person){
System.out.println("creating!");
try{
if (person != null){
peopleRepository.save(person);
}
}
catch (Exception e){
e.printStackTrace();
return new ResponseEntity<Person>(HttpStatus.UNAUTHORIZED);
}
return new ResponseEntity<Person>(HttpStatus.OK);
}
The Person class:
#Entity
public class Person {
#Id
#GeneratedValue
private Long id;
#Temporal(TemporalType.DATE)
#DateTimeFormat(pattern = "dd/MM/yyyy")
private Date registryDate;
#Temporal(TemporalType.DATE)
#DateTimeFormat(pattern = "dd/MM/yyyy")
private Date updateDate;
private String firstName;
private String lastName;
#Temporal(TemporalType.DATE)
#DateTimeFormat(pattern = "dd/MM/yyyy")
private Date birthDate;
private String gender;
private String rg;
private String cpf;
private String phone;
private String celPhone;
#ManyToOne
private Address address;
#Column(unique=true)
private String email;
private String observation;
byte situation;
protected Person(){}
public Person(Long id, Date registryDate, Date updateDate,
String firstName, String lastName, Date birthDate, String gender, String rg,
String cpf, String phone, Address address, String email,
String observation, byte situation) {
super();
this.id = id;
this.registryDate = registryDate;
this.updateDate = updateDate;
this.firstName = firstName;
this.lastName = lastName;
this.birthDate = birthDate;
this.gender = gender;
this.rg = rg;
this.cpf = cpf;
this.phone = phone;
if (address != null)
this.address = address;
this.email = email;
this.observation = observation;
this.situation = situation;
}
public String getFirstName() {
return firstName;
}
public void setFirstName(String firstName) {
this.firstName = firstName;
}
public String getLastName() {
return lastName;
}
public void setLastName(String lastName) {
this.lastName = lastName;
}
public Long getId() {
return id;
}
public void setId(Long id) {
this.id = id;
}
public Date getRegistryDate() {
return registryDate;
}
public void setRegistryDate(Date registryDate) {
this.registryDate = registryDate;
}
public Date getUpdateDate() {
return updateDate;
}
public void setUpdateDate(Date updateDate) {
this.updateDate = updateDate;
}
public Date getBirthDate() {
return birthDate;
}
public void setBirthDate(Date birthDate) {
this.birthDate = birthDate;
}
public Address getAddress() {
return address;
}
public void setAddress(Address address) {
this.address = address;
}
public String getGender() {
return gender;
}
public void setGender(String gender) {
this.gender = gender;
}
public String getRg() {
return rg;
}
public void setRg(String rg) {
this.rg = rg;
}
public String getCpf() {
return cpf;
}
public void setCpf(String cpf) {
this.cpf = cpf;
}
public String getPhone() {
return phone;
}
public void setPhone(String phone) {
this.phone = phone;
}
public String getEmail() {
return email;
}
public void setEmail(String email) {
this.email = email;
}
public String getObservation() {
return observation;
}
public void setObservation(String observation) {
this.observation = observation;
}
public byte getSituation() {
return situation;
}
public void setSituation(byte situation) {
this.situation = situation;
}
public String getCelPhone() {
return celPhone;
}
public void setCelPhone(String celPhone) {
this.celPhone = celPhone;
}
}
The JSON Object:
var person = {"firstName":"Joao","lastName":"Silva",
"gender":"Masculino","rg":"808080",
"cpf":"00","phone":"0","celPhone":"0",
"address":{"street":"Rua","number":5,"neighborhood":"Bairro",
"city":{"name":"Cidade",
"state":{"name":"Rio Grande do Sul","uf":"RS"},
"uf":"AE"},"cep":"Cep","complement":"Complemento"},
"email":"maluco#gmail.com","observation":"poisé","situation":0};
And the Angular.JS post ->
$http.post('rest/person/create', person).
success(function(data, status, headers, config) {
console.log('all good!');
$scope.create = true;
}).
error(function(data, status, headers, config) {
console.log('error');
$scope.create = false;
});
I've been looking how to do it, and could'nt find it.
I'm completely beginner with Spring
The error happens only with the relationships, if I remove
"address":{"street":"Rua","number":5,"neighborhood":"Bairro",
"city":{"name":"Cidade",
"state":{"name":"Rio Grande do Sul","uf":"RS"},
"uf":"AE"},"cep":"Cep","complement":"Complemento"}
It will work.
You need to declare Content-Type when you post a request, and then #RequesetBody annotation convert http body text to a JSON object using jackson.
So you need to change your content-types, and stringify your json object.
$http({
url: 'rest/person/create',
dataType: 'json',
method: 'POST',
data: JSON.stringify(persion),
headers: {
"Content-Type": "application/json"
}
}).success(function(response){
$scope.response = response;
}).error(function(error){
$scope.error = error;
});
I am trying to get the user's friends list from Facebook.
The problem seems to be the Javabean...
FBUser fbuser = new Gson().fromJson(jsonStr, FBUser.class);
public class FBUser implements Serializable {
private static final long serialVersionUID = -3154429420153433117L;
private String id;
private String name;
private String email;
private Friends friendsList = new Friends();
private FBUser() { }
public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getEmail() {
return email;
}
public void setEmail(String email) {
this.email = email;
}
public List<Data> getFriendsList() {
return friendsList.getData();
}
public static class Friends implements Serializable {
private static final long serialVersionUID = 6991758772193514527L;
private List<Data> data;
private Friends() { }
public List<Data> getData() {
return data;
}
public void setData(List<Data> data) {
this.data = data;
}
public class Paging implements Serializable {
private static final long serialVersionUID = 1689816298710621080L;
private String next;
private Paging() { }
public String getNext() {
return next;
}
public void setNext(String next) {
this.next = next;
}
}
}
public class Data implements Serializable {
private static final long serialVersionUID = -5008541658519841090L;
private String id;
private String name;
private Data() { }
public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
}
}
Json:
json: {"id":"10861234","name":"Whatever","email":"whatever\u0040gmail.com","friends":{"data":[{"name":"Someone","id":"10861234"},{"name" ...43"}],"paging":{"next":"https:\/\/graph.facebook.com\/10861234\/friends..."}}}
The fields ID, Name and Email I can retrieve succesfully... but the friendsList is null... =(
Maybe it is the way I am trying to get it from the nested class, any suggestions on that?
There is no friendsList in your JSON (or, there's no friends in your Java class - whichever way you'd like to look at it). Gson silently ignores anything in the JSON that is not present in your classes.
You have a field friends whose value is an object. That object has a field data which is an array of objects and a field paging which is another object.
You need to write Java classes that match that structure. You're ... close.
In your FBUser class change:
private Friends friendsList = new Friends();
to:
private Friends friends = new Friends();
or:
#SerializedName("friends")
private Friends friendsList = new Friends();
Then in your Friends class you need to add:
private Paging paging = new Paging();
Also note that you don't have to initialize these values unless you specifically don't want them to be non-null when using these classes elsewhere.