018 瀹炵幇鍟嗗搧鍒嗙被鏌ヨ

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了018 瀹炵幇鍟嗗搧鍒嗙被鏌ヨ相关的知识,希望对你有一定的参考价值。

鏍囩锛?a href='http://www.mamicode.com/so/1/%e6%a0%87%e5%87%86' title='鏍囧噯'>鏍囧噯   瀹炵幇   鍙傛暟   gets   鍝佺墝   瀹氫箟   搴旇   dom   鍒嗙被   

鍟嗗煄鐨勬牳蹇冭嚜鐒舵槸鍟嗗搧锛岃€屽晢鍝佸浜嗕互鍚庯紝鑲畾瑕佽繘琛屽垎绫伙紝骞朵笖涓嶅悓鐨勫晢鍝佷細鏈変笉鍚岀殑鍝佺墝淇℃伅锛屾垜浠渶瑕佷緷娆″幓瀹屾垚锛氬晢鍝佸垎绫汇€佸搧鐗屻€佸晢鍝佺殑寮€鍙戙€?/span>

1.瀵煎叆鏁版嵁搴撹〃

鎶€鏈浘鐗? src=

 

鎵撳紑Navicat杞欢,閫夋嫨瀵瑰簲鐨勬暟鎹簱锛岃繍琛宻ql鏂囦欢銆?/span>

鎶€鏈浘鐗? src=

 

鎶€鏈浘鐗? src=

鎵ц缁撴灉锛?/span>

鎶€鏈浘鐗? src=

 

2.瀹炵幇鍔熻兘

鍦ㄦ祻瑙堝櫒椤甸潰鐐瑰嚮“鍒嗙被绠$悊”鑿滃崟锛?/span>

鎶€鏈浘鐗? src=

 

鏍规嵁杩欎釜璺敱璺緞鍒拌矾鐢辨枃浠讹紙src/route/index.js锛夛紝鍙互瀹氫綅鍒板垎绫荤鐞嗛〉闈細

鎶€鏈浘鐗? src=

鐢辫矾鐢辨枃浠剁煡锛岄〉闈㈡槸src/pages/item/Category.vue

鎶€鏈浘鐗? src=

鍟嗗搧鍒嗙被浣跨敤浜嗘爲鐘剁粨鏋勶紝鑰岃繖绉嶇粨鏋勭殑缁勪欢vuetify骞舵病鏈変负鎴戜滑鎻愪緵锛岃繖閲岃嚜瀹氫箟浜嗕竴涓爲鐘剁粍浠躲€備笉瑕佹眰瀹炵幇鎴栬€呮煡璇㈢粍浠剁殑瀹炵幇锛屽彧瑕佹眰鍙互鍙傜収鏂囨。浣跨敤璇ョ粍浠跺嵆鍙細

鎶€鏈浘鐗? src=

(1)url寮傛璇锋眰

鐐瑰嚮鍟嗗搧绠$悊涓嬬殑鍒嗙被绠$悊瀛愯彍鍗曪紝鍦ㄦ祻瑙堝櫒鎺у埗鍙板彲浠ョ湅鍒帮細

 鎶€鏈浘鐗? src=

鎴戜滑鏄庢槑鏄娇鐢ㄧ殑鐩稿璺緞锛?item/category/list锛岃閬撶悊鍙戣捣鐨勮姹傚湴鍧€搴旇鏄細

http://manage.leyou.com/item/category/list

浣嗗疄闄呭嵈鏄細

http://api.leyou.com/api/item/category/list?pid=0

杩欏氨浼氬嚭鐜拌法鍩熼棶棰?绗?澶х偣鍒嗘瀽)

杩欐槸鍥犱负锛屾垜浠湁涓€涓叏灞€鐨勯厤缃枃浠讹紝瀵规墍鏈夌殑璇锋眰璺緞杩涜浜嗙害瀹氾細

鎶€鏈浘鐗? src=

 

璺緞鏄?span class="md-link">http://api.leyou.com锛屽苟涓旈粯璁ゅ姞涓婁簡/api鐨勫墠缂€锛岃繖鎭板ソ涓庢垜浠殑缃戝叧璁剧疆鍖归厤锛屾垜浠彧闇€瑕佹妸鍦板潃鏀规垚缃戝叧鐨勫湴鍧€鍗冲彲,鍥犱负鎴戜滑浣跨敤浜唍ginx鍙嶅悜浠g悊锛岃繖閲屽彲浠ュ啓鍩熷悕銆?/span>

