奇怪的是我的 PUT 操作没有映射到 spring 方法,我有以下类...
@Controller
@RequestMapping(value = { "/**/applicationContext/abcde" })
public abstract class AbstractController {
// Exception handlers
}
该类由另一个抽象类扩展,如下
public abstract class AbstractAdapter extends AbstractController{}
最后,上面的类由下面的类扩展
public class SpringAdapter extends AbstractAdapter implements MyInterface{}
我需要进行一些放置操作,如下所示:-
@Override
@ResponseBody
@RequestMapping(method = { RequestMethod.PUT }, consumes = "application/x-www-form-urlencoded", produces = "application/xml", params = {
"param1=some_value", param2 })
public MyObject update(
@RequestParam(value = param2, required = true) @ContentType(value = Encoding.XML) MyObejct myObject, HttpServletRequest request) throws Exception{ }
但是当我调用 put 方法时,请求未映射到我的更新方法。 有人可以帮忙吗?
最佳答案
你的子类没有被标记为@Controller,因此Spring(当它扫描 Controller 时)找不到它。
请参阅this answer
基本上,默认情况下注释不会被继承。 您可以在注释的声明中使用注释将其标记为继承as is shown here 通过使用:
public @interface Inherited
但是如果你看the controller source ,spring使用的Controller实现中没有使用这个关键字。
@Target({ElementType.TYPE})
@Retention(RetentionPolicy.RUNTIME)
@Documented
@Component
public @interface Controller {
所以我假设 @Controller 在您的情况下不会被继承。我首先会用一个简单的例子来尝试继承(类 A 扩展了类 B ,只有一个简单的未修饰的方法)来验证这一点。
关于java - Spring PUT 未映射到方法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27922418/