Spring XML 406错误

2024-04-23

我正在尝试使用 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),
                            String.format(template, name));
    }
}

问候语.java:

import javax.xml.bind.annotation.XmlAccessType;
import javax.xml.bind.annotation.XmlAccessorType;
import javax.xml.bind.annotation.XmlElement;
import javax.xml.bind.annotation.XmlRootElement;

@XmlRootElement(name="greeting")
@XmlAccessorType(XmlAccessType.PUBLIC_MEMBER)
public class Greeting {

    private Integer id;
    private String content;

    public Greeting() {

    }

    public Greeting(Integer id, String content) {
        this.id = id;
        this.content = content;
    }

    @XmlElement
    public void setId(Integer id) {
        this.id = id;
    }

    public Integer getId() {
        return id;
    }

    @XmlElement
    public void setContent(String content) {
        this.content = content;
    }

    public String getContent() {
        return content;
    }
}

x-servlet.xml:

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
    xmlns:security="http://www.springframework.org/schema/security"
    xmlns:context="http://www.springframework.org/schema/context"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xmlns:oxm="http://www.springframework.org/schema/oxm"
    xmlns:mvc="http://www.springframework.org/schema/tx"
    xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
        http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd
        http://www.springframework.org/schema/oxm http://www.springframework.org/schema/oxm/spring-oxm-3.0.xsd
        http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.0.xsd
        http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd">

    <context:component-scan base-package="com.package.name" />

    <mvc:annotation-driven/>

    <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
      <property name="prefix">
            <value>/WEB-INF/pages/</value>
      </property>
      <property name="suffix">
            <value>.jsp</value>
      </property>
    </bean>

</beans>

Http 标头(通过 firebug):

Response Headers
Content-Language    en
Content-Length  1067
Content-Type    text/html;charset=utf-8
Date    Tue, 18 Feb 2014 20:24:13 GMT
Server  Apache-Coyote/1.1

Request Headers
Accept  text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8
Accept-Encoding gzip, deflate
Accept-Language en-US,en;q=0.5
Connection  keep-alive
Cookie  JSESSIONID=9064B3E9F1C3259B73C65B022C8BDC75
Host    localhost:8080
User-Agent  Mozilla/5.0 (Windows NT 6.0; WOW64; rv:26.0) Gecko/20100101 Firefox/26.0

启动 tomcat 并访问所述服务器上的不同页面时记录:

DEBUG o.s.security.web.FilterChainProxy - Converted URL to lowercase, from: '/greeting'; to: '/greeting'
DEBUG o.s.security.web.FilterChainProxy - Candidate is: '/greeting'; pattern is /**; matched=true
DEBUG o.s.security.web.FilterChainProxy - /greeting at position 1 of 12 in additional filter chain; firing Filter: 'org.springframework.security.web.access.channel.ChannelProcessingFilter@9a6bbb'
DEBUG o.s.s.w.a.i.DefaultFilterInvocationSecurityMetadataSource - Converted URL to lowercase, from: '/greeting'; to: '/greeting'
DEBUG o.s.s.w.a.i.DefaultFilterInvocationSecurityMetadataSource - Candidate is: '/greeting'; pattern is /**; matched=true
DEBUG o.s.s.w.a.c.ChannelProcessingFilter - Request: FilterInvocation: URL: /greeting; ConfigAttributes: [ANY_CHANNEL]
DEBUG o.s.security.web.FilterChainProxy - /greeting at position 2 of 12 in additional filter chain; firing Filter: 'org.springframework.security.web.context.SecurityContextPersistenceFilter@16fcc4'
DEBUG o.s.s.w.c.HttpSessionSecurityContextRepository - No HttpSession currently exists
DEBUG o.s.s.w.c.HttpSessionSecurityContextRepository - No SecurityContext was available from the HttpSession: null. A new one will be created.
DEBUG o.s.security.web.FilterChainProxy - /greeting at position 3 of 12 in additional filter chain; firing Filter: 'org.springframework.security.web.authentication.logout.LogoutFilter@1db52c8'
DEBUG o.s.security.web.FilterChainProxy - /greeting at position 4 of 12 in additional filter chain; firing Filter: 'org.springframework.security.web.authentication.UsernamePasswordAuthenticationFilter@17d6c1'
DEBUG o.s.security.web.FilterChainProxy - /greeting at position 5 of 12 in additional filter chain; firing Filter: 'org.springframework.security.web.authentication.ui.DefaultLoginPageGeneratingFilter@1144823'
DEBUG o.s.security.web.FilterChainProxy - /greeting at position 6 of 12 in additional filter chain; firing Filter: 'org.springframework.security.web.authentication.www.BasicAuthenticationFilter@8c02cc'
DEBUG o.s.security.web.FilterChainProxy - /greeting at position 7 of 12 in additional filter chain; firing Filter: 'org.springframework.security.web.savedrequest.RequestCacheAwareFilter@bbd1b'
DEBUG o.s.security.web.FilterChainProxy - /greeting at position 8 of 12 in additional filter chain; firing Filter: 'org.springframework.security.web.servletapi.SecurityContextHolderAwareRequestFilter@ac576f'
DEBUG o.s.security.web.FilterChainProxy - /greeting at position 9 of 12 in additional filter chain; firing Filter: 'org.springframework.security.web.authentication.AnonymousAuthenticationFilter@15daa9e'
DEBUG o.s.s.w.a.AnonymousAuthenticationFilter - Populated SecurityContextHolder with anonymous token: 'org.springframework.security.authentication.AnonymousAuthenticationToken@9055c2bc: Principal: anonymousUser; Credentials: [PROTECTED]; Authenticated: true; Details: org.springframework.security.web.authentication.WebAuthenticationDetails@b364: RemoteIpAddress: 0:0:0:0:0:0:0:1; SessionId: null; Granted Authorities: ROLE_ANONYMOUS'
DEBUG o.s.security.web.FilterChainProxy - /greeting at position 10 of 12 in additional filter chain; firing Filter: 'org.springframework.security.web.session.SessionManagementFilter@6c9f0f'
DEBUG o.s.security.web.FilterChainProxy - /greeting at position 11 of 12 in additional filter chain; firing Filter: 'org.springframework.security.web.access.ExceptionTranslationFilter@13ec758'
DEBUG o.s.security.web.FilterChainProxy - /greeting at position 12 of 12 in additional filter chain; firing Filter: 'org.springframework.security.web.access.intercept.FilterSecurityInterceptor@917cb0'
DEBUG o.s.s.w.a.i.DefaultFilterInvocationSecurityMetadataSource - Converted URL to lowercase, from: '/greeting'; to: '/greeting'
DEBUG o.s.s.w.a.i.DefaultFilterInvocationSecurityMetadataSource - Candidate is: '/greeting'; pattern is /welcome*; matched=false
DEBUG o.s.s.w.a.i.DefaultFilterInvocationSecurityMetadataSource - Candidate is: '/greeting'; pattern is /**; matched=true
DEBUG o.s.s.w.a.i.FilterSecurityInterceptor - Secure object: FilterInvocation: URL: /greeting; Attributes: [IS_AUTHENTICATED_ANONYMOUSLY]
DEBUG o.s.s.w.a.i.FilterSecurityInterceptor - Previously Authenticated: org.springframework.security.authentication.AnonymousAuthenticationToken@9055c2bc: Principal: anonymousUser; Credentials: [PROTECTED]; Authenticated: true; Details: org.springframework.security.web.authentication.WebAuthenticationDetails@b364: RemoteIpAddress: 0:0:0:0:0:0:0:1; SessionId: null; Granted Authorities: ROLE_ANONYMOUS
DEBUG o.s.s.access.vote.AffirmativeBased - Voter: org.springframework.security.access.vote.RoleVoter@1913751, returned: 0
DEBUG o.s.s.access.vote.AffirmativeBased - Voter: org.springframework.security.access.vote.AuthenticatedVoter@c6b80e, returned: 1
DEBUG o.s.s.w.a.i.FilterSecurityInterceptor - Authorization successful
DEBUG o.s.s.w.a.i.FilterSecurityInterceptor - RunAsManager did not change Authentication object
DEBUG o.s.security.web.FilterChainProxy - /greeting reached end of additional filter chain; proceeding with original chain
DEBUG o.s.web.servlet.DispatcherServlet - DispatcherServlet with name 'spring-tutorial' determining Last-Modified value for [/SpringMVC/greeting]
DEBUG o.s.w.s.m.a.DefaultAnnotationHandlerMapping - Mapping [/greeting] to handler 'com.example.controller.GreetingController@1eb1db2'
DEBUG o.s.web.servlet.DispatcherServlet - Last-Modified value for [/SpringMVC/greeting] is: -1
DEBUG o.s.web.servlet.DispatcherServlet - DispatcherServlet with name 'spring-tutorial' processing GET request for [/SpringMVC/greeting]
DEBUG o.s.w.b.a.s.HandlerMethodInvoker - Invoking request handler method: public com.example.form.Greeting com.example.controller.GreetingController.greeting()
INFO  c.s.controller.GreetingController - Entered greeting()
DEBUG o.s.w.s.m.a.AnnotationMethodHandlerExceptionResolver - Resolving exception from handler [com.example.controller.GreetingController@1eb1db2]: org.springframework.web.HttpMediaTypeNotAcceptableException: Could not find acceptable representation
DEBUG o.s.w.s.m.a.ResponseStatusExceptionResolver - Resolving exception from handler [com.example.controller.GreetingController@1eb1db2]: org.springframework.web.HttpMediaTypeNotAcceptableException: Could not find acceptable representation
DEBUG o.s.w.s.m.s.DefaultHandlerExceptionResolver - Resolving exception from handler [com.example.controller.GreetingController@1eb1db2]: org.springframework.web.HttpMediaTypeNotAcceptableException: Could not find acceptable representation
DEBUG o.s.s.w.c.HttpSessionSecurityContextRepository - SecurityContext contents are anonymous - context will not be stored in HttpSession. 
DEBUG o.s.web.servlet.DispatcherServlet - Null ModelAndView returned to DispatcherServlet with name 'spring-tutorial': assuming HandlerAdapter completed request handling
DEBUG o.s.web.servlet.DispatcherServlet - Successfully completed request
DEBUG o.s.s.w.a.ExceptionTranslationFilter - Chain processed normally
DEBUG o.s.s.w.c.SecurityContextPersistenceFilter - SecurityContextHolder now cleared, as request processing completed

我在尝试解决同一问题时刚刚遇到了这个问题。查看 pfac 的答案后,我发现只需将 jackson-dataformat-xml 库添加到我的项目中就可以解决我的问题。

如果使用maven:

<dependency>
    <groupId>com.fasterxml.jackson.dataformat</groupId>
    <artifactId>jackson-dataformat-xml</artifactId>
    <version>2.3.2</version>
</dependency>
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

Spring XML 406错误 的相关文章

  • JUnit 使用 Mockito 测试异步方法

    我已经使用 Spring Framework 版本 5 0 5 RELEASE 在 Java 1 8 类中实现了异步方法 public class ClassToBeTested Autowired private MyComponent
  • 如何在Spring的applicationContext.xml中指定默认范围来请求范围?

    我想让所有 bean 请求默认作用域 但是 Spring 文档说默认作用域是 Singleton 第 3 4 1 和 3 4 2 节http static springsource org spring docs 2 5 x referen
  • 如何在 OpenAPI 3.0 中定义字节数组

    我正在将 API 从 Swagger 2 0 迁移到 OpenAPI 3 0 在 DTO 中 我有一个指定为字节数组的字段 Swagger 对 DTO 的定义 Job type object properties body type str
  • 如何正确配置Tomcat SSLHostConfig?

    我正在按照本教程在 tomcat 中启用 ssl https medium com raupach how to install lets encrypt with tomcat 3db8a469e3d2 https medium com
  • firestore快照监听器生命周期和定价之间有什么关系?

    在我的活动中 我有一个字符串列表 这些字符串表示我想要附加快照侦听器的 Firestore 文档 我使用 Acivity ModelView 存储库结构 在活动的 onCreate 中 我向 ViewModelProvider 询问适当的
  • Java:检查给定日期是否在当前月份内

    我需要检查给定的日期是否在当前月份 我编写了以下代码 但 IDE 提醒我getMonth https docs oracle com javase 7 docs api java util Date html getMonth and ge
  • 无法从后台服务通过 WiFi 访问互联网

    我将直接介绍我发现的一些事实 数据 如果您遇到 解决了类似的问题 请帮助我 我每 5 分钟向服务器发送一次数据 除非用户在服务器的帮助下手动将其关闭 wakeful broadcast receiver通过一个intent service
  • 业务代表与服务定位器

    Business Delegate 和 Service Locator 之间有什么区别 两者都负责封装查找和创建机制 如果 Business Delegate 使用 Service Locator 来隐藏查找和创建机制 那么 Busines
  • Vertx HttpClient getNow 不工作

    我的 vertx HttpClient 有问题 下面的代码显示使用 vertx 和纯 java 测试 GET Vertx vertx Vertx vertx HttpClientOptions options new HttpClientO
  • java JFileChooser 文件大小过滤器

    我知道我可以按文件类型进行过滤 但是可以按文件大小进行过滤吗 例如 JFileChooser 仅显示 3 MB 以内的图片 简短的回答应该是 你尝试过什么 长答案是肯定的 JFileChooser fc new JFileChooser f
  • Java String.format 向整数添加空格

    我有一小段代码 我不明白输出 此输出向我的字符串格式文本添加空格 我做错了什么吗 public class HelloWorld public static void main String args int a1 540 int a2 4
  • 如何在将数据发送到 Firebase 数据库之前对其进行加密?

    我正在使用 Firebase 实时数据库制作聊天应用程序 我知道 Firebase 非常安全 只要您的规则正确 但我自己可以阅读使用我的应用程序的人的所有聊天记录 我想阻止这种情况 为此我需要一种解密和加密方法 我尝试使用凯撒解密 但失败了
  • 用 Java 创建迷宫求解算法

    我被分配了用 Java 创建迷宫求解器的任务 这是任务 Write an application that finds a path through a maze The maze should be read from a file A
  • java中的比较器链

    正在阅读Oracle 关于接口的 Java 教程 https docs oracle com javase tutorial java IandI createinterface html其中给出了一个例子Card 打牌 我试图理解接口中的
  • 使用 Maven 3 时 Cobertura 代码覆盖率为 0%

    读完这篇文章后 将 Cobertura 与 Maven 3 0 2 一起使用的正确方法是什么 https stackoverflow com questions 6931360 what is the proper way to use c
  • Scala repl 抛出错误

    当我打字时scala在终端上启动 repl 它会抛出此错误 scala gt init error error while loading AnnotatedElement class file usr lib jvm java 8 ora
  • Android同步onSensorChanged?

    这是我的问题的后续 Android线程可运行性能 https stackoverflow com questions 36395440 android thread runnable performance 我在理解应用程序的同步方法时遇到
  • Java SE + Spring Data + Hibernate

    我正在尝试使用 Spring Data Hibernate 启动 Java SE 应用程序 并且到目前为止已经完成了以下操作 配置文件 Configuration PropertySource classpath hibernate pro
  • JDK 7 的快速调试/调试构建

    我正在寻找 JDK 的调试 或者我猜他们称之为快速调试构建 以启用在运行时生成的打印程序集以及查找性能问题时所需的其他诊断 就目前情况而言 我似乎找不到可以直接使用的 现成的 快速调试构建二进制包 有人可以帮我提供下载链接 或者至少提供有关
  • 如何使用 Spring AOP 建议静态方法?

    在执行类的静态方法之前和之后需要完成一些日志记录 我尝试使用 Spring AOP 来实现这一点 但它不起作用 而对于正常方法来说它起作用 请帮助我理解如何实现这一点 如果可以使用注释来完成 那就太好了 也许您应该在使用 Spring AO

随机推荐

  • 如何使两个Viewbox中的文本大小相同?

    在 WPF 中 我使用 Viewbox 使一些文本自动调整大小 问题是文本最终会使用不同的字体 因为较短的文本会显示得较大
  • 在 UITableViewCell 中编辑 TextLabel [重复]

    这个问题在这里已经有答案了 可能的重复 如何在 UITableView 中进行就地编辑 https stackoverflow com questions 2803626 how to do edit in place in a uitab
  • 使用纯 JavaScript 创建、追加和提交表单

    我正在尝试实现 onclick 函数 该函数使用 POST 方法发送参数 我需要这个函数来重新加载页面 这使得我使用 AJAX 之外的其他方式 我有一个函数可以做到这一点 但这个函数使用 jQuery 现在我需要将其 移植 到纯 JavaS
  • 初始化函数破坏单元测试

    在我想要测试的包中 我有一个 init 函数 它加载配置文件 其中包含我想用来运行应用程序的一些内容 但是 我不想在运行单元测试时触发此初始化函数 有没有办法在单元测试期间跳过或阻止调用此 init 函数 一些片段来说明这个问题 func
  • 使用 SKAction colorizeWithColor 使 SKSpriteNode 闪烁白色

    好的 我有一个精灵 当被某物击中时应该会闪烁白色 我正在使用这个 SKAction changeColorAction SKAction colorizeWithColor SKColor whiteColor colorBlendFact
  • 使用 array_diff 时保留重复项

    我使用 array diff 从 array1 中取出在 array2 中找到的值 问题是它删除了 array1 中的所有出现项 正如 PHP 文档所指出的那样 我希望它一次只取出一个 array1 array array1 a array
  • 使用 haskell 构建动态库并从 C++ 中使用它

    我想构建一个包含 haskell 函数的动态库 我在 Linux 上工作 想从 C 代码中调用这个动态库 我用的例子是http wiki python org moin PythonVsHaskell http wiki python or
  • Java中如何检查抛出的异常类型?

    如果一个操作捕获了多个异常 如何确定捕获了哪种类型的异常 这个例子应该更有意义 try int x doSomething catch NotAnInt ParseError e if thrown error is NotAnInt li
  • 为什么 Google Analytics 使用 __utm.gif?

    只是想了解他们为什么不使用 REST API 在REST中 客户端向服务器发起资源请求 服务器处理这些请求并返回适当的响应 The utm gif不参与服务器到客户端的数据传输 而是参与向另一个方向移动数据 当然 REST 具有供客户端与服
  • 是否可以将公共IP分配给AWS中的Lambda函数?

    我们需要访问企业后端中的 API 该 API 仅接受来自授权 IP 地址的调用 此时 我们的移动客户端正在调用 AWS Lambda 函数 该函数执行一些转换 然后调用分配了授权公共 IP 地址的 EC2 实例上的另一个服务 第二个服务执行
  • SQLite数据适配器不显示数据

    我试图在 Windows 窗体应用程序中填充数据网格视图 但当我执行选择查询时 数据库没有返回任何内容 我已经在本网站上查看了有关此主题的其他问题 但找不到任何可以解决我的问题的内容 数据视图表的名称是qbcMemDataView 数据源是
  • 如何使用 Java 中的 Scanner 将输入值存储在数组中

    您好 我是 Java 新手 我正在尝试 Scanner 类 我试图解决一个小问题 我想输入两个输入 例如 4 5 6 和 8 9 0 我想将 4 5 6 存储在一个数组中 将 8 9 0 存储在另一个数组中 然后打印这些数组 但我无法这样做
  • “:=”有什么作用?

    我见过 在多个代码示例中使用 但从未附带说明 如果不知道它的正确名称 就不可能通过谷歌搜索它的用途 它有什么作用 http en wikipedia org wiki Equals sign In computer programming
  • Gitolite 权限仅限于一个分支

    我有点困惑gitolite https gitolite com gitolite 权限 仅允许 1 个分支并拒绝特定用户或组的所有其他分支的最佳方法是什么 See 部分复制 分支的选择性读取控制 https gitolite com gi
  • 从列表转换为数字

    我正在从列表形式强制转换为数字形式 如果有用的话 列表最初是从一个因素中绘制的 并且是 1x33 行 我的列表定义为 tmpseqsf 1 其中规定 TradeValue 1 72914431 2 25325 3 20139 4 因此 根据
  • 与基于类的方法相比,使用 Hooks 设置状态后重新渲染有什么区别?

    类组件 在 React 类组件中 我们被告知setState always导致重新渲染 无论状态是否实际更改为新值 实际上 一个组件will当状态更新到时重新渲染same以前的价值 文档 setState API 参考 https reac
  • ViewPager内部的TextView拦截触摸事件

    我有一个ViewPager包含两个片段 在任何片段中我都可以触摸一个地方 滑动即可切换到另一个地方fragment 其中一个片段包含一个列表 列表中的项目包含一项TextView和一个ImageView 问题是 如果您是从点击开始拖动的Im
  • 未定义的索引:php 脚本中的错误

    在 php 页面中我有以下代码 if REQUEST c I get error on this line itself Why pidis int REQUEST c 我不断收到未定义索引错误 在谷歌搜索上 我设法理解 如果我们尝试访问的
  • Visual Studio 设置以在运行时删除对 dll 文件的依赖

    到处都有人建议当 Visual Studio 在运行时找不到 DLL 时如何让 Visual Studio 知道 但我想知道在哪里删除该选项 以便 Visual Studio 在运行时不会尝试查找 dll 文件 我昨天也有类似的问题 但没有
  • Spring XML 406错误

    我正在尝试使用 Java 制作 RESTful 服务 使用多个教程和许多 StackOverflow 条目 不幸的是 我似乎无法让我的代码工作 当我尝试到达端点时 我一直收到 Http 406 任何帮助表示赞赏 GreetingContro