-
2021-03-01 08:07:58
SpringMVC简介
在WEB开发中, SpringMVC实现了较为经典的MVC(Model,View,Controller)模式,
组成: 1.Model层(模型层): 管理App中每个功能模块所用到的值和数据.(实体类entity).
2.View层(视图层): 将模型层的数据展示给用户.(页面jsp,html,thymeleaf等..)
3.Controller层(控制层/控制器): 管理页面跳转, 处理用户请求响应逻辑. 也就是管理用 户和视图层交互的. 类似一个齿轮.
SpringMVC 使用 @Controller或@RestController注解的Bean来处理传入的HTTP请求.使用@RestController注解将Http请求映射到相应的控制器中的方法.
以下是@RestController用于提供JSON数据的典型示例
@RestController
@RequestMapping("/user")
public class RestControllerGet(){
@RequestMapping(value="/{user}", method=RequestMethod.GET)
public User getAllUser(){
//.... }
}三层架构与MVC的差异
2.Spring MVC 中的自动配置
SpringBoot提供了适用于大多数应用程序的SpringMVC的自动配置.
自动配置在Spring的默认值之上添加以下功能.1.包含ContentNegotiatingViewResolver bean 和 BeanNameViewResolver bean.
2.支持静态资源的服务,包括对WebJars的支持.
3.自动注册Converter, GenericConverter, Formatter等bean.
4.支持HttpMessageConverters.
5.自动注册MessageCodesResolver.
6.支持静态index.html.
7.支持自定义Favicon .
8.自动使用ConfigurableWebBindingInitializer bean.
1.HttpMessageConverter
Spring MVC 使用HttpMessageConverter接口来转换Http请求和响应. 其默认值提供了开箱即用的功能, 例如, 对象可以自动转换为JSON(使用Jackson库)或XML(如果Jackson XML扩展不可用,则使用JAXB), 字符串默认使用UTF-8进行编码.
如果需要添加或自定义转换器, 可以使用SpringBoot的HttpMessageConverters类.
@Configuration
public class MyConfiguration{
@Bean
public HttpMessageConverters customConverters(){
HttpMessageConverter> additional = ...;
HttpMessageConverter> another= ... ;
return new HttpMessageConverters(additional, another);
}
}
更多相关内容 -
media type(媒体类型)与media query(媒体查询)简介及使用方法介绍
2020-09-25 08:51:32media type(媒体类型)是css 2中的一个非常有用的属性,通过media type我们可以对不同的设备指定特定的样式,从而实现更丰富的界面。media query(媒体查询)是对media type的一种增强,是CSS 3的重要内容之一,需要了解... -
媒体类型mediaType
2020-12-15 16:18:55媒体类型决定浏览器将以何种形式对资源进行解析。 常见的媒体格式类型(/)如下: ...text/plain:纯文本格式 image/gif:gif图片格式 application/pdf:pdf格式 ...Content-Type实体头部用于指示资源的MIME类型media type。媒体类型决定浏览器将以何种形式对资源进行解析。
常见的媒体格式类型(/)如下:
text/html: HTML格式
text/plain:纯文本格式
image/gif:gif图片格式
application/pdf:pdf格式
application/octet-stream:二进制流数据(如常见的文件下载)
…
复制代码主要的使用场景如下:HTTP头部的ContentType、伪协议的data:
Content-Type
Content-Type实体头部用于指示资源的MIME类型media type。
语法格式:
Content-Type: text/html; charset=utf-8
Content-Type: multipart/form-data; boundary=something
复制代码这里我们重点讨论POST请求时Content-type的几种媒体类型:
application/x-www-form-urlencoded
原生中默认的encType,即表单默认的数据提交格式,提交的数据以key=val形式编码,用&连接,如
POST / HTTP/1.1
Content-Type: application/x-www-form-urlencoded
Content-Length: 14 //注明内容长度name=M2&age=18
复制代码注意:数据被使用URL编码(支持ASCII字符集)。
如果是get请求,上述编码后的数据会被附加在url链接后;
如果是post请求,上述编码后的数据会被放在HTTP的请求体中。
multipart/form-data
对于发送大量二进制数据或者包含non-ASCII字符的文本,application/x-www-form-urlencoded是效率低下的(需要用更多字符表示一个non-ASCII字符)。
因此提交form时,如果数据包含文件上传,non-ASCII数据或者二进制数据时,就需要设定****。
格式举例:
POST / HTTP/1.1
Content-Type:multipart/form-data; boundary=splitTag–splitTag
Content-Disposition: form-data; name=“file”; filename=“chrome.png”
Content-Type: image/png… content of chrome.png …
–splitTag–
复制代码注意:
1)boundary分隔符将数据分为多个Parts,每个Part都包含头部信息;
2)每个部分,使用–boundary开头,以–boundary–结束。
【formData】
利用formData,使用append方法进行key-value的数据添加来模拟表单信息,然后使用XMLHttpRequest发送这个表单信息。键值可以是:文本
文件File对象
Blob对象var form = document.getElementById(“form”);
var xhr = new XMLHttpRequest();
xhr.open(“POST”, “/upload”);
//可以使用form表单初始化
let formData = new FormData(form);
//字符串或对象
formData.append(‘name’, ‘M2’);
//File
formData.append(‘userfile’,fileInputElement.files[0]);
//Blob
let oFileBody=‘hey!’;
let oBlob=new Blob([oFileBody],{type:“text/xml”});
formData.append(“webmasterfile”,oBlob);xhr.send(formData);
复制代码application/json
目前用的较多的一种编码当时。用来告诉服务端消息主体是序列化后的JSON字符串。
方便传输复杂的结构化数据。
Data URL
先简单介绍下伪协议,不同于http://、https://、ftp://,伪协议是为了关联应用程序而使用的,如:
tencent://(关联QQ),
data: //用base64编码在浏览器端输出二进制文件
javascript: //后面的代码当JavaScript来执行,并将结果值返回给当前页面。
复制代码这里我们主要介绍DataURL,其允许内容创建者向文档中嵌入小文件。
格式如下:
data:[][;charset=][;base64],
复制代码应用举例:
//可以将logo图片转为DataURL,内联到src中
复制代码好处: 减少请求数。
缺点: 但是无法对资源做缓存,体积增大。温馨提示:以下案例,可以复制到浏览器网址栏看效果。
Example1:
data:text/plain;base64,SGVsbG8sIFdvcmxkIQ%3D%3D
复制代码指定为base64编码的文本,会被当做文本解码后为:Hello, World
Example2:
data:application/octet-stream;base64,SGVsbG8sIFdvcmxkIQ%3D%3D
复制代码指定为二进制流的形式,则会使用下载的方式处理,文件内容为:Hello, World
Example3:
data:image/gif;base64,SGVsbG8sIFdvcmxkIQ%3D%3D
复制代码如果强行把一段文本指定为image类型,浏览器会按照图片解析,但是显而易见解析不成功。
如果对你有帮助,请点赞 or 在看,谢谢~
获取更多技术干货,欢迎【扫码关注】~ -
MediaType介绍
2020-12-04 15:41:00MediaType媒体类型:决定浏览器将以什么形式、什么编码对资源进行解析 Content-Type:也属于MediaType媒体类型,主要用于在请求头中指定资源的MediaType 一、MediaType类型 类型 描述 text/html HTML格式 ...MediaType媒体类型:决定浏览器将以什么形式、什么编码对资源进行解析
Content-Type:也属于MediaType媒体类型,主要用于在请求头中指定资源的MediaType
一、MediaType类型
类型 描述 text/html HTML格式 text/plain 纯文本格式,空格转换为 “+” 加号,但不对特殊字符编码 text/xml XML格式 text/x-markdown Markdown格式 image/gif gif图片格式 image/jpeg jpg图片格式 image/png png图片格式 application/xhtml+xml XHTML格式 application/xml XML数据格式 application/json 用来告诉服务端,消息主体是序列化后的JSON字符串 application/pdf pdf格式 application/msword Word文档格式 application/octet-stream 二进制流数据(如常见的文件下载) application/x-www-form-urlencoded 参数为键值对形式,在发送前编码所有字符(默认)。浏览器的原生 <form encType=”” 表单提交类型,如果不设置 enctype 属性,那么最终就会以 application/x-www-form-urlencoded 方式提交数据 multipart/form-data 不对字符编码,发送大量二进制数据或包含non-ASCII字符的文本,application/x-www-form-urlencoded是效率低下的(需要用更多字符表示一个non-ASCII字符)。需要设定“ <form enctype=‘multipart/form-data’” 二、MediaType对象解析
MediaType对象包含了三种信息:type 、subtype、charset,一般将这些信息传入parse()方法中,这样就可以解析出MediaType对象
例子1:
text/x-markdown; charset=utf-8
- type值是text,表示是文本这一大类;
- / 后面的x-markdown是subtype,表示是文本这一大类下的markdown这一小类;
- charset=utf-8 则表示采用UTF-8编码
-
Java MediaType.MULTIPART_FORM_DATA属性代码示例
2021-03-08 18:05:50/*** Tries to determine the content/media type of this HTTP request iff the HTTP "Content-Type"* header was not explicitly set by the user, otherwise the user provided value is used. If the* "Content-.../**
* Tries to determine the content/media type of this HTTP request iff the HTTP "Content-Type"
* header was not explicitly set by the user, otherwise the user provided value is used. If the
* "Content-Type" HTTP header value is null, then the content/media/payload of this HTTP request
* is inspected to determine the content type.
*
* The simplest evaluation sets the content type to "application/x-www-form-urlencoded" if this is
* a POST or PUT HTTP request, unless any request parameter value is determined to have multiple
* parts, the the content type will be "multipart/form-data".
*
*
* @param defaultContentType the default content/media type to use when the content type cannot be
* determined from this HTTP request.
* @return a MediaType for the value of the HTTP Content-Type header as determined from this HTTP
* request.
* @see #getHeaders()
* @see org.springframework.http.HttpHeaders#getContentType()
* @see org.springframework.http.MediaType
*/
protected MediaType determineContentType(final MediaType defaultContentType) {
MediaType contentType = getHeaders().getContentType();
// if the content type HTTP header was not explicitly set, try to determine the media type from
// the content body
// of the HTTP request
if (contentType == null) {
if (isPost() || isPut()) {
OUT: for (final String name : getParameters().keySet()) {
for (final Object value : getParameters().get(name)) {
if (value != null && !(value instanceof String)) {
contentType = MediaType.MULTIPART_FORM_DATA;
break OUT;
}
}
}
// since this is a POST/PUT HTTP request, default the content/media type to
// "application/x-www-form-urlencoded"
contentType = ObjectUtils.defaultIfNull(contentType, MediaType.APPLICATION_FORM_URLENCODED);
} else {
// NOTE the "Content-Type" HTTP header is not applicable to GET/DELETE and other methods of
// HTTP requests
// since there is typically no content (media/payload/request body/etc) to send. Any request
// parameters
// are encoded in the URL as query parameters.
}
}
return ObjectUtils.defaultIfNull(contentType, defaultContentType);
}
-
Ajax请求(415 Unsupported Media Type)
2021-08-07 07:35:48Unsupported media type-415(不支持的媒体类型)该错误类型是后台接收参数为json类型的,然而ajax提交的类型不对,如下:异常代码:$.ajax({url: api + "/sendMessage",type: "post",dataType : "json",cache: false,... -
Http 415:Unsupported Media Type
2022-02-18 17:44:17问题 在用POST方式携带token访问一个API接口时,发现返回状态码为415,错误信息为“Unsupported Media Type” 解决方法 在请求头加入"Content-Type":“application/json” 再次请求,发送成功 -
Java-Class-C:org.springframework.http.MediaType
2019-06-24 17:47:00ylbtech-Java-Class-C:org.springframework.http.MediaType 1.返回顶部 1.1、 /* * Copyright 2002-2018 the original author or authors. * * Licensed under the Apache Lice.... -
Okhttp的MediaType
2020-02-18 11:30:361、MIME 类型 MIME (MultipurposeInternetMailExtensions) 是描述消息内容类型的因特网标准。 MIME 消息能包含文本、图像、...MediaType指的是要传递的数据的MIME类型。 2.1、MediaType的构造函数 private M... -
321_MediaType Media Type 是什么
2016-10-14 21:42:37MediaType是什么 MediaType在网络协议的消息头里面叫做Content-Type 使用两部分的标识符来确定一个类型 所以我们用的时候其实就是为了表明我们传的东西是什么类型 比如 application/json:JSON格式的数据,在RFC ... -
Java MediaType.APPLICATION_JSON_UTF8屬性代碼示例
2021-03-08 18:06:07/** * Searches {@link org.springframework.web.bind.... } catch (InvalidMediaTypeException exception) { mediaType = MediaType.APPLICATION_JSON_UTF8; } return new Mapping(httpMethod, url, mediaType); } -
QT http访问时,提示Unsupported Media Type
2022-01-21 16:00:52在使用QNetworkAccessManager进行网络访问时,提示“Unsupported Media Type”,状态码415。 状态码415:表示服务器无法处理请求的媒体格式。 解决方案 出现415错误,有一种情况和请求报文中Header的Content-Type... -
http文件传输类型——MediaType
2021-04-06 20:42:31目录一、什么是MediaType?二、MediaType有哪些类型? 一、什么是MediaType? MediaType(Internet Media Type):互联网媒体类型,也称为MIME类型,它一般会被携带在http协议的请求头中,使用Content-Type字段名来... -
关于HTTP请求返回415错误UnsupportedMediaType定位问题
2021-11-23 16:33:54@TO关于HTTP请求返回415错误UnsupportedMediaType定位问题 前言:这是我的第一篇博客,很多知识也是正在学习和积累中。以后会记录一些技术上遇到的问题和总结一些知识点 。 今天在工作中,发现我再调用甲方API接口的... -
Unsupported Media Type 415问题解决办法(Ajax)
2021-08-07 07:35:50场景:Ajax传一个json对象到...错误类型错误类型1:"status":415"error":"Unsupported Media Type""exception":"org.springframework.web.HttpMediaTypeNotSupportedException"原因:没有使用JSON.stringify(rowDa... -
记录一次Unsupported Media Type解决方案
2021-03-30 22:09:05笔者今天碰到一个问题,就是自身的一个接口被第三方的业务回调的时候出现了一个415的错误,也就是Unsupported Media Type。之后查了些资料,总结一下。 文章目录一、415是什么错误?二、两种解决办法三、顺便记录... -
OkHttp初探3:简单文件上传、表单文件一起上传、带进度条的文件上传、MediaType介绍。Kotlin版本
2022-01-09 15:38:30文件上传;表单文件一起上传;带进度条的文件上传;MediaType介绍。Kotlin版本 -
axios POST请求 415 Unsupported Media Type
2021-03-02 09:41:31首先保证前后台 都是用参数JSON请求接受。 其次保证请求的参数是不是在请求中传入了。 -
【http-MediaType】Okhttp的MediaType.parse属性
2019-07-23 18:23:47关于MediaType的详细介绍 MediaType指的是要传递的数据的MIME类型,MediaType对象包含了三种信息:type 、subtype以及charset,一般将这些信息传入parse()方法中,这样就可以解析出MediaType对象,比如 "text/x-... -
okhttp的MediaType.parse属性
2019-03-20 15:44:58MediaType指的是要传递的数据的MIME类型,MediaType对象包含了三种信息:type 、subtype以及charset,一般将这些信息传入parse()方法中,这样就可以解析出MediaType对象,比如 “text/x-markdown; charset=utf-8” ... -
http请求415,报错Unsupported Media Type
2019-07-17 14:21:53原因:根据接口所传参数的不同,post请求的Content-type有四种: application/x-www-form-urlencoded(默认) application/xml application/json multipart/form-data 我所对接的接口所传的... -
HTTP Status 415 – Unsupported Media Type
2019-06-03 11:04:55HTTP Status 415 – Unsupported Media Type 今天在测试springmvc的restful接口时候遇到了一个问题:通过body传参报错 HTTP Status 415 – Unsupported Media Type 简述restful接口传参方式 restful推荐的传参... -
Java Status 415 – Unsupported Media Type问题解决
2021-08-15 15:57:25"error": "Unsupported Media Type", "path": "/create" } 问题分析: 1、使用PostMan调试后端接口时,请求体里面的content type为multipart/form-data,但是后端接口不支持text/plain,只支持application/json... -
Postman报错Unsupported Media Type
2019-10-22 16:13:31报错信息如下: { "timestamp": "2018-12-06T01:37:11.184+0000", ..."error": "Unsupported Media Type", "message": "Content type 'text/plain;charset=UTF-8' not supported", "path": "/testgu/ycyzha... -
openvino CvCapture_MSMF::initStream Failed to set mediaType (unsupported media type)
2020-07-17 17:43:32msmf.cpp (680) CvCapture_MSMF::initStream Failed to se t mediaType (stream 0, (0x0 @ 1) MFVideoFormat_RGB24(unsupported media type) Traceback (most recent call last): File "video_test.py", line 8, in ... -
MediaType对应的各种类型参数
2020-05-12 14:31:11https://tool.oschina.net/commons -
Java MediaType.APPLICATION_JSON_UTF8属性代码示例
2021-03-08 15:39:23/** * Searches {@link org.springframework.web.bind.... } catch (InvalidMediaTypeException exception) { mediaType = MediaType.APPLICATION_JSON_UTF8; } return new Mapping(httpMethod, url, mediaType); } -
Jmeter-请求接口时返回415状态码,提示“ Unsupported Media Type
2021-12-17 10:18:30如图,在通过Jmeter请求接口时,返回了415状态码,在响应数据中提示"Unsupported Media Type" http 415状态码是指的对于当前请求的方法和所请求的资源,请求中提交的实体并不是服务器中所支持的格式,因此请求被... -
Python requests post 请求报错:415 Unsupported Media Type
2021-02-23 23:38:26requests post 请求报错:415 Unsupported Media Type 在使用response.post发送json数据时,出现如题所示错误,是因为User-Agent 被服务器设置 拒绝请求了 解决方法: 'content-type': 'application/json' headers...