(2)瀹炰綋绫?/span>

鍦?span>leyou-item-interface涓坊鍔燾ategory瀹炰綋绫伙細

 

package lucky.leyou.item.domain;

import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.Table;

@Table(name="tb_category")
public class Category {
    @Id
    @GeneratedValue(strategy=GenerationType.IDENTITY)
    private Long id;
    private String name;
    private Long parentId;
    // 娉ㄦ剰isParent鐢熸垚鐨刧etter鍜宻etter鏂规硶闇€瑕佹墜鍔ㄥ姞涓奍s
    //瀹為檯寮€鍙戜腑灏介噺閬垮厤鏁版嵁搴撳瓧娈靛悕浠s寮€澶?/span>
    private Boolean isParent; 
    private Integer sort;

    public Long getId() {
        return id;
    }

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

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public Long getParentId() {
        return parentId;
    }

    public void setParentId(Long parentId) {
        this.parentId = parentId;
    }

    public Boolean getIsParent() {
        return isParent;
    }

    public void setIsParent(Boolean parent) {
        isParent = parent;
    }

    public Integer getSort() {
        return sort;
    }

    public void setSort(Integer sort) {
        this.sort = sort;
    }
}

闇€瑕佹敞鎰忕殑鏄紝杩欓噷瑕佺敤鍒癹pa鐨勬敞瑙?鍗冲疄浣撶被涓婃墍鍔犵殑娉ㄨВ@Table銆丂Id銆丂GeneratedValue)锛屽洜姝ゆ垜浠湪leyou-item-interface妯″潡涓殑pom鏂囦欢娣诲姞jpa渚濊禆

<dependencies>
        <dependency>
            <groupId>javax.persistence</groupId>
            <artifactId>persistence-api</artifactId>
            <version>1.0</version>
        </dependency>
    </dependencies>

(3)mapper

鎶€鏈浘鐗? src=

鎴戜滑浣跨敤閫氱敤mapper鏉ョ畝鍖栧紑鍙戯細

package lucky.leyou.item.mapper;

import lucky.leyou.item.domain.Category;
import tk.mybatis.mapper.common.Mapper;

public interface CategoryMapper extends Mapper<Category> {
}

瑕佹敞鎰忥紝鎴戜滑骞舵病鏈夊湪mapper鎺ュ彛涓婂0鏄嶡Mapper娉ㄨВ锛岄偅涔坢ybatis濡備綍鎵嶈兘鎵惧埌鎺ュ彛鍛紵

鎴戜滑鍦ㄥ惎鍔ㄧ被涓婃坊鍔犱竴涓壂鎻忓寘鍔熻兘锛?/span>

package lucky.leyou;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.client.discovery.EnableDiscoveryClient;
import tk.mybatis.spring.annotation.MapperScan;

@SpringBootApplication
@EnableDiscoveryClient
@MapperScan("lucky.leyou.item.mapper")   // mapper鎺ュ彛鐨勫寘鎵弿
public class LeyouItemServiceApplication {

    public static void main(String[] args) {
        SpringApplication.run(LeyouItemServiceApplication.class, args);
    }
}

(4)service

涓€鑸瑂ervice灞傛垜浠細瀹氫箟鎺ュ彛鍜屽疄鐜扮被銆?/p>

鎺ュ彛锛?/p>

package lucky.leyou.item.service;

import lucky.leyou.item.domain.Category;

import java.util.List;

public interface ICategoryService {
    /**
     * 鏍规嵁parentId鏌ヨ瀛愮被鐩?
     * @param pid
     * @return
     */
    public List<Category> queryCategoriesByPid(Long pid);
}

瀹炵幇绫伙細

package lucky.leyou.item.service.impl;

import lucky.leyou.item.domain.Category;
import lucky.leyou.item.mapper.CategoryMapper;
import lucky.leyou.item.service.ICategoryService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class CategoryServiceImpl implements ICategoryService {

    @Autowired
    private CategoryMapper categoryMapper;

    /**
     * 鏍规嵁parentId鏌ヨ瀛愮被鐩?
     * @param pid
     * @return
     */
    @Override
    public List<Category> queryCategoriesByPid(Long pid) {
        Category record = new Category();
        record.setParentId(pid);
        return this.categoryMapper.select(record);
    }
}

