x213212
    • Create new note
    • Create a note from template
      • Sharing URL Link copied
      • /edit
      • View mode
        • Edit mode
        • View mode
        • Book mode
        • Slide mode
        Edit mode View mode Book mode Slide mode
      • Customize slides
      • Note Permission
      • Read
        • Only me
        • Signed-in users
        • Everyone
        Only me Signed-in users Everyone
      • Write
        • Only me
        • Signed-in users
        • Everyone
        Only me Signed-in users Everyone
      • Engagement control Commenting, Suggest edit, Emoji Reply
    • Invite by email
      Invitee

      This note has no invitees

    • Publish Note

      Share your work with the world Congratulations! 🎉 Your note is out in the world Publish Note

      Your note will be visible on your profile and discoverable by anyone.
      Your note is now live.
      This note is visible on your profile and discoverable online.
      Everyone on the web can find and read all notes of this public team.
      See published notes
      Unpublish note
      Please check the box to agree to the Community Guidelines.
      View profile
    • Commenting
      Permission
      Disabled Forbidden Owners Signed-in users Everyone
    • Enable
    • Permission
      • Forbidden
      • Owners
      • Signed-in users
      • Everyone
    • Suggest edit
      Permission
      Disabled Forbidden Owners Signed-in users Everyone
    • Enable
    • Permission
      • Forbidden
      • Owners
      • Signed-in users
    • Emoji Reply
    • Enable
    • Versions and GitHub Sync
    • Note settings
    • Note Insights
    • Engagement control
    • Transfer ownership
    • Delete this note
    • Save as template
    • Insert from template
    • Import from
      • Dropbox
      • Google Drive
      • Gist
      • Clipboard
    • Export to
      • Dropbox
      • Google Drive
      • Gist
    • Download
      • Markdown
      • HTML
      • Raw HTML
Menu Note settings Versions and GitHub Sync Note Insights Sharing URL Create Help
Create Create new note Create a note from template
Menu
Options
Engagement control Transfer ownership Delete this note
Import from
Dropbox Google Drive Gist Clipboard
Export to
Dropbox Google Drive Gist
Download
Markdown HTML Raw HTML
Back
Sharing URL Link copied
/edit
View mode
  • Edit mode
  • View mode
  • Book mode
  • Slide mode
Edit mode View mode Book mode Slide mode
Customize slides
Note Permission
Read
Only me
  • Only me
  • Signed-in users
  • Everyone
Only me Signed-in users Everyone
Write
Only me
  • Only me
  • Signed-in users
  • Everyone
