- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
目前实现的 REST 端点如下:
@RequestMapping(path = "/login/user/{username:.+}", method = POST, produces = "application/json; charset=utf-8")
@ResponseStatus(code = HttpStatus.OK)
public User userLogin(@PathVariable("username") String username, @RequestBody Password password) {
//do stuff
return new User(UUID.randomUUID());
}
我目前使用电子邮件地址作为用户名,当我使用以 .au 结尾的用户名时,端点返回 406 内容 Not Acceptable 。
我试着把上面的改成这个
@RequestMapping(path = "/login/user/{username:.+}", method = POST, produces = "application/json; charset=utf-8")
@ResponseStatus(code = HttpStatus.OK)
public String userLogin(@PathVariable("username") String username, @RequestBody Password password) {
//do stuff
return "blah";
}
当我访问它时,它会提示我下载一个 .au 文件(由 Sun microsystems 制作的音频格式...),其中包含“blah”。如果我在方法中随时检查用户名的值,我会得到正确的电子邮件地址,其中包含 .au。
我猜 Spring 堆栈中的某些东西正在解析 .au 并尝试强制执行不同的媒体类型,所以现在它忽略了 application/json
最佳答案
我最近遇到了同样的问题并找到了问题所在。想在这里分享它,因为它会帮助别人。 @Patrick 解释的这种行为似乎是由于 Spring MVC 中的基于 URL(URL 后缀)的内容协商而发生的。
什么是内容协商?
在某些情况下,我们必须处理 Controller 返回的相同数据的多个表示(或 View )。确定要返回的数据格式称为内容协商。
内容协商如何运作?
当通过 HTTP 发出请求时,可以通过设置 Accept
header 属性来指定您想要的响应类型。但是,浏览器实际上会发送非常困惑的 Accept
header ,这使得依赖它们变得不切实际。因此,Spring 为内容协商提供了一些替代约定。
Spring 内容协商备选方案 - URL 后缀和/或 URL 参数
These work alongside the use of
Accept
headers. As a result, the content-type can be requested in any of three ways. By default they are checked in this order:
Add a path extension (suffix) in the URL. So, if the incoming URL is something like http://myserver/myapp/accounts/list.html then HTML is required. For a spreadsheet the URL should be http://myserver/myapp/accounts/list.xls. The suffix to media-type mapping is automatically defined via the JavaBeans Activation Framework or JAF (so
activation.jar
must be on the class path).A URL parameter like this: http://myserver/myapp/accounts/list?format=xls. The name of the parameter is format by default, but this may be changed. Using a parameter is disabled by default, but when enabled, it is checked second.
- Finally the
Accept
HTTP header property is checked. This is how HTTP is actually defined to work, but, as previously mentioned, it can be problematic to use.
在问题中解释的上述案例中,您看到的是基于路径扩展的内容协商在进行中。 (.au)
来自 ContentNegotiationConfigurer 的 hava 文档,
favorPathExtension
public ContentNegotiationConfigurer favorPathExtension(boolean favorPathExtension)
Whether the path extension in the URL path should be used to determine the requested media type.
By default this is set to true in which case a request for /hotels.pdf will be interpreted as a request for "application/pdf" regardless of the 'Accept' header.
解决方案 - 将 favorPathExtension 设置为 false
@Configuration
@EnableWebMvc
public class WebConfig extends WebMvcConfigurerAdapter {
@Override
public void configureContentNegotiation(ContentNegotiationConfigurer configurer) {
configurer.favorPathExtension(false).
favorParameter(true).
parameterName("mediaType").
ignoreAcceptHeader(true).
useJaf(false).
defaultContentType(MediaType.APPLICATION_JSON).
mediaType("xml", MediaType.APPLICATION_XML).
mediaType("json", MediaType.APPLICATION_JSON);
}
}
<bean id="contentNegotiationManager" class="org.springframework.web.accept.ContentNegotiationManagerFactoryBean">
<property name="favorPathExtension" value="false" />
<property name="favorParameter" value="true" />
<property name="parameterName" value="mediaType" />
<property name="ignoreAcceptHeader" value="true"/>
<property name="useJaf" value="false"/>
<property name="defaultContentType" value="application/json" />
<property name="mediaTypes">
<map>
<entry key="json" value="application/json" />
<entry key="xml" value="application/xml" />
</map>
</property>
</bean>
请注意,除了将 favorPathExtension 设置为 false 之外,上述配置还有一些其他更改。
有关这方面的更多详细信息,请参见 here .
仅作为补充,我们得到的问题回复如下。
{
"timestamp": 1518691842254,
"status": 406,
"error": "Not Acceptable",
"exception": "org.springframework.web.HttpMediaTypeNotAcceptableException",
"message": "Not Acceptable",
"path": "/rest/token/something.au"
}
关于java - Spring 在 URI 上更改媒体类型,最后带有 .au,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41262661/
我想知道,A 和 AU 在 Jenkins 中是什么意思?我现在正在看着我的东西通过控制台运行,只是想知道。似乎在谷歌上根本没有关于此的任何信息。 (话又说回来,它可能只是不喜欢这么短的搜索词) 这就
我想知道,A 和 AU 在 Jenkins 中是什么意思?我现在正在看着我的东西通过控制台运行,只是想知道。似乎在谷歌上根本没有关于此的任何信息。 (话又说回来,它可能只是不喜欢这么短的搜索词) 这就
我正在为我的 (iPhone 4+) 应用开发自定义采样器和音频引擎,特别是添加录音和发送效果器功能。我一直在尝试决定是沿着一个大的 RemoteIO 渲染回调处理所有事情的路线,还是将其分解为单独的
我尝试在网上搜索资源非常有限。在操作执行方法中: actionPerformed(){ --------------- new Sound();} 在声音类中 public Sound(){
显然Java只播放.au音频文件,而我想播放mp3文件。我想将它们转换为 au 并且不想使用任何第三方库直接播放 mp3。您能否告诉我如何准备 mp3 文件以便能够在 Java 中播放它们。 (具体来
我有一条线。我正在从左向右移动它。这很好。但是现在我想掉头或像蛇一样转动它。我该怎么做? .top { position: absolute; width: 100px; height:
我正在尝试编写一个非常简单的程序,它以公里为单位打印到任何行星的当前距离。我正在使用天域。这是我的火星代码: from skyfield.api import earth, mars, now ra,
这是我现在如何获取货币符号的代码: NSLocale *lcl = [[[NSLocale alloc] initWithLocaleIdentifier:@"au_AU"] autorelease]
我正在尝试读取 C 语言中的 .au 文件的 header ,并且我将所有值存储在 struct 中,但在转换时遇到问题它们为 Little Endian 格式。 我偶然发现了一种方法,ntohl,它
我编写了一个小程序来从 .au 文件中获取魔数(Magic Number)并将其打印到控制台,但每次尝试时,我都没有得到预期的 .snd,而是得到了 .snd$ 代替。 我不确定为什么会这样,考虑到我
我正在尝试将现有项目转换为使用 aurelia cli,但我遇到了一个老问题。我收到 regeneratorRuntime is not defined 错误。我在以前的版本中修复了这个问题,但要么我
更新: 我正在更改我的代码以更简化的方式说明问题。我还有一个小错误,虽然没有阻止这个问题,但确实增加了一些困惑。 我在 iOS (kAudioUnitSubType_MultiChannelMixer
我有一个国际化的 C#/WPF 应用程序(某些语言的资源字符串)。 目前我正在为资源文件使用以下代码:default.resx、de-DE、fr-FR、es-ES、pt-BR、pt-PT、nl-NL等
我有一个基于 php 的 Web 应用程序,我想在其中根据用户所在的国家/地区(例如 nz、au、in 等)将用户重定向到子域。 最好的方法是什么? Htaccess?PHP?JavaScript?
目前实现的 REST 端点如下: @RequestMapping(path = "/login/user/{username:.+}", method = POST, produces = "appl
我刚刚将 aurelia 应用程序升级到最新版本,现在发现它在运行时失败 - 构建期间没有错误报告。失败 Uncaught TypeError: Cannot read property '__use
正如标题所说。我究竟做错了什么?我正在使用 Netbeans,我为我的第 3 方库创建了一个文件夹,右键单击包,导航: Properties > Libraries > Add JAR/Folder
我一直在使用 iOS LoadPresetDemo示例代码 - 如果加载 AUPreset 文件以配置不同类型的采样器(非常酷) - 并且遇到了问题/问题。 演示代码运行良好,但是当我尝试在从头构建的
我有 node v4.6.1安装在我的系统上。当我尝试安装 aurelia-cli 时通过执行 npm install aurelia-cli -g ,安装通过。这是输出。 npm install -
在 EXTRACT 文档中,有对 gzip 压缩文件(我们正在使用的)的(很棒的)自动魔法支持。 但是我应该假设它不会使用多个 AU 吗?好像我理解正确一样,文件需要“可拆分”才能跨 AU 传播? 或
我是一名优秀的程序员,十分优秀!