(5)Controller

缂栧啓涓€涓猚ontroller涓€鑸渶瑕佺煡閬撳洓涓唴瀹癸細

  • 璇锋眰鏂瑰紡锛氬喅瀹氭垜浠敤GetMapping杩樻槸PostMapping

  • 璇锋眰璺緞锛氬喅瀹氭槧灏勮矾寰?/span>

  • 璇锋眰鍙傛暟锛氬喅瀹氭柟娉曠殑鍙傛暟

  • 杩斿洖鍊肩粨鏋滐細鍐冲畾鏂规硶鐨勮繑鍥炲€?/span>

鍦ㄥ垰鎵嶉〉闈㈠彂璧风殑璇锋眰涓紝鎴戜滑灏辫兘寰楀埌缁濆ぇ澶氭暟淇℃伅

鎶€鏈浘鐗? src=

  • 璇锋眰鏂瑰紡锛欸et锛屾煡璇㈣偗瀹氭槸get璇锋眰
  • 璇锋眰璺緞锛?api/item/category/list銆傚叾涓?api鏄綉鍏冲墠缂€锛?item鏄綉鍏崇殑璺敱鏄犲皠锛岀湡瀹炵殑璺緞搴旇鏄?category/list
  • 璇锋眰鍙傛暟锛歱id=0锛屾牴鎹畉ree缁勪欢鐨勮鏄庯紝搴旇鏄埗鑺傜偣鐨刬d锛岀涓€娆℃煡璇负0锛岄偅灏辨槸鏌ヨ涓€绾х被鐩?/span>

妯″潡缁撴瀯锛?/p>

鎶€鏈浘鐗? src=

娣诲姞Controller锛?/p>

package lucky.leyou.item.controller;

import lucky.leyou.item.domain.Category;
import lucky.leyou.item.service.ICategoryService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Controller;
import org.springframework.util.CollectionUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

import java.util.List;

@Controller
@RequestMapping(path = "/category")
public class CategoryController {
    @Autowired
    private ICategoryService iCategoryService;

    /**
     * 鏍规嵁parentId鏌ヨ瀛愮被鐩?
     * @param pid @RequestParam(value = "pid",defaultValue = "0") long pid 浣滅敤锛氭帴鏀秛rl涓惡甯︾殑璇锋眰鍙傛暟,璁剧疆榛樿鍊间负0
     * @return
     */
    @RequestMapping("/list")
    public ResponseEntity<List<Category>> queryCategoriesByPid(@RequestParam(value = "pid",defaultValue = "0") Long pid){

        if(pid==null||pid<0){
            //鍝嶅簲绫诲瀷400:濡傛灉pid涓簄ull鎴杙id<0,杩斿洖璇锋眰鍙傛暟涓嶅悎娉?/span>
            return ResponseEntity.badRequest().build();
        }
        List<Category> categories = this.iCategoryService.queryCategoriesByPid(pid);
        //鍒╃敤CollectionUtils.isEmpty(categories)鍒ゆ柇闆嗗悎鏄惁涓虹┖
        if(CollectionUtils.isEmpty(categories)){
            //鍝嶅簲绫诲瀷404:璧勬簮鏈嶅姟鍣ㄦ湭鎵惧埌
            return ResponseEntity.notFound().build();
        }
        //鍝嶅簲绫诲瀷200:鏌ヨ鎴愬姛
        return ResponseEntity.ok(categories);

    }


}

(6)鍚姩骞舵祴璇?/p>

<1>鎴戜滑涓嶇粡杩囩綉鍏筹紝鐩存帴璁块棶锛?span class="md-link md-expand">http://localhost:8081/category/list

鎶€鏈浘鐗? src=

 

<2>璇曡瘯缃戝叧鏄惁鐣呴€氾細http://api.leyou.com/api/item/category/list

鎶€鏈浘鐗? src=

<3>鍒锋柊鍚庡彴绠$悊椤甸潰鏌ョ湅锛?/p>

鎶€鏈浘鐗? src=

杩欏叾瀹炴槸娴忚鍣ㄧ殑鍚屾簮绛栫暐閫犳垚鐨勮法鍩熼棶棰樸€?/p>

