【问题标题】:Unable to configure json with spring - Request method 'GET' not supported无法使用 spring 配置 json - 不支持请求方法“GET”
【发布时间】:2014-07-13 16:36:29
【问题描述】:

我无法正确配置 Spring 4.0.4 的 json 支持。一个json请求导致:

org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerMapping.getHandlerInternal(246):  - Looking up handler method for path /equipments/datatable.json
org.springframework.web.servlet.mvc.method.annotation.ExceptionHandlerExceptionResolver.resolveException(134):  - Resolving exception from handler [null]: org.springframework.web.HttpRequestMethodNotSupportedException: Request method 'GET' not supported
org.springframework.web.servlet.mvc.annotation.ResponseStatusExceptionResolver.resolveException(134):  - Resolving exception from handler [null]:             org.springframework.web.HttpRequestMethodNotSupportedException: Request method 'GET' not supported
org.springframework.web.servlet.mvc.support.DefaultHandlerExceptionResolver.resolveException(134):  - Resolving exception from handler [null]:      org.springframework.web.HttpRequestMethodNotSupportedException: Request method 'GET' not supported
org.springframework.web.servlet.PageNotFound.handleHttpRequestMethodNotSupported(198):  - Request method 'GET' not supported

我有一个applicationContext.xml 和一个my-servlet.xml

web.xml

<context-param>
    <param-name>contextConfigLocation</param-name>
    <param-value>                      
        /WEB-INF/conf/applicationContext.xml
        /WEB-INF/conf/my-security.xml          
    </param-value>
</context-param>

<servlet>
    <servlet-name>losap</servlet-name>
    <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
    <init-param>
        <param-name>contextConfigLocation</param-name>
        <param-value>/WEB-INF/conf/my-servlet.xml</param-value>
    </init-param>
    <load-on-startup>1</load-on-startup>
</servlet>

applicationContext.xml

<mvc:annotation-driven />
<task:annotation-driven />
<tx:annotation-driven />

<context:component-scan base-package="com.htg.*">
   <context:exclude-filter type="regex" expression="com\.htg\.myapp\.mvc"/>
</context:component-scan>

<bean id="messageAdapter"
    class="org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter">
    <property name="messageConverters">
        <util:list>
            <bean
                class="org.springframework.http.converter.json.MappingJacksonHttpMessageConverter" />
        </util:list>
    </property>
</bean>

my-servlet.xml

<mvc:annotation-driven/>        
<context:component-scan base-package="com.htg.myspp.mvc" />

pom.xml

<dependency>
        <groupId>org.codehaus.jackson</groupId>
        <artifactId>jackson-mapper-asl</artifactId>
        <version>1.9.13</version>
</dependency>

控制器

@Controller()
@RequestMapping(value = "/equipments")
public class EquipmentController extends BaseController {

    @RequestMapping(value = "/datatable.json", method = RequestMethod.GET, headers = "Accept:*/*", produces = "application/json")
    public @ResponseBody Equipment getEquipmentsDatatable(Model model) {
        Equipment e = new Equipment();
        e.setEquipmentName("Lathe 123");
        e.setEquipmentNumber("12345TTY");
        e.setId(89L);
        return e;
    }
}

jQuery

$.getJSON($('#ctxtrelurl').val() + 'equipments/datatable.json', function(e) {
    console.log(e.equipmentName);
});

Chrome 开发者。工具输出

我感觉这与拥有两个独立的上下文配置有关。文件。任何指针将不胜感激。

【问题讨论】:

  • 您的控制器中还有其他功能支持 PUT 请求吗?
  • 您能否在应用程序启动时验证,日志将包含类似“Mapped”{[/eqipments/datatable.json],methods=[GET],params=[],headers=[接受:*/*],consumes=[],produces=[application/json],custom=[]}" 到公共 EquipmentController.getEquipmentsDatatable"。基本上,这将确保 Spring 正确读取映射
  • @JohnnyAW,是的,我愿意:@RequestMapping(value = "/{equipId}", method = RequestMethod.PUT)
  • @Shailendra,不,我没看到。我看到了所有其他的,但没有看到这个。
  • 这只是意味着它没有被拾取,因此它不会响应您的请求。

标签: ajax json spring spring-mvc jackson


【解决方案1】:

好吧,我的猜测是,由于“.”,spring 没有正确映射 /datatable.json。我在映射这个时遇到了类似的问题:

@RequestMapping(value="/somestuff/{fileName:.+}"

我必须输入 :.+ 以便 spring 可以正确映射 url。

在这种情况下,spring 似乎截断了“.something”。我实际上不知道在没有@PathVariable 的情况下是否是常规映射的情况。但是您可以尝试在不带点的情况下映射您的 URL,并检查它是否有帮助

这就是为什么 spring 尝试调用 {equipId} 函数,它正在等待 PUT 请求。这就是为什么在您看到的响应标头中:allow:PUT

【讨论】:

  • 更改为“/datatable/json”,但是仍然没有映射。
猜你喜欢
  • 2017-10-21
  • 2018-05-26
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2018-04-01
  • 1970-01-01
  • 2018-06-20
  • 1970-01-01
相关资源
最近更新 更多