在我的Ruby on Rails应用程序中,我尝试以Base64格式通过POSTMAN REST客户端上传图像.当我发布图像时,我得到了406 Not Acceptable Response.当我检查我的数据库时,图像就在那里并成功保存.
这个错误的原因是什么,我需要在标题中指定什么?
我的请求:
网址--- http://localhost:3000/exercises.json
标题:
Content-Type - application/json
Run Code Online (Sandbox Code Playgroud)
原始数据:
{
"exercise": {
"subbodypart_ids": [
"1",
"2"
],
"name": "Exercise14"
},
"image_file_name": "Pressurebar Above.jpg",
"image":"******base64 Format*******"
}
Run Code Online (Sandbox Code Playgroud) 我正在使用@ResponseBody返回Spring MVC中的Json对象.它在版本4.0.7和3.2.11上按预期工作,但当我尝试使用最新的Spring版本4.1.1(截至10/16)而没有任何其他配置更改时,它返回HTTP状态406.这被认为是一个错误还是4.1.1需要不同的配置?
最新的jackson jar已经在classpath中了
<dependency>
<groupId>org.codehaus.jackson</groupId>
<artifactId>jackson-mapper-asl</artifactId>
<version>1.9.13</version>
</dependency>
Run Code Online (Sandbox Code Playgroud)
Spring 文档上的示例运行正常
@RequestMapping(value = "/something", method = RequestMethod.PUT)
@ResponseBody
public String helloWorld() {
return "Hello World";
}
Run Code Online (Sandbox Code Playgroud)
当返回类型是String时.当返回类型是POJO时会发生问题.
当我尝试使用 REST API 时出现此错误:
Exception in thread "main" org.springframework.web.client.HttpClientErrorException: 406 Not Acceptable
Run Code Online (Sandbox Code Playgroud)
这是执行的客户端代码:
public static void main(String[] args) {
Car c = getCarById(4);
System.out.println(c);
}
public static @ResponseBody Car getCarById(int id){
return new RestTemplate().getForObject("http://localhost:8080/rest/cars/{id}", Car.class, id);
}
Run Code Online (Sandbox Code Playgroud)
这是映射请求的控制器的代码:
@RequestMapping(value="/cars/{id}", method=RequestMethod.GET, headers = {"Accept=text/html,application/xhtml+xml,application/xml"}, produces="application/xml")
public @ResponseBody Car getCarById(@PathVariable("id") int id){
return carService.getCarById(id);
}
Run Code Online (Sandbox Code Playgroud)
尽管映射器应该负责映射到正确的类型,但为什么会发生此错误(406-Not Acceptable)?
我正在Web API 2.2上构建OData 3服务.
该服务正确返回我的实体的元数据,但406 Not Available在查询其中一个实际实体时返回.我做了很多研究(我目前正在学习几个教程),但我还没有找到任何实际工作的东西.
这是我的WebApiConfig:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web.Http;
using System.Web.OData.Builder;
using System.Web.OData.Extensions;
namespace MyProject
{
public static class WebApiConfig
{
public static void Register(HttpConfiguration config)
{
ODataModelBuilder builder = new ODataConventionModelBuilder();
builder.EntitySet<MarvelCharacter>("MarvelCharacters");
config.MapODataServiceRoute(
routeName: "Marvel",
routePrefix: "dude",
model: builder.GetEdmModel());
}
}
}
Run Code Online (Sandbox Code Playgroud)
和我的控制器(不完整,但你明白了):
using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Net;
using System.Net.Http;
using System.Web.Http;
using System.Web.Http.OData;
using System.Web.Http.OData.Query;
using Microsoft.Data.OData;
using MyProject;
namespace MyProject.Controllers
{
public class …Run Code Online (Sandbox Code Playgroud) 我是Spring Boot的新手,我可能在犯一些愚蠢的错误,因此Appologies会提前提出此类问题。我正在尝试编写接受以下JSON的POST API:
{
"id" : null,
"a": 1.3,
"b": "somestring",
"mapJson" :
{
"monday" : "10:00-12:00/n14:00-18:00",
"tuesday" : "10:00-12:00/n14:00-18:00",
"wednesday" : "10:00-12:00/n14:00-18:00",
"thursday" : "10:00-12:00/n14:00-18:00",
"friday" : "10:00-12:00/n14:00-18:00",
"saturday" : "10:00-12:00/n14:00-18:00",
"sunday" : "10:00-12:00/n14:00-18:00"
},
"list" : ["cc","paytm","debit"]
}
Run Code Online (Sandbox Code Playgroud)
考虑遵循DTO类AbcDTO:
package com.abb.dto;
import java.util.List;
import com.abb.entities.OpeningHrs;
import lombok.Data;
@SuppressWarnings("unused")
@Data
public class AbcDTO {
private Long id;
private Double a;
private String b;
private MapJson mapJson;
private List<String> list;
}
Run Code Online (Sandbox Code Playgroud)
OpeningHrs是用于映射Json Map结构的类,
package com.abb.entities;
import lombok.Data; …Run Code Online (Sandbox Code Playgroud) 我正在关注Railscast#199,以便在移动浏览器中查看我的网络应用程序.除非我尝试在移动版本中使用UJS访问选项卡式界面中的信息,否则它的效果很好.单击选项卡可在Web应用程序中运行,但在移动端,我收到406错误.(我在Safari中将用户代理设置为iPhone后尝试了这一点.我还在iOS模拟器和我的iPhone上进行了测试.两者都没有加载任何东西.)
下面是其中一个选项卡的一些代码.任何人都可以帮我定位正在发生的事情吗?这是我的代码.
以下是profile_about操作profiles_controller.rb:
def profile_about
@profile = Profile.find(params[:id])
respond_to do |format|
format.js { render :layout => nil }
end
end
Run Code Online (Sandbox Code Playgroud)
在我profiles/show.mobile.erb(这是完全相同的代码profiles/show.html.erb):
<div id="tabs">
<ul id="infoContainer">
<li><%= link_to "Cred", profile_cred_profile_path, :class=> 'active', :remote => true %></li>
<li><%= link_to "About", profile_about_profile_path, :class=> 'inactive', :remote => true %></li>
</ul>
<div id="tabs-1">
<%= render :partial => 'profile_cred' %>
</div>
</div><!-- end tabs -->
Run Code Online (Sandbox Code Playgroud)
(注意:我有一个文件profiles/_profile_about.html.erb和profiles/_profile_about.mobile.erb.)
这是我的profiles/profile_about.js.erb:
$("#tabs-1").html("<%= escape_javascript(render(:partial => 'profile_about'))%>");
Run Code Online (Sandbox Code Playgroud)
我的Heroku日志显示406: …
我正在尝试使用Java创建RESTful服务,使用多个教程和许多很多StackOverflow条目.不幸的是我似乎无法让我的代码工作,当我尝试命中端点时,我坚持不懈地获得Http 406.任何帮助表示赞赏.
GreetingController.java:
import java.util.Random;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
@Controller
@RequestMapping("/greeting")
public class GreetingController {
protected final Logger log = LoggerFactory.getLogger(GreetingController.class);
private static final String template = "Hello, %s!";
private static Random rand = new Random();
@RequestMapping(method = RequestMethod.GET, headers="Accept=*/*")//, value="/{name}")
@ResponseBody
public Greeting greeting() {
log.debug("Entered greeting()");
return new Greeting(rand.nextInt(99999999),
String.format(template, "Stephen"));
}
@RequestMapping(method = RequestMethod.GET, value="/{name}")
@ResponseBody
public Greeting greetingName(@PathVariable String name) {
log.debug("Entered greetingName()");
return new Greeting(rand.nextInt(99999999), …Run Code Online (Sandbox Code Playgroud) 在我的Spring XML中,我有以下代码段:
<bean class="org.springframework.web.servlet.mvc.annotation.DefaultAnnotationHandlerMapping">
<property name="useDefaultSuffixPattern" value="false"/>
</bean>
<mvc:annotation-driven>
<mvc:message-converters>
<bean class="org.springframework.http.converter.json.MappingJacksonHttpMessageConverter">
<property name="objectMapper" ref="objectMapper" />
</bean>
</mvc:message-converters>
</mvc:annotation-driven>
Run Code Online (Sandbox Code Playgroud)
据我了解,这意味着春天应该不登记"ABC*"和"ABC /"当我有"ABC"的映射.
在我的一个控制器中,我有一个将图像写入响应的方法:
@RequestMapping(value="{path}", method=RequestMethod.GET, produces=MediaType.IMAGE_PNG_VALUE)
@ResponseBody
public void getPath(
@PathVariable String path,
HttpServletResponse res) {
...
}
Run Code Online (Sandbox Code Playgroud)
当我请求像"abc"这样的东西时,这很有用,但是当我请求"abc.com"时,它会在文本中抛出406错误:
The resource identified by this request is only capable of generating responses with characteristics not acceptable according to the request "accept" headers."
Run Code Online (Sandbox Code Playgroud)
当我请求"abc.img"时,"path"参数只接收文本"abc"; Spring省略了扩展名.
看来Spring似乎没有正确地忽略后缀模式.为什么是这样?
编辑:我从Dirk的评论中翻译了java配置,以下XML似乎解决了这个问题:
<bean id="contentNegotiationManager" class="org.springframework.web.accept.ContentNegotiationManagerFactoryBean">
<property name="favorPathExtension" value="false" />
</bean>
<mvc:annotation-driven content-negotiation-manager="contentNegotiationManager">
<mvc:message-converters>
<bean …Run Code Online (Sandbox Code Playgroud) 我正在使用Spring MVC 4.1.5及其REST支持尝试使用谷歌协议缓冲区消息格式的Web服务.我看过很多帖子都提到了JSON格式的这个问题,但没有一个用google协议缓冲区格式.另外我没有看到Spring框架文档有一个用于protobufs的MediaType(下面有更多内容)我的代码发布在github上
这是我的控制器代码(使用或不使用produce ="application-xprotobuf"没有区别 - 相同406不可接受的错误)
@RestController
@RequestMapping("/ws")
@EnableWebMvc
public class CustomerRestController {
@Autowired
private CustomerRepository customerRepository;
//@RequestMapping(value="/customers/{id}", method = RequestMethod.GET, produces="application/x-protobuf")
@RequestMapping(value="/customers/{id}", method = RequestMethod.GET)
public CustomerProtos.Customer customer(@PathVariable Integer id) {
return this.customerRepository.findById(id);
}
@Bean
ProtobufHttpMessageConverter protobufHttpMessageConverter() {
return new ProtobufHttpMessageConverter();
}
private CustomerProtos.Customer customer(int id, String f, String l, Collection<String> emails) {
String emailAddressString = "defaultEmail@address.com";
for (String email : emails) {
emailAddressString = email;
break;
}
EmailAddress emailAddress = CustomerProtos.Customer.EmailAddress.newBuilder()
.setType(CustomerProtos.Customer.EmailType.PROFESSIONAL).setEmail(emailAddressString).build();
return CustomerProtos.Customer.newBuilder().setFirstName(f).setLastName(l).setId(id).addEmail(emailAddress)
// .addAllEmail(emailAddresses) …Run Code Online (Sandbox Code Playgroud) 当我检测到特定的客户端错误时,我只是尝试设置(实际上是通过将最大年龄设置为0来删除)会话cookie。我正在使用的HTTP响应来自4xx类别(例如401、406等)。
Cookie删除与服务器端生成的这种响应配合得很好:
Response resp = Response.status(Response.Status.OK).header(
"Set-Cookie",
cookieName+"="+sessionId+"; "+
"Version=1; Max-Age=0; Path=" + cookiePath + "; " +
"Expires=Thu, 01 Jan 1970 00:00:00 GMT").entity("").build();
Run Code Online (Sandbox Code Playgroud)
...但是失败了:
Response resp = Response.status(Response.Status.UNAUTHORIZED).header(
"Set-Cookie",
cookieName+"="+sessionId+"; "+
"Version=1; Max-Age=0; Path=" + cookiePath + "; " +
"Expires=Thu, 01 Jan 1970 00:00:00 GMT").entity("").build();
Run Code Online (Sandbox Code Playgroud)
(仅相差:200 => 406)。
不能使用4xx响应设置cookie吗?
rest ×4
spring ×4
spring-mvc ×4
json ×3
http ×2
java ×2
.net ×1
c# ×1
cookies ×1
mime-types ×1
mobile ×1
odata ×1
spring-boot ×1
uri ×1
xml ×1