Only me Signed-in users Everyone
Engagement control Commenting, Suggest edit, Emoji Reply
  • Invite by email
    Invitee

    This note has no invitees

  • Publish Note

    Share your work with the world Congratulations! 🎉 Your note is out in the world Publish Note

    Your note will be visible on your profile and discoverable by anyone.
    Your note is now live.
    This note is visible on your profile and discoverable online.
    Everyone on the web can find and read all notes of this public team.
    See published notes
    Unpublish note
    Please check the box to agree to the Community Guidelines.
    View profile
    Engagement control
    Commenting
    Permission
    Disabled Forbidden Owners Signed-in users Everyone
    Enable
    Permission
    • Forbidden
    • Owners
    • Signed-in users
    • Everyone
    Suggest edit
    Permission
    Disabled Forbidden Owners Signed-in users Everyone
    Enable
    Permission
    • Forbidden
    • Owners
    • Signed-in users
    Emoji Reply
    Enable
    Import from Dropbox Google Drive Gist Clipboard
       owned this note    owned this note      
    Published Linked with GitHub
    Subscribed
    • Any changes
      Be notified of any changes
    • Mention me
      Be notified of mention me
    • Unsubscribe
    Subscribe
    # vue element admin 移除 官方 mock.js 連接後端 https://www.one-tab.com/page/GvY65sttTNiDkljsAsPKEw spring boot 首先必須熟悉一下json格式 https://segmentfault.com/a/1190000017372916 這邊可以返回簡單範例 ```json {"id":1,"age":18,"user-name":"Java技术栈"} ``` 我們可以運行 vue element admin 進行 觀察 在目錄底下 ```path src/api/user.js ``` 這個檔案裡 ![](https://i.imgur.com/XFsj6OS.png) 我們要把官方的 mock.js 原封不動 的 轉換為自訂 後段 spring boot mock.js 就是一種 在 後端 method 還沒完成的時候,前端可以自行模擬後端 產生資料 所以在開發的時候,相當方便。 怎麼做呢? 來看一下 原生 dev 環境 運行畫面。 按下 login ![](https://i.imgur.com/fxB0Fmh.png) ![](https://i.imgur.com/XrhaJyF.png) 可以看到 login這個地方產生了 json 也就是 token 看是否給予權限? 這邊可以看到 伺服器預設返回為 ```code admin-token ``` 那麼 我們來追一下程式碼 在 path ```path src/store/modules/user.js ``` 可以看到這邊是 login 呼叫事件並且產生cookies ![](https://i.imgur.com/W89gm9U.png) 我們來看一下 settoken是否為cookies 對的沒錯 ![](https://i.imgur.com/m4Mit0F.png) 那我們只要村造他的規則,基本上 只要 改 ```path src/api/user.js ``` 就可以進行無痛從 mock.js 改為 其他 後端restful ,只要 符合他的規則 (當然後面比較熟悉一點可以對她進行更深入的改動。 那我們可以看到 ![](https://i.imgur.com/PUyrgI0.png) 在這邊的話 ![](https://i.imgur.com/K5Y4kD1.png) 我們更動為 ```code return request({ url: 'http://localhost:9990/httpMethod', method: 'post', data: { name: data.username, pwd: data.password } }) ``` 在這個部分的話 ,我傳了 使用者id ,與 password 給伺服器,伺服器在 根據 所分配的權限 給予前端再來根據權限來決定畫面如何呈現。 *** url的部分 我們更改為 我們自己後端, 這邊需要 根據 自行選用後端進行相關 設定與配置 例如我們上一章,所說的 跨網域 等等 ,相關設定。 這邊我們來看 spring boot 如何設定 ```java @ComponentScan(basePackages = {"com.demo.controller"}) @Controller @RestController @RequestMapping("/") @EnableAutoConfiguration @CrossOrigin(origins = "http://172.16.7.21:9527", maxAge = 3600) public class DemoApplication { // // 跨網域 @Bean public WebMvcConfigurer corsConfigurer() { return new WebMvcConfigurer() { @Override public void addCorsMappings(CorsRegistry registry) { registry.addMapping("/httpMethod/**") .allowedOrigins("http://localhost:9528");//允许域名访问,如果*,代表所有域名 //.allowedOrigins("http://localhost:9527");//允许域名访问,如果*,代表所有域名 registry.addMapping("/httpMethod2/**") .allowedOrigins("http://localhost:9527");//允许域名访问,如果*,代表所有域名 } }; } Login memberAccount; @CrossOrigin @PostMapping(value = "/httpMethod", produces = "application/json") @ResponseBody public Login httpMethod(@RequestBody Map<String, Object> params) throws JsonProcessingException{ System.out.println("sent name is "+ params.get("name").toString()); System.out.println("sent pwd is "+ params.get("pwd").toString()); if( params.get("name").equals("admin") && params.get("pwd").equals("111111" ) ) { Login memberAccount = new Login(); memberAccount.setCode(20000); Token test = new Token(); memberAccount.setData(test); ObjectMapper objectMapper = new ObjectMapper(); String userJsonStr = objectMapper.writeValueAsString(memberAccount); System.out.print(userJsonStr); return memberAccount; } return memberAccount; } @CrossOrigin @GetMapping("/httpMethod2") @ResponseBody public String httpMethod2(){ System.out.println("sent name is "); System.out.println("sent pwd is "); return "success"; } } ``` # Json 與 CORS 跨網域 紀錄 ![](https://i.imgur.com/EDDOugD.png) ![](https://i.imgur.com/jmIDRra.png) ![](https://i.imgur.com/pBQC17k.png) 這邊稍微紀錄 如何使用 跨網域 和 jackson java 在 spring boot 所以用的 json 好像是 fastjson 他好像跟jackson 差不多 https://segmentfault.com/a/1190000005717319 來介紹一下 如後 透過 java class to json 很簡單, 只要 ```java Login memberAccount = new Login(); memberAccount.setCode(20000); Token test = new Token(); memberAccount.setData(test); ///這邊是我 自己看能不能把 java 轉成 字串測試 ObjectMapper objectMapper = new ObjectMapper(); String userJsonStr = objectMapper.writeValueAsString(memberAccount); System.out.print(userJsonStr); //到了 return 前端所接收到的就是一個 json 格式的字串了 return memberAccount; ``` 後端 restful 好了之後 可以發現,已經可以移植成功! ![](https://i.imgur.com/yp5wY0n.png) 後面會有詳細的git.... <script src="https://gist.github.com/x213212/6de9f23fa5cbabba9186eb04e405b1d6.js"></script>

    Import from clipboard

    Paste your markdown or webpage here...

    Advanced permission required

    Your current role can only read. Ask the system administrator to acquire write and comment permission.

    This team is disabled

    Sorry, this team is disabled. You can't edit this note.

    This note is locked

    Sorry, only owner can edit this note.

    Reach the limit

    Sorry, you've reached the max length this note can be.
    Please reduce the content or divide it to more notes, thank you!

    Import from Gist

    Import from Snippet

    or

    Export to Snippet

    Are you sure?

    Do you really want to delete this note?
    All users will lose their connection.

    Create a note from template

    Create a note from template

    Oops...
    This template has been removed or transferred.
    Upgrade
    All
    • All
    • Team
    No template.

    Create a template

    Upgrade

    Delete template

    Do you really want to delete this template?
    Turn this template into a regular note and keep its content, versions, and comments.

    This page need refresh

    You have an incompatible client version.
    Refresh to update.
    New version available!
    See releases notes here
    Refresh to enjoy new features.
    Your user state has changed.
    Refresh to load new user state.

    Sign in

    Forgot password

    or

    By clicking below, you agree to our terms of service.

    Sign in via Facebook Sign in via Twitter Sign in via GitHub Sign in via Dropbox Sign in with Wallet
    Wallet ( )
    Connect another wallet

    New to HackMD? Sign up

    Help

    • English
    • 中文
    • Français
    • Deutsch
    • 日本語
    • Español
    • Català
    • Ελληνικά
    • Português
    • italiano
    • Türkçe
    • Русский
    • Nederlands
    • hrvatski jezik
    • język polski
    • Українська
    • हिन्दी
    • svenska
    • Esperanto
    • dansk

    Documents

    Help & Tutorial

    How to use Book mode

    Slide Example

    API Docs

    Edit in VSCode

    Install browser extension

    Contacts

    Feedback

    Discord

    Send us email

    Resources

    Releases

    Pricing

    Blog

    Policy

    Terms

    Privacy

    Cheatsheet

    Syntax Example Reference
    # Header Header 基本排版
    - Unordered List
    • Unordered List
    1. Ordered List
    1. Ordered List
    - [ ] Todo List
    • Todo List
    > Blockquote
    Blockquote
    **Bold font** Bold font
    *Italics font* Italics font
    ~~Strikethrough~~ Strikethrough
    19^th^ 19th
    H~2~O H2O
    ++Inserted text++ Inserted text
    ==Marked text== Marked text
    [link text](https:// "title") Link
    ![image alt](https:// "title") Image
    `Code` Code 在筆記中貼入程式碼
    ```javascript
    var i = 0;
    ```
    var i = 0;
    :smile: :smile: Emoji list
    {%youtube youtube_id %} Externals
    $L^aT_eX$ LaTeX
    :::info
    This is a alert area.
    :::

    This is a alert area.

    Versions and GitHub Sync
    Get Full History Access

    • Edit version name
    • Delete

    revision author avatar     named on  

    More Less

    Note content is identical to the latest version.
    Compare
      Choose a version
      No search result
      Version not found
    Sign in to link this note to GitHub
    Learn more
    This note is not linked with GitHub
     

    Feedback

    Submission failed, please try again

    Thanks for your support.

    On a scale of 0-10, how likely is it that you would recommend HackMD to your friends, family or business associates?

    Please give us some advice and help us improve HackMD.

     

    Thanks for your feedback

    Remove version name

    Do you want to remove this version name and description?

    Transfer ownership

    Transfer to
      Warning: is a public team. If you transfer note to this team, everyone on the web can find and read this note.

        Link with GitHub

        Please authorize HackMD on GitHub
        • Please sign in to GitHub and install the HackMD app on your GitHub repo.
        • HackMD links with GitHub through a GitHub App. You can choose which repo to install our App.
        Learn more  Sign in to GitHub

        Push the note to GitHub Push to GitHub Pull a file from GitHub

          Authorize again
         

        Choose which file to push to

        Select repo
        Refresh Authorize more repos
        Select branch
        Select file
        Select branch
        Choose version(s) to push
        • Save a new version and push
        • Choose from existing versions
        Include title and tags
        Available push count

        Pull from GitHub

         
        File from GitHub
        File from HackMD

        GitHub Link Settings

        File linked

        Linked by
        File path
        Last synced branch
        Available push count

        Danger Zone

        Unlink
        You will no longer receive notification when GitHub file changes after unlink.

        Syncing

        Push failed

        Push successfully