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.瀵煎叆鏁版嵁搴撹〃
鎵撳紑Navicat杞欢,閫夋嫨瀵瑰簲鐨勬暟鎹簱锛岃繍琛宻ql鏂囦欢銆?/span>
鎵ц缁撴灉锛?/span>
2.
鏍规嵁杩欎釜璺敱璺緞鍒拌矾鐢辨枃浠讹紙src/route/index.js锛夛紝鍙互瀹氫綅鍒板垎绫荤鐞嗛〉闈細
鐢辫矾鐢辨枃浠剁煡锛岄〉闈㈡槸src/pages/item/Category.vue
鍟嗗搧鍒嗙被浣跨敤浜嗘爲鐘剁粨鏋勶紝鑰岃繖绉嶇粨鏋勭殑缁勪欢vuetify骞舵病鏈変负鎴戜滑鎻愪緵锛岃繖閲岃嚜瀹氫箟浜嗕竴涓爲鐘剁粍浠躲€備笉瑕佹眰瀹炵幇鎴栬€呮煡璇㈢粍浠剁殑瀹炵幇锛屽彧瑕佹眰鍙互鍙傜収鏂囨。浣跨敤璇ョ粍浠跺嵆鍙細
(1)url寮傛璇锋眰
http://manage.leyou.com/item/category/list
浣嗗疄闄呭嵈鏄細
http://api.leyou.com/api/item/category/list?pid=0
杩欏氨浼氬嚭鐜拌法鍩熼棶棰?绗?澶х偣鍒嗘瀽)
杩欐槸鍥犱负锛屾垜浠湁涓€涓叏灞€鐨勯厤缃枃浠讹紝瀵规墍鏈夌殑璇锋眰璺緞杩涜浜嗙害瀹氾細
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; } }
GeneratedValue
<dependencies> <dependency> <groupId>javax.persistence</groupId> <artifactId>persistence-api</artifactId> <version>1.0</version> </dependency> </dependencies>
(3)mapper
鎴戜滑浣跨敤閫氱敤mapper鏉ョ畝鍖栧紑鍙戯細
package lucky.leyou.item.mapper; import lucky.leyou.item.domain.Category; import tk.mybatis.mapper.common.Mapper; public interface CategoryMapper extends Mapper<Category> { }
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
-
璇锋眰鏂瑰紡锛氬喅瀹氭垜浠敤GetMapping杩樻槸PostMapping
-
璇锋眰璺緞锛氬喅瀹氭槧灏勮矾寰?/span>
-
璇锋眰鍙傛暟锛氬喅瀹氭柟娉曠殑鍙傛暟
-
杩斿洖鍊肩粨鏋滐細鍐冲畾鏂规硶鐨勮繑鍥炲€?/span>
- 璇锋眰鏂瑰紡锛欸et锛屾煡璇㈣偗瀹氭槸get璇锋眰
- 璇锋眰璺緞锛?api/item/category/list銆傚叾涓?api鏄綉鍏冲墠缂€锛?item鏄綉鍏崇殑璺敱鏄犲皠锛岀湡瀹炵殑璺緞搴旇鏄?category/list
- 璇锋眰鍙傛暟锛歱id=0锛屾牴鎹畉ree缁勪欢鐨勮鏄庯紝搴旇鏄埗鑺傜偣鐨刬d锛岀涓€娆℃煡璇负0锛岄偅灏辨槸鏌ヨ涓€绾х被鐩?/span>
妯″潡缁撴瀯锛?/p>
娣诲姞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>
<3>鍒锋柊鍚庡彴绠$悊椤甸潰鏌ョ湅锛?/p>
杩欏叾瀹炴槸娴忚鍣ㄧ殑鍚屾簮绛栫暐閫犳垚鐨勮法鍩熼棶棰樸€?/p>
3.
浠ヤ笅鎯呭喌閮藉睘浜庤法鍩燂細
濡傛灉鍩熷悕鍜岀鍙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
鍥犱负璺ㄥ煙闂鏄祻瑙堝櫒瀵逛簬ajax璇锋眰鐨勪竴绉嶅畨鍏ㄩ檺鍒?/strong>锛?strong>涓€涓〉闈㈠彂璧风殑ajax璇锋眰锛屽彧鑳芥槸涓庡綋鍓嶉〉鍩熷悕鐩稿悓鐨勮矾寰?/strong>锛岃繖鑳芥湁鏁堢殑闃绘璺ㄧ珯鏀诲嚮銆?/span>
鍥犳锛?strong>璺ㄥ煙闂 鏄拡瀵筧jax鐨勪竴绉嶉檺鍒?/strong>銆?/span>
浼樺娍锛?/span>
-
鍦ㄦ湇鍔$杩涜鎺у埗鏄惁鍏佽璺ㄥ煙锛屽彲鑷畾涔夎鍒?/span>
-
瀹冨厑璁告祻瑙堝櫒鍚戣法婧愭湇鍔″櫒锛屽彂鍑?span class=" md-link">XMLHttpRequest
璇锋眰锛屼粠鑰屽厠鏈嶄簡AJAX鍙兘鍚屾簮浣跨敤鐨勯檺鍒躲€?/span>
CORS闇€瑕佹祻瑙堝櫒鍜屾湇鍔″櫒鍚屾椂鏀寔銆傜洰鍓嶏紝鎵€鏈夋祻瑙堝櫒閮芥敮鎸佽鍔熻兘锛孖E娴忚鍣ㄤ笉鑳戒綆浜嶪E10銆?/span>
-
娴忚鍣ㄧ锛?/span>
-
鏈嶅姟绔細
<2>CORS瑙e喅璺ㄥ煙瀹炵幇
SpringMVC宸茬粡甯垜浠啓濂戒簡CORS鐨勮法鍩熻繃婊ゅ櫒锛欳orsFilter ,鍐呴儴宸茬粡瀹炵幇浜嗗垰鎵嶆墍璁茬殑鍒ゅ畾閫昏緫锛屾垜浠洿鎺ョ敤灏卞ソ浜嗐€?/span>
浠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锛岀劧鍚庡湪娴忚鍣ㄤ腑娴嬭瘯锛岃闂甯革細
鑻ュ嚭鐜發eyou-gateway2妯″潡涓姤濡備笅閿欒com.netflix.zuul.exception.ZuulException: Forwarding error
瑙e喅鏂规锛氬皢濡備笅鐨?涓湇鍔″叏閮ㄩ噸鏂板惎鍔ㄣ€?/span>
以上是关于018 瀹炵幇鍟嗗搧鍒嗙被鏌ヨ的主要内容,如果未能解决你的问题,请参考以下文章