3.璺ㄥ煙闂

璺ㄥ煙锛氭祻瑙堝櫒瀵逛簬javascript鐨勫悓婧愮瓥鐣ョ殑闄愬埗 銆?/span>

鎶€鏈浘鐗? src=

浠ヤ笅鎯呭喌閮藉睘浜庤法鍩燂細

濡傛灉鍩熷悕鍜岀鍙i兘鐩稿悓锛屼絾鏄姹傝矾寰勪笉鍚?/strong>锛屼笉灞炰簬璺ㄥ煙锛屽锛?/span>

www.jd.com/item

www.jd.com/goods

http鍜宧ttps涔熷睘浜庤法鍩?/span>

鑰屾垜浠?span style="color: #000000;">鍒氭墠鏄粠manage.leyou.com鍘昏闂?span style="color: #ff0000;">api.leyou.com锛岃繖灞炰簬浜岀骇鍩熷悕涓嶅悓锛岃法鍩熶簡銆?/span>

(1)璺ㄥ煙闂浜х敓鐨勫満鏅?/span>

璺ㄥ煙涓嶄竴瀹氶兘浼氭湁璺ㄥ煙闂銆?/span>

鍥犱负璺ㄥ煙闂鏄祻瑙堝櫒瀵逛簬ajax璇锋眰鐨勪竴绉嶅畨鍏ㄩ檺鍒?/strong>锛?strong>涓€涓〉闈㈠彂璧风殑ajax璇锋眰锛屽彧鑳芥槸涓庡綋鍓嶉〉鍩熷悕鐩稿悓鐨勮矾寰?/strong>锛岃繖鑳芥湁鏁堢殑闃绘璺ㄧ珯鏀诲嚮銆?/span>

鍥犳锛?strong>璺ㄥ煙闂 鏄拡瀵筧jax鐨勪竴绉嶉檺鍒?/strong>銆?/span>

(2)CORS瑙e喅璺ㄥ煙

<1>CORS姒傝堪

CORS鏄竴涓猈3C鏍囧噯锛屽叏绉版槸"璺ㄥ煙璧勬簮鍏变韩"锛圕ross-origin resource sharing锛夈€?/span>

CORS鏄竴涓鑼冨寲鐨勮法鍩熻姹傝В鍐虫柟妗堬紝瀹夊叏鍙潬銆?/span>

浼樺娍锛?/span>

  • 鍦ㄦ湇鍔$杩涜鎺у埗鏄惁鍏佽璺ㄥ煙锛屽彲鑷畾涔夎鍒?/span>

  • 鏀寔鍚勭璇锋眰鏂瑰紡

瀹冨厑璁告祻瑙堝櫒鍚戣法婧愭湇鍔″櫒锛屽彂鍑?span class=" md-link">XMLHttpRequest璇锋眰锛屼粠鑰屽厠鏈嶄簡AJAX鍙兘鍚屾簮浣跨敤鐨勯檺鍒躲€?/span>

CORS闇€瑕佹祻瑙堝櫒鍜屾湇鍔″櫒鍚屾椂鏀寔銆傜洰鍓嶏紝鎵€鏈夋祻瑙堝櫒閮芥敮鎸佽鍔熻兘锛孖E娴忚鍣ㄤ笉鑳戒綆浜嶪E10銆?/span>

  • 娴忚鍣ㄧ锛?/span>

    鐩墠锛屾墍鏈夋祻瑙堝櫒閮芥敮鎸佽鍔熻兘锛圛E10浠ヤ笅涓嶈锛夈€傛暣涓狢ORS閫氫俊杩囩▼锛岄兘鏄祻瑙堝櫒鑷姩瀹屾垚锛屼笉闇€瑕佺敤鎴峰弬涓庛€?/span>

  • 鏈嶅姟绔細

    CORS閫氫俊涓嶢JAX娌℃湁浠讳綍宸埆锛屽洜姝や綘涓嶉渶瑕佹敼鍙樹互鍓嶇殑涓氬姟閫昏緫銆傚彧涓嶈繃锛屾祻瑙堝櫒浼氬湪璇锋眰涓惡甯︿竴浜涘ご淇℃伅锛屾垜浠渶瑕佷互姝ゅ垽鏂槸鍚﹀厑璁稿叾璺ㄥ煙锛岀劧鍚庡湪鍝嶅簲澶翠腑鍔犲叆涓€浜涗俊鎭嵆鍙€傝繖涓€鑸€氳繃杩囨护鍣ㄥ畬鎴愬嵆鍙€?/span>

