• springboot web & 增加不存在的url返回200状态码& vue 打包设置&vue.js 单文件使用


    spring boot项目增加 html web页面访问

    1. 首先 application.properties 文件中增加配置,指定静态资源目录(包括html的存放)

    spring.resources.static-locations=classpath:/webapp/,classpath:/webapp/static/

    2. 项目目录

    3. 如果有实现 WebMvcConfigurer  类的,增加实现

    1. package yourpack;
    2. import org.springframework.beans.factory.annotation.Autowired;
    3. import org.springframework.context.annotation.Configuration;
    4. import org.springframework.http.HttpStatus;
    5. import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
    6. import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry;
    7. import org.springframework.web.servlet.config.annotation.ViewControllerRegistry;
    8. import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;
    9. import javax.annotation.Resource;
    10. /**
    11. * @author wangtong
    12. */
    13. @Configuration
    14. public class CustomWebMvcConfigurer implements WebMvcConfigurer {
    15. @Autowired
    16. private YourInterceptor yourint;// 拦截器
    17. @Override
    18. public void addInterceptors(InterceptorRegistry registry) {
    19. registry.addInterceptor(yourint).addPathPatterns("/url").addPathPatterns("/url2");// 不设置拦截器的可以不实现该方法
    20. }
    21. @Override
    22. public void addViewControllers(ViewControllerRegistry registry) {
    23. // registry.addViewController("/").setStatusCode(HttpStatus.OK);
    24. registry.addViewController("/index.jsp").setViewName("index"); // 配置首页
    25. registry.addViewController("/").setViewName("index");
    26. registry.addViewController("/404").setViewName("404"); // 配置404页面
    27. }
    28. @Override
    29. public void addResourceHandlers(ResourceHandlerRegistry registry) {
    30. // 设置静态资源目录
    31. registry.addResourceHandler("/static/**")
    32. .addResourceLocations("classpath:/webapp/static/")
    33. .addResourceLocations("classpath:/resources/webapp/static/");
    34. }
    35. }

    如果访问不到页面的,可以检查下application配置文件是否有以下配置

    1. #spring.web.resources.add-mappings=false
    2. #spring.resources.add-mappings=false
    3. spring.mvc.view.suffix=.html

    如果有的话,需要进行注释。这两个配置都是不进行静态资源的映射。所以会导致html等无法访问。

    增加spring boot web不存在的url返回200状态码

    1. application配置文件增加以下配置

    spring.mvc.throw-exception-if-no-handler-found=true
    

    2. 增加一个error配置类的实现

    1. package ;
    2. import org.springframework.boot.web.server.ErrorPage;
    3. import org.springframework.boot.web.server.ErrorPageRegistrar;
    4. import org.springframework.boot.web.server.ErrorPageRegistry;
    5. import org.springframework.context.annotation.Configuration;
    6. import org.springframework.http.HttpStatus;
    7. @Configuration
    8. public class ErrorConfig implements ErrorPageRegistrar {
    9. @Override
    10. public void registerErrorPages(ErrorPageRegistry registry) {
    11. ErrorPage[] errorPages = new ErrorPage[1];
    12. errorPages[0] = new ErrorPage(HttpStatus.NOT_FOUND, "/404.do");
    13. registry.addErrorPages(errorPages);
    14. }
    15. }

    3. 增加一个mapping

    1. import org.springframework.http.HttpStatus;
    2. import org.springframework.http.ResponseEntity;
    3. @RequestMapping(value = {"/404.do"})
    4. public ResponseEntity> error() {
    5. Map retMap = new HashMap<>();
    6. retMap.put("message", "请求路径不存在");
    7. return new ResponseEntity> (retMap, HttpStatus.OK);
    8. }

    访问一下

    vue 打包配置:

    1. main.js 配置 axios相关,这里没有进行增加前缀路由,注释调的api是增加的,但是打包后,访问的页面里面也加上了,不知道为什么,所有就去掉吧

    1. // var baseURL = '/api';
    2. var baseURL = 'http://localhost:8080';
    3. axios.interceptors.request.use(config=>{
    4. config.baseURL= baseURL;
    5. config.headers.post["Origin"] = baseURL;
    6. config.headers.post["Referer"] = baseURL;
    7. return config;
    8. });
    9. axios.defaults.withCredentials = true;
    10. axios.defaults.headers.post["Origin"] = baseURL;
    11. axios.defaults.headers.post["Referer"] = baseURL;
    12. Vue.prototype.$request=axios;

    2. package.json 文件, scripts 中没有build的可以增加一个,如果执行 npm run build 报错的,可以改成build+后缀的其它。 我这里的话 npm run buildt

    1. {
    2. "name": "vue-admin-template",
    3. "version": "4.4.0",
    4. "description": "A vue admin template with Element UI & axios & iconfont & permission control & lint",
    5. "author": "",
    6. "scripts": {
    7. "dev": "vue-cli-service serve",
    8. "build:prod": "vue-cli-service build",
    9. "buildt": "npm install && vue-cli-service build",
    10. "preview": "node build/index.js --preview",
    11. "svgo": "svgo -f src/icons/svg --config=src/icons/svgo.yml",
    12. "lint": "eslint --ext .js,.vue src",
    13. "test:unit": "jest --clearCache && vue-cli-service test:unit",
    14. "test:ci": "npm run lint && npm run test:unit"
    15. },

    3. vue.config.js文件

    1. module.exports = {
    2. publicPath: '/',
    3. outputDir: '../your-web/src/main/resources/webapp',
    4. assetsDir: 'static',
    5. lintOnSave: process.env.NODE_ENV === 'development',
    6. productionSourceMap: false,
    7. devServer: {
    8. port: 2234,
    9. open: true,
    10. overlay: {
    11. warnings: false,
    12. errors: true
    13. },
    14. // proxy: {
    15. // '/api': {
    16. // target: 'http://localhost:8080',
    17. // ws: true,
    18. // changeOrigin: true ,
    19. // pathRewrite:{
    20. // '^/api':''
    21. // }
    22. // }
    23. // }
    24. },

    这里的话,axios没有设置前缀,所以这里的路由也就不需要了。注释掉。

    outputDir  要输出的目录路径,这里的话,我这里打包的不在当前这个目录下面。

    生成到和当前node父目录同层的指定目录下。

    elementUi 单vue.js 文件使用

    页面效果:

    由于简单点,就直接都引入到一个html了

    index.html

    1. html>
    2. <html>
    3. <head>
    4. <meta charset="UTF-8">
    5. <link rel="stylesheet" href="https://unpkg.com/element-ui/lib/theme-chalk/index.css">
    6. <script src="https://unpkg.com/element-ui/lib/index.js">script>
    7. <script src="https://unpkg.com/axios/dist/axios.min.js">script>
    8. <script src="https://cdn.bootcss.com/qs/6.5.1/qs.min.js">script>
    9. head>
    10. <script type="module">
    11. script>
    12. <body>
    13. <div id="app">
    14. <el-form ref="form" :model="form" label-width="120px">
    15. <el-form-item label="select url">
    16. <el-select v-model="form.region" @change="changeName" placeholder="please select your zone">
    17. <el-option
    18. v-for="item in serviceUrl"
    19. :key="item.value"
    20. :label="item.label"
    21. :value="item.value"
    22. />
    23. el-select>
    24. el-form-item>
    25. <el-form-item label="url">
    26. <el-input v-model="form.name" />
    27. el-form-item>
    28. <el-form-item label="select url">
    29. <el-select v-model="form.encryptionType" placeholder="please select encryptionType">
    30. <el-option
    31. v-for="item in encryType"
    32. :key="item.value"
    33. :label="item.label"
    34. :value="item.value"
    35. />
    36. el-select>
    37. el-form-item>
    38. <el-form-item label="type">
    39. <el-radio-group v-model="form.resource">
    40. <el-radio label="post" key="post" />
    41. <el-radio label="get" key="get" disabled />
    42. el-radio-group>
    43. el-form-item>
    44. <el-form-item label="privateKey">
    45. <el-input v-model="form.privateKey" />
    46. el-form-item>
    47. <el-form-item label="service">
    48. <el-input v-model="form.service" />
    49. el-form-item>
    50. <el-form-item label="merchantName">
    51. <el-input v-model="form.merchantName" />
    52. el-form-item>
    53. <el-form-item label="dataContent">
    54. <el-input v-model="form.dataContent" type="textarea" />
    55. el-form-item>
    56. <el-form-item>
    57. <el-button type="primary" :disabled="loading" @click="onSubmit">提交el-button>
    58. <el-button @click="onCancel">Cancelel-button>
    59. el-form-item>
    60. el-form>
    61. <div description="描述文字">
    62. <span>调用结果span>
    63. <p v-html="result" >p>
    64. div>
    65. div>
    66. div>
    67. body>
    68. <script src="https://unpkg.com/vue@2/dist/vue.js">script>
    69. <script src="https://unpkg.com/element-ui/lib/index.js">script>
    70. <script>
    71. new Vue({
    72. el: '#app',
    73. data: function() {
    74. return {
    75. form: {
    76. name: 'openapi/fsddg/v1',
    77. region: 'openApi',
    78. date1: '',
    79. date2: '',
    80. delivery: false,
    81. type: [],
    82. privateKey: 'gawegsfew',
    83. resource: 'post',
    84. encryptionType: 'md5',
    85. service: 'gawdewae',
    86. merchantName: 'fewageawwe',
    87. requestSeq: "fawegeaw",
    88. dataContent: JSON.stringify({
    89. "backTrackingDate": "",
    90. "bizNo": "20220329000002",
    91. "reqParams": {
    92. "saasChannelInfo": {
    93. "secondLevel": "XW",
    94. "thirdLevel": "",
    95. "custType": "",
    96. "firstLevel": "JC"
    97. }
    98. },
    99. "idNo": "fafwfawefawew",
    100. "encryptType": "md5"
    101. })
    102. },
    103. serviceUrl:[{
    104. value: 'openapi/dataservice/v1',
    105. label: 'openApi'
    106. },{
    107. value: "sfs/dataservice/v1",
    108. label: "dataserviceV1"
    109. },{
    110. value: "sfs/dataservice/v2",
    111. label: "dataserviceV2"
    112. }],
    113. encryType:[{
    114. label:"rsa",
    115. value:"rsa"
    116. },{
    117. label: "md5",
    118. value: "md5"
    119. }],
    120. result: '',
    121. loading : false,
    122. visible: false
    123. }
    124. },
    125. methods:{
    126. onSubmit() {
    127. this.loading = false;
    128. this.$message('submit!')
    129. const form = this.form;
    130. var params = {
    131. "dataContent": form.dataContent,
    132. "service": form.service,
    133. "productCode": form.service,
    134. "signature": '',
    135. "requestSeq": form.requestSeq,
    136. "encryptionType": form.encryptionType,
    137. "privateKey": form.privateKey,
    138. "merchantSecret": "123456",
    139. "merchantName": form.merchantName
    140. };
    141. var paramsStr = Qs.stringify(params);
    142. var th = this;
    143. axios.post('sfs/getSe', paramsStr).then((response) =>{
    144. console.log('获取sign成功', response);
    145. if(response.status == 200){
    146. params["signature"] = response.data;
    147. paramsStr = Qs.stringify(params);
    148. console.log("入参", params)
    149. axios.post(form.name, paramsStr).then((response) => {
    150. console.log(response.data);
    151. th.result = response.data;
    152. }).catch(function (error) { // 请求失败处理
    153. console.log(error);
    154. });;
    155. }
    156. }).catch(function (error) { // 请求失败处理
    157. console.log(error);
    158. });;
    159. },
    160. changeName(){
    161. this.form.name = this.form.region;
    162. },
    163. onCancel() {
    164. this.$message({
    165. message: 'cancel!',
    166. type: 'warning'
    167. })
    168. }
    169. }
    170. });
    171. // service.post('fsagsad', {}).then(response=>{
    172. // console.log(response)
    173. // });
    174. script>
    175. html>

  • 相关阅读:
    nginx转发https:SSL_do_handshake() failed
    【Linux】常见指令(二)
    如何制作网页 ico
    【Rust笔记】Rust与Java交互-JNI模块编写-实践总结
    基于ABP和Magicodes实现Excel导出操作
    【Leetcode每日一题:882. 细分图中的可到达节点~~~单源最短路径Dijkstra算法】
    C++QT开发——TCP&UDP网络编程
    C语言 | Leetcode C语言题解之第513题找树左下角的值
    CSS 实现动态显示隐藏(:checked 和 :target 的妙用)
    【Android UI】贝塞尔曲线 ⑤ ( 德卡斯特里奥算法 | 贝塞尔曲线递推公式 )
  • 原文地址:https://blog.csdn.net/dandanforgetlove/article/details/132764878