Java .DefaultHandlerExceptionResolver我在上传数据时遇到这个错误,请帮助我解决这个项目,它在服务器上正常工作
联系人类作为将与address类链接的实体Java .DefaultHandlerExceptionResolver我在上传数据时遇到这个错误,请帮助我解决这个项目,它在服务器上正常工作,java,spring-boot,hibernate,spring-data-jpa,jackson,Java,Spring Boot,Hibernate,Spring Data Jpa,Jackson,联系人类作为将与address类链接的实体 package asmt1.demo.entity; import java.util.HashSet; import java.util.Set; import javax.persistence.CascadeType; import javax.persistence.Entity; import javax.persistence.EnumType; import javax.persistence.Enumerated; import
package asmt1.demo.entity;
import java.util.HashSet;
import java.util.Set;
import javax.persistence.CascadeType;
import javax.persistence.Entity;
import javax.persistence.EnumType;
import javax.persistence.Enumerated;
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.Table;
import com.fasterxml.jackson.annotation.JsonIgnoreProperties;
import asmt1.demo.dto.UserStatus;
//@Entity annotation specifies that the class is an entity and is mapped to a database table.
//@Table annotation specifies the name of the database table to be used for mapping
@Entity
@Table(name="Contactdetail")
public class Contact {
//@Id is used to specify the primary key
@Id
//Generated value is used to generate pk value ie. id to be autogenerated and assign identity column(id)
@GeneratedValue(strategy = GenerationType.AUTO)
private long id;
//@column is used to specify the column condition
//@Column( unique = true)
private String firstName;
private String lastName;
//@Column(unique = true)
private long contactNo;
private String mailId;
//list of named constant ie. status
@Enumerated(EnumType.STRING)
private UserStatus status;
//it is used to create one-to-one relationship between the contact and address table
//fetch type.lazy tells Hibernate to only fetch the related entities from the database when you use the relationship
@ManyToMany(fetch = FetchType.LAZY,cascade = CascadeType.ALL)
@JoinTable(name="conadd",
joinColumns = {@JoinColumn(name="id")},
inverseJoinColumns = {@JoinColumn(name="addid")})
//To handle the problem related to the serialization of the model using Jackson API when the model attributes have a lazy loading defined,
//we have to tell the serializer to ignore the chain or helpful garbage that Hibernate adds to classes, so it can manage lazy loading of data by declaring @JsonIgnoreProperties
@JsonIgnoreProperties({"hibernateLazyInitializer", "handler"})
private Set<Address> address=new HashSet<>();
//generate getters,setters, toString() and constructor using fields
public Contact() {}
public Contact(String firstName, String lastName, long contactNo, String mailId, UserStatus status,
Set<Address> address) {
super();
this.firstName = firstName;
this.lastName = lastName;
this.contactNo = contactNo;
this.mailId = mailId;
this.status = status;
this.address = address;
}
public Set<Address> getAddress() {
return address;
}
public void setAddress(Set<Address> address) {
this.address = address;
}
public long getId() {
return id;
}
public void setId(long id) {
this.id = id;
}
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 getContactNo() {
return contactNo;
}
public void setContactNo(long contactNo) {
this.contactNo = contactNo;
}
public String getMailId() {
return mailId;
}
public void setMailId(String mailId) {
this.mailId = mailId;
}
public UserStatus getStatus() {
return status;
}
public void setStatus(UserStatus status) {
this.status = status;
}
@Override
public String toString() {
return "Contact [id=" + id + ", firstName=" + firstName + ", lastName=" + lastName + ", contactNo=" + contactNo
+ ", mailId=" + mailId + "]";
}
}
控制器类
package asmt1.demo.controller;
import java.util.List;
import java.util.NoSuchElementException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Sort;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import asmt1.demo.converter.ContactConverter;
import asmt1.demo.dto.AddressReq;
import asmt1.demo.dto.ContactDto;
import asmt1.demo.entity.Contact;
import asmt1.demo.repository.AddressRepo;
import asmt1.demo.repository.ContactRepo;
//@restcontroller is used for restful services
@RestController
//@RequestMapping is used for creating baseurl for controller will be used
@RequestMapping("/contact")
public class ContactController {
//@Autowired will search the object of class
@Autowired
private ContactRepo ctrepo;
@Autowired
private AddressRepo addrepo;;
@Autowired
private ContactConverter converter;
//@Requestbody is used to map/bind methods with pojo pr value to return value to the web
//@postmapping is used to add data to database from web
@PostMapping("/add")
public List<Contact> newcontact(@RequestBody AddressReq req) {
return ctrepo.saveAll(req.getContact());
}
//@getmapping is used to get the details/records from database on web page
@GetMapping("/contactlist")
public List<Contact> getcontactlist(){
return ctrepo.findAll(Sort.by(Sort.Direction.ASC, "firstName","lastName"));
}
@GetMapping("/contactdto")
public List<ContactDto> getcontactlistdto(){
List<Contact> findAll=ctrepo.findAll();
return converter.entitytodto(findAll);
}
@GetMapping("/contactlist/{id}")
public ResponseEntity<Contact> get(@PathVariable Long id) {
try {
Contact contact = ctrepo.getOne(id);
return new ResponseEntity<Contact>(contact, HttpStatus.OK);
} catch (NoSuchElementException e) {
return new ResponseEntity<Contact>(HttpStatus.NOT_FOUND);
}
}
@GetMapping("/contactdto/{id}")
public ContactDto getbyid(@PathVariable Long id) {
Contact orElse=ctrepo.findById(id).orElse(null);
return converter.entitytodto(orElse);
}
@GetMapping("/orderlist")
public List<Contact> getcontactlistbyorder(){
return ctrepo.findAllByOrderByIdDesc();
}
@PostMapping("/save")
public ContactDto savedto(@RequestBody ContactDto dto) {
Contact contact=converter.dtotoentity(dto);
contact=ctrepo.save(contact);
return converter.entitytodto(contact);
}
//@deletemapping is used to delete the records/details from database by web page
@DeleteMapping("/delete/{id}")
public String deletebyid(@PathVariable long id){
if (ctrepo.findById(id)==null) {
return "Id not found.....Please enter correct id";
}
ctrepo.deleteById(id);
return "Successfully deleted "+id;
}
//@putmapping is used to change/update the records/details in database by web page
@PutMapping("/edit")
public List<Contact> editcontactbyid(@RequestBody AddressReq req ){
return ctrepo.saveAll(req.getContact());
}
}
在您的
AddressReq
类contact
设置为收款,但在您的付费负载中,您发送的是一个对象
,它应该是对象的收款
根据AddressReq
类,有效负载应为
{["contact":{
"firstName":"tomu",
"lastName":"shawn",
"contactNo":9124245,
"mailId":"ggia@gmail.com",
"status":"INACTIVE",
"address":{
"street1":"A/wing-24",
"street2":"plotno-4",
"city":"Mumbai",
"state":"Maharashtra",
"country":"India",
"zipcode":705
}
}]
}
或者,如果您的请求始终是联系人的单个条目,则您可以将联系人属性更改为单个实例,而不是
联系人的集合实例。它仅显示错误的请求
package asmt1.demo.dto;
//constant value for userstatus class
public enum UserStatus {
ACTIVE,INACTIVE
}
package asmt1.demo.controller;
import java.util.List;
import java.util.NoSuchElementException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Sort;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import asmt1.demo.converter.ContactConverter;
import asmt1.demo.dto.AddressReq;
import asmt1.demo.dto.ContactDto;
import asmt1.demo.entity.Contact;
import asmt1.demo.repository.AddressRepo;
import asmt1.demo.repository.ContactRepo;
//@restcontroller is used for restful services
@RestController
//@RequestMapping is used for creating baseurl for controller will be used
@RequestMapping("/contact")
public class ContactController {
//@Autowired will search the object of class
@Autowired
private ContactRepo ctrepo;
@Autowired
private AddressRepo addrepo;;
@Autowired
private ContactConverter converter;
//@Requestbody is used to map/bind methods with pojo pr value to return value to the web
//@postmapping is used to add data to database from web
@PostMapping("/add")
public List<Contact> newcontact(@RequestBody AddressReq req) {
return ctrepo.saveAll(req.getContact());
}
//@getmapping is used to get the details/records from database on web page
@GetMapping("/contactlist")
public List<Contact> getcontactlist(){
return ctrepo.findAll(Sort.by(Sort.Direction.ASC, "firstName","lastName"));
}
@GetMapping("/contactdto")
public List<ContactDto> getcontactlistdto(){
List<Contact> findAll=ctrepo.findAll();
return converter.entitytodto(findAll);
}
@GetMapping("/contactlist/{id}")
public ResponseEntity<Contact> get(@PathVariable Long id) {
try {
Contact contact = ctrepo.getOne(id);
return new ResponseEntity<Contact>(contact, HttpStatus.OK);
} catch (NoSuchElementException e) {
return new ResponseEntity<Contact>(HttpStatus.NOT_FOUND);
}
}
@GetMapping("/contactdto/{id}")
public ContactDto getbyid(@PathVariable Long id) {
Contact orElse=ctrepo.findById(id).orElse(null);
return converter.entitytodto(orElse);
}
@GetMapping("/orderlist")
public List<Contact> getcontactlistbyorder(){
return ctrepo.findAllByOrderByIdDesc();
}
@PostMapping("/save")
public ContactDto savedto(@RequestBody ContactDto dto) {
Contact contact=converter.dtotoentity(dto);
contact=ctrepo.save(contact);
return converter.entitytodto(contact);
}
//@deletemapping is used to delete the records/details from database by web page
@DeleteMapping("/delete/{id}")
public String deletebyid(@PathVariable long id){
if (ctrepo.findById(id)==null) {
return "Id not found.....Please enter correct id";
}
ctrepo.deleteById(id);
return "Successfully deleted "+id;
}
//@putmapping is used to change/update the records/details in database by web page
@PutMapping("/edit")
public List<Contact> editcontactbyid(@RequestBody AddressReq req ){
return ctrepo.saveAll(req.getContact());
}
}
{"contact":{
"firstName":"tomu",
"lastName":"shawn",
"contactNo":9124245,
"mailId":"ggia@gmail.com",
"status":"INACTIVE",
"address":{
"street1":"A/wing-24",
"street2":"plotno-4",
"city":"Mumbai",
"state":"Maharashtra",
"country":"India",
"zipcode":705
}}}
{["contact":{
"firstName":"tomu",
"lastName":"shawn",
"contactNo":9124245,
"mailId":"ggia@gmail.com",
"status":"INACTIVE",
"address":{
"street1":"A/wing-24",
"street2":"plotno-4",
"city":"Mumbai",
"state":"Maharashtra",
"country":"India",
"zipcode":705
}
}]
}