<2>CORS瑙e喅璺ㄥ煙瀹炵幇

SpringMVC宸茬粡甯垜浠啓濂戒簡CORS鐨勮法鍩熻繃婊ゅ櫒锛欳orsFilter ,鍐呴儴宸茬粡瀹炵幇浜嗗垰鎵嶆墍璁茬殑鍒ゅ畾閫昏緫锛屾垜浠洿鎺ョ敤灏卞ソ浜嗐€?/span>

鍦?span>leyou-gateway2妯″潡涓紪鍐欎竴涓厤缃被锛屽苟涓旀敞鍐孋orsFilter锛?/span>

鎶€鏈浘鐗? src=

 

浠g爜锛?/p>

package lucky.leyou.config;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.cors.CorsConfiguration;
import org.springframework.web.cors.UrlBasedCorsConfigurationSource;
import org.springframework.web.filter.CorsFilter;

/**
 * 鍒╃敤cors瑙e喅璺ㄥ煙闂
 */
@Configuration
public class LeyouCorsConfig {
    @Bean
    public CorsFilter corsFilter() {
        //1.娣诲姞CORS閰嶇疆淇℃伅
        CorsConfiguration config = new CorsConfiguration();
        //1) 鍏佽鐨勫煙,涓嶈鍐?锛屽惁鍒檆ookie灏辨棤娉曚娇鐢ㄤ簡
        config.addAllowedOrigin("http://manage.leyou.com");
        //2) 鏄惁鍙戦€丆ookie淇℃伅
        config.setAllowCredentials(true);
        //3) 鍏佽鐨勮姹傛柟寮?/span>
        config.addAllowedMethod("OPTIONS");
        config.addAllowedMethod("HEAD");
        config.addAllowedMethod("GET");
        config.addAllowedMethod("PUT");
        config.addAllowedMethod("POST");
        config.addAllowedMethod("DELETE");
        config.addAllowedMethod("PATCH");
        // 4锛夊厑璁哥殑澶翠俊鎭?/span>
        config.addAllowedHeader("*");

        //2.娣诲姞鏄犲皠璺緞锛屾垜浠嫤鎴竴鍒囪姹?/span>
        UrlBasedCorsConfigurationSource configSource = new UrlBasedCorsConfigurationSource();
        configSource.registerCorsConfiguration("/**", config);

        //3.杩斿洖鏂扮殑CorsFilter.
        return new CorsFilter(configSource);
    }
}

閲嶅惎缃戝叧妯″潡leyou-gateway2锛岀劧鍚庡湪娴忚鍣ㄤ腑娴嬭瘯锛岃闂甯革細

鎶€鏈浘鐗? src=

鑻ュ嚭鐜發eyou-gateway2妯″潡涓姤濡備笅閿欒com.netflix.zuul.exception.ZuulException: Forwarding error

瑙e喅鏂规锛氬皢濡備笅鐨?涓湇鍔″叏閮ㄩ噸鏂板惎鍔ㄣ€?/span>

鎶€鏈浘鐗? src=

以上是关于018 瀹炵幇鍟嗗搧鍒嗙被鏌ヨ的主要内容,如果未能解决你的问题,请参考以下文章

Python Django妗嗘灦瀹炵幇鍟嗗煄椤圭洰婧愮爜鍔犺璁℃枃妗e拰娉ㄩ噴

涓€鍙ヨ瘽瀹炵幇MySQL搴撲腑鐨勬寜鏉′欢鍙樺寲鍒嗙粍

MyCat瀹炵幇鏁版嵁搴撲笌鏁版嵁搴撲箣闂寸殑璇诲啓鍒嗙

寰俊app鏀粯python浠g爜瀹炵幇

鏈哄櫒瀛︿範锛?3-鍨冨溇閭欢鍒嗙被2

銆怭ython鏈哄櫒瀛︿範銆戝喅绛栨爲鍒嗙被

(c)2006-2024 SYSTEM All Rights Reserved IT常识