Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
menu search
person
Welcome To Ask or Share your Answers For Others

Categories

I am trying to make a shopping app When I am trying to save a Product and make an ajax request to my Save product controller the controller is not being called and I am getting a 400(Bad Request) error on Browser Console.

MainControllerCode:

@Controller
@RequestMapping("/")
public class MainController {
    private static final Logger logger = LogManager.getLogger(MainController.class.getName());

    public static String loginPage = "login/login";
    public static String signUpPage = "login/signUp";


    ProductService productServ;
    UserService userService;

    @RequestMapping("/")
    public String viewPage() {
        return "landing";
    }

    @RequestMapping("/login")
    public String viewLoginPage() {
        return loginPage;
    }

    @RequestMapping("/Register")
    public String viewRegisterPage() {
        return signUpPage;
    }


    @RequestMapping(path= "/ValidateLogin" , method = RequestMethod.POST)
    public String ValidateLogin(@ModelAttribute("loginUser")UserDTO userDto, HttpSession session,BindingResult errorList) {
        String returnPath="";
        userService = new UserServiceImpl(); 
        try {
//          if(session.getAttribute("loggedInUser") == null){
            if(Utils.isNotNull(userDto.getEmail())) {
                if(Utils.isNotNull(userDto.getPassword())) {
                    UserModel user = userService.validateLogin(userDto, null, errorList);
                    if(!errorList.hasErrors()) {
                        user = userService.validateLogin(userDto, null, errorList);
                        if(!errorList.hasErrors()) {
                            session.setAttribute("loggedInUser", user);
                            
                            if(user.getRole() == Constants.UserRole.ADMIN) {
                                returnPath = "Admin";
                            }else if(user.getRole() == Constants.UserRole.STORE_MANAGER) {
                                returnPath = "StoreOwner";
                            }else {
                                returnPath = "landing";
                            }
                        }else {
                            returnPath = loginPage;
                        }
                    }else {
                        returnPath =loginPage;
                    }
                }else {
                    returnPath =loginPage;
                    errorList.addError(new ObjectError("error","Please Enter the Password!!"));
                }
            }else {
                returnPath =loginPage;
                errorList.addError(new ObjectError("error","Email is Required"));
                }
//          }else {
//              logger.info("You are already logged in!! ");
//          }
        }catch(Exception e) {
            e.printStackTrace(); 
            errorList.addError(new ObjectError("error", "Somthing went wrong please try again"));
        }
        logger.info("--------------------Logged in USer is -------------------   : "+session.getAttribute("loggedInUser"));
        return returnPath;
    }

    @ResponseBody
    @RequestMapping(path="/saveUser" , method = RequestMethod.POST)
    public APIResponseModel saveUser(@ModelAttribute UserModel user , HttpSession session , BindingResult errorList) {
        APIResponseModel apiResponseModel = new Utils().getDefaultApiResponse();
        try {
            if(Utils.isNotNull(user)) {
                userService = new UserServiceImpl();
                userService.SaveUpdateUser(user, null, errorList);
                if(!errorList.hasErrors()) {
                    session.setAttribute("loggedInUser", user);
                    apiResponseModel.setStatus(HttpStatus.OK);
                    apiResponseModel.setMessage("Saved Successfully");
                    apiResponseModel.setData(user.toString());
                }else {
                    apiResponseModel.setMessage("Got Error for "+ errorList);
                }
            }else {
                errorList.addError(new ObjectError("error", "Please Enter All the Mandatory Fields "));
                apiResponseModel.setMessage("Please Enter All the Mandatory Fields");
            }
        }catch(Exception e) {
            e.printStackTrace();
            logger.info("Exception Occured because of : "+ e.getMessage());
        }
        logger.info(apiResponseModel);
        return apiResponseModel;
    }

    @ResponseBody
    @RequestMapping(path="/saveProduct", method = RequestMethod.POST , consumes = {"multipart/form-data"})
    public APIResponseModel saveProduct(@ModelAttribute ProductDTO productDto, HttpSession session) {
        logger.info("I am called !!");
        APIResponseModel apiResponseModel = new Utils().getDefaultApiResponse();
        UserModel user = new UserModel();
        BindingResult errorList = new DataBinder(new Object()).getBindingResult();
        logger.info("------------------------------------Logged In user info----------------------------------------------------");
        logger.info(user);
        logger.info("----------------------------------------------------------------------------------------");

        if(Utils.isNotNull(productDto)) {
            productServ = new ProductServiceImpl();
            productServ.SaveProduct(productDto, user, null, errorList);
            if(!errorList.hasErrors()) {
                apiResponseModel.setStatus(HttpStatus.OK);
                apiResponseModel.setData(productDto.toString());
                apiResponseModel.setMessage("Product Saved Successfully !!");
            }else {
                errorList.addError(new ObjectError("error", "Please fill the mandatory fields"));
            }

        }else {
            errorList.addError(new ObjectError("error", "Please fill the mandatory fields"));
        }

        return apiResponseModel;
    }

Code for EntityClass

@Entity
@Table(name="product_clothes")
public class ProductModal {

    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    @Column(name="id")
    private Integer id;
    @Column(name="brand")
    private String brand;
    @Column(name="gender")
    private Integer gender;
    @Column(name="price")
    private Double price;
    @Column(name="quantity")
    private Integer quantity;
    @Column(name="addedAt")
    private Timestamp addedAt;
//  @Column(name="added_By")
    @ManyToOne(cascade = CascadeType.ALL, fetch = FetchType.LAZY)
    @JoinColumn(name="added_By",referencedColumnName = "id")
    private UserModel addedBy;  
    @Column(name="type")
    private Integer type;
    @Column(name="description")
    private String description;
    @Column(name="main_display_pic")
    private String mainPic;
    @Column(name="present_in_market")
    private Integer market;
    
    public ProductModal() {
        
    }

    public ProductModal(String brand, Integer gender, Double price, Integer quantity, Timestamp addedAt,
            UserModel addedBy, Integer type, String description, String mainPic, Integer market) {
        this.brand = brand;
        this.gender = gender;
        this.price = price;
        this.quantity = quantity;
        this.addedAt = addedAt;
        this.addedBy = addedBy;
        this.type = type;
        this.description = description;
        this.mainPic = mainPic;
        this.market = market;
    }


    public Integer getId() {
        return id;
    }

    public void setId(Integer id) {
        this.id = id;
    }

    public Integer getType() {
        return type;
    }

    public void setType(Integer type) {
        this.type = type;
    }

    public Integer getGender() {
        return gender;
    }

    public void setGender(Integer gender) {
        this.gender = gender;
    }

    public Integer getQuantity() {
        return quantity;
    }

    public void setQuantity(Integer quantity) {
        this.quantity = quantity;
    }

    public String getMainPic() {
        return mainPic;
    }

    public void setMainPic(String mainPic) {
        this.mainPic = mainPic;
    }

    public String getBrand() {
        return brand;
    }

    public void setBrand(String brand) {
        this.brand = brand;
    }

    public Double getPrice() {
        return price;
    }

    public void setPrice(Double price) {
        this.price = price;
    }

    public UserModel getAddedBy() {
        return addedBy;
    }

    public void setAddedBy(UserModel addedBy) {
        this.addedBy = addedBy;
    }

    public Timestamp getAddedAt() {
        return addedAt;
    }

    public void setAddedAt(Timestamp addedAt) {
        this.addedAt = addedAt;
    }
    public String getDescription() {
        return description;
    }

    public void setDescription(String description) {
        this.description = description;
    }

    public Integer getMarket() {
        return market;
    }


    public void setMarket(Integer market) {
        this.market = market;
    }

    @Override
    public String toString() {
        return "{"id":"" + id + "", "brand":"" + brand + "", "gender":"" + gender + "", "price":""
                + price + "", "quantity":"" + quantity + "", "addedAt":"" + (Hibernate.isInitialized(addedBy) ? addedBy : null) + "", "addedBy":""
                + addedBy + "", "type":"" + type + "", "description":"" + description + "", "mainPic":""
                + mainPic + "", "market":"" + market + ""}";
    }

    
}

Code For DTO

public class ProductDTO {

    private Integer id;
    private Integer type;
    private Integer gender;
    private Integer quantity;
    private String brand;
    private Double price;
    private UserModel addedBy;
    private String description;
    private Integer market;
    private MultipartFile mainPic;
    private List<MultipartFile> additionalImages;
    

    
    
    public Integer getId() {
        return id;
    }
    public void setId(Integer id) {
        this.id = id;
    }
    public Integer getType() {
        return type;
    }
    public void setType(Integer type) {
        this.type = type;
    }
    public Integer getGender() {
        return gender;
    }
    public void setGender(Integer gender) {
        this.gender = gender;
    }
    public Integer getQuantity() {
        return quantity;
    }
    public void setQuantity(Integer quantity) {
        this.quantity = quantity;
    }
    public MultipartFile getMainPic() {
        return mainPic;
    }
    public void setMainPic(MultipartFile mainPic) {
        this.mainPic = mainPic;
    }
    public String getBrand() {
        return brand;
    }
    public void setBrand(

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
159 views
Welcome To Ask or Share your Answers For Others

1 Answer

等待大神答复

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
...