brian661
brian661

Reputation: 548

Posting data as a object by Vue axios to spring back end server

I am trying to implement a login system with firebase.
In my page, I have several Ids from firebase and allow user to input a username.
And I want to pass this information to my backend server for inserting to the database.

My concern: My UserController#createAccount(...) is setting each data I needed as the parameters, which makes my sources code quite messy.

Question: Is there any way that I can pass all my data as a model?
For example I have a class in backend call MyLoginInfo, so that I can change my function to createAccount(MyLoginInfo loginInfo)?

// vue front end
<template lang="html">
   <v-form>
      <v-text-field :label="label" :clearable="clearable" :counter="counter"></v-text-field>
      <v-btn color="primary" @click="createAccount">
         <span>
         submit
         </span>
      </v-btn>
   </v-form>
</template>
<script>
   var firebase = require('firebase/app');

   export default {
     name: 'CreateAccount',
     data: () => ({
       label: 'User name',
       clearable: true,
       counter: 20,
       firebaseUId:null,
       firebaseProviderData:null
     }),
     created() {
       firebase.auth().onAuthStateChanged(user => {
         if (user) {
           this.firebaseUId = user.uid;
           this.firebaseProviderData = user.providerData;
           this.$store.dispatch('signInAction', user);
         }
       });
     },
     methods: {
       createAccount() {
         return myServer  // axios object which pointing to my backend server
           .post('/user/createAccount', {
             username: username,
             firebaseUid: firebaseUid,
             providerId: providerId,
             providerUid: providerUid
           })
       }
     }
   }
</script>
<style scoped>
</style>    
// Spring back end
@RestController
@RequestMapping("/user")

public class UserController {

    @ResponseBody
    @RequestMapping("/createAccount")
    public String createAccount(@RequestParam("username") String username,
        @RequestParam("firebaseUid") String firebaseUid,
        @RequestParam("firebaseProviderId") String firebaseProviderId,
        @RequestParam("firebaseProviderUId") String firebaseProviderUId) {

        return "create Success";
    }
}

Upvotes: 2

Views: 678

Answers (1)

Dedy Chaidir
Dedy Chaidir

Reputation: 927

Why dont you use json type for posting? Just create a json object to be posted in the body section. And in the backend, I assume it will be easy for java to catch the body and parse it into json again.

Upvotes: 1

Related Questions