简体   繁体   English

Reactjs/Java Spring Boot - RequestMethod.POST - 空值

[英]Reactjs/Java Spring Boot - RequestMethod.POST - null values

I am using reactjs and Java Spring Boot.我正在使用 reactjs 和 Java Spring Boot。 I am trying to build an admin panel - that will use a POST instead of GET request - to update site panel data.我正在尝试构建一个管理面板 - 将使用 POST 而不是 GET 请求 - 来更新站点面板数据。

I had been using GET - as I couldn't get this to work, but now the data is more complex I feel I have to try and get it work as a POST.我一直在使用 GET - 因为我无法让它工作,但现在数据更复杂,我觉得我必须尝试让它作为 POST 工作。 I am sure the axios part is pushing data to the server - I can see it in the payload - but when I system print out the params they come up as null?我确定 axios 部分正在将数据推送到服务器 - 我可以在有效负载中看到它 - 但是当我系统打印出参数时,它们会显示为空?

the reactjs action reactjs 动作

import axios from 'axios';

import CONFIG from './_configApi';//add config api

import { fetchInitPane } from './initPaneAction';

export const FETCH_EDIT_PANE_SUCCESS = 'FETCH_EDIT_PANE_SUCCESS'
export const FETCH_EDIT_PANE_FAILURE = 'FETCH_EDIT_PANE_FAILURE'

export function editPaneSuccess(response) {
  return {
    type: FETCH_EDIT_PANE_SUCCESS,
    payload: response
  }
}

export function editPaneFail(response) {
  return {
    type: FETCH_EDIT_PANE_FAILURE,
    payload: response
  }
}

export function fetchEditPane(data) {
  let url = CONFIG.EDIT_PANE_API;
  return function (dispatch) {     
   /*axios.get(url, {
      params: data
    })*/
    axios.post(url, data)
      .then(function (response) {

      response = null;

        dispatch(editPaneSuccess(response));      
      dispatch(fetchInitPane(null));
      })
      .catch(function (error) {
        dispatch(editPaneFail(error));
      });
  }
}

but my problem lies in the Java method.但我的问题在于 Java 方法。

//api/editPane
@RequestMapping(value = {"/api/editPane"}, method = RequestMethod.POST)
@CrossOrigin(origins = {"*"})
public ResponseEntity<?> editpane(
        @RequestParam(value="tile1", required=false) String tile1,
        @RequestParam(value="tile2", required=false) String tile2,
        @RequestParam(value="about", required=false) String about,
        @RequestParam(value="privacy", required=false) String privacy           
        //HttpServletRequest request
        ) throws Exception {

            JSONObject loggedUser = getLoggedInUser();
            String role = (String) loggedUser.get("role");
            //check to make sure they are an admin user - this is sensitive data
            //System.out.println("role"+ role);

            System.out.println("tile1"+ tile1);
            System.out.println("tile2"+ tile2);
            System.out.println("about"+ about);
            System.out.println("privacy"+ privacy);


            if(role.equals("1")){
                //create api admin instance
                //AdminModel myApiAdmin = new AdminModel(); 

                //find matching row
                Long id = (long) 0;
                TblSitePages sitePages = tblSitePagesRepository.findById(id);

                //tile1
                if(tile1 != sitePages.getTile1()){
                    //sitePages.setTile1(tile1);
                }           
                //tile2
                if(tile2 != sitePages.getTile2()){
                    //sitePages.setTile2(tile2);
                }   
                //about
                if(about != sitePages.getAbout()){
                    sitePages.setAbout(about);
                }                   
                //privacy
                if(privacy != sitePages.getPrivacy()){
                    sitePages.setPrivacy(privacy);
                }

                tblSitePagesRepository.saveAndFlush(sitePages);

                JSONObject response = ResponseWrapper(null, "success", "Updating site pane data");
                return new ResponseEntity<>(response, HttpStatus.OK);                   
            } else{

                JSONObject response = ResponseWrapper(null, "error", "Not an admin");
                return new ResponseEntity<>(response, HttpStatus.OK);
            }
}   

The solution to this - is the following.对此的解决方案 - 如下。

In the JS side for axios - use POST.在 axios 的 JS 端 - 使用 POST。

import axios from 'axios';

import CONFIG from './_configApi';//add config api

import { fetchInitPane } from './initPaneAction';

export const FETCH_EDIT_PANE_SUCCESS = 'FETCH_EDIT_PANE_SUCCESS'
export const FETCH_EDIT_PANE_FAILURE = 'FETCH_EDIT_PANE_FAILURE'

export function editPaneSuccess(response) {
  return {
    type: FETCH_EDIT_PANE_SUCCESS,
    payload: response
  }
}

export function editPaneFail(response) {
  return {
    type: FETCH_EDIT_PANE_FAILURE,
    payload: response
  }
}

export function fetchEditPane(data) {
  let url = CONFIG.EDIT_PANE_API;
  return function (dispatch) {     
    axios.post(url, data)
      .then(function (response) {

      response = null;

        dispatch(editPaneSuccess(response));      
      dispatch(fetchInitPane(null));
      })
      .catch(function (error) {
        dispatch(editPaneFail(error));
      });
  }
}

on the Java side - pick up the variables via the following.在 Java 方面 - 通过以下方式获取变量。

  1. create a model that maps the variables创建一个映射变量的模型

- ——

package controller;

    public class EditPane {
        private String tile1;
        private String tile2;
        private String about;
        private String privacy;
        private String terms;

        public String getTile1() {
            return tile1;
        }
        public void setTile1(String tile1) {
            this.tile1 = tile1;
        }


        public String getTile2() {
            return tile2;
        }
        public void setTile2(String tile2) {
            this.tile2 = tile2;
        }

        public String getAbout() {
            return about;
        }
        public void setAbout(String about) {
            this.about = about;
        }


        public String getPrivacy() {
            return privacy;
        }
        public void setPrivacy(String privacy) {
            this.privacy = privacy;
        }

        public String getTerms() {
            return terms;
        }
        public void setTerms(String terms) {
            this.terms = terms;
        }
    }

then reconfigure the mapping as follows然后重新配置映射如下

//api/editPane
@RequestMapping(value = {"/api/editPane"}, method = RequestMethod.POST)
@CrossOrigin(origins = {"*"})
public ResponseEntity<?> editpane(
        @RequestBody EditPane editPane
        ) throws Exception {
            String tile1 = editPane.getTile1();
            String tile2 = editPane.getTile2();
            String about = editPane.getAbout();
            String privacy = editPane.getPrivacy();
            String terms = editPane.getTerms();                     
}   

If you use query params, which is shown as @RequestParam(), you need to call the service like如果您使用查询参数,显示为@RequestParam(),您需要调用该服务,如

http://localhost:8080/api/editPane?title1="value"&title2="value"

required=false means that the queryparams are optional. required=false 表示 queryparams 是可选的。 So if you dont provide a param it will be null因此,如果您不提供参数,它将为空

Here is a better Solution:这是一个更好的解决方案:

Use a pojo class like使用像这样的 pojo 类

public class EditPane {
    private String title1;
    private String title2;
    private String about;
    private String privacy;

    public EditPane() {}

    // Getter and Setters
}

Rewrite your Post Method:重写你的 Post 方法:

@RequestMapping(value = {"/api/editPane"}, method = RequestMethod.POST)
@CrossOrigin(origins = {"*"})
public ResponseEntity<?> editpane(@RequestBody EditPane editPane) {
  //doSuff();
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM