【问题标题】:spring rest not returning json object - getting 404弹簧休息不返回 json 对象 - 得到 404
【发布时间】:2026-02-19 20:55:02
【问题描述】:

我已经找了几天了,但没有运气.. 我得到一个 http 404 并且找不到我缺少的东西,为什么它没有拿起我的映射。我做错了吗?

我正在使用 Spring Rest 并尝试使用 @ResponseBody 注释返回一个 json 对象。请帮忙!我正在使用 jackson-core-2.2.3.jar、jackson-databind-2.2.3.jar 和 jackson-annotations-2.2.3.jar。这是我的代码,我的服务器日志如下。感谢您的帮助!

web.xml

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
    xmlns="http://xmlns.jcp.org/xml/ns/javaee" 
    xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd" 
    id="WebApp_ID" version="3.1">

  <display-name>app</display-name>

  <servlet>
        <servlet-name>dispatcher</servlet-name>
        <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
        <load-on-startup>1</load-on-startup>
    </servlet>

    <servlet-mapping>
        <servlet-name>dispatcher</servlet-name>
        <url-pattern>/rest/**</url-pattern>
    </servlet-mapping>

    <context-param>
        <param-name>contextConfigLocation</param-name>
        <param-value>/WEB-INF/dispatcher-servlet.xml</param-value>
    </context-param>

    <listener>
        <listener-class>
            org.springframework.web.context.ContextLoaderListener
        </listener-class>
    </listener>

  <welcome-file-list>
    <welcome-file>index.jsp</welcome-file>
  </welcome-file-list>
</web-app>

调度程序-servlet.xml

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

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

    <bean class="org.springframework.web.servlet.mvc.annotation.DefaultAnnotationHandlerMapping"/>

</beans>

地址.java

package com.app.domain;

import com.fasterxml.jackson.annotation.JsonIgnoreProperties;


@JsonIgnoreProperties(ignoreUnknown=true)
public class Address {

    private String street;
    private String zip;
    private String city;
    private String state;

    //getters and setters

}

AddressController.java

package com.app.controller;

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.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.app.domain.Address;

@Controller
@RequestMapping(value="/rest")
public class AddressController {

    @RequestMapping(value="address", method=RequestMethod.GET, headers="application/json")
    public @ResponseBody Address addressEntry(@RequestParam(value="street", required=true) String street, 
            @RequestParam(value="zip", required=true) String zip){

        Address addressRequest = new Address();
        addressRequest.setStreet(street);
        addressRequest.setZip(zip);
        return addressRequest;
    }

    @RequestMapping(value="address/{street}/{zip}", method=RequestMethod.GET, headers="application/json")
    public @ResponseBody Address addressEntry2(@PathVariable(value="street") String street, 
            @PathVariable(value="zip") String zip){


        Address addressRequest = new Address();
        addressRequest.setStreet(street);
        addressRequest.setZip(zip);
        return addressRequest;
    }
}

我正在使用 Jboss AS7,当服务器启动时,这里是日志记录

19:09:22,052 INFO  [org.springframework.beans.factory.xml.XmlBeanDefinitionReader] (MSC service thread 1-8) Loading XML bean definitions from ServletContext resource [/WEB-INF/dispatcher-servlet.xml]
19:09:22,430 INFO  [org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor] (MSC service thread 1-8) JSR-330 'javax.inject.Inject' annotation found and supported for autowiring
19:09:22,604 INFO  [org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerMapping] (MSC service thread 1-8) Mapped "{[/rest/address],methods=[GET],params=[],headers=[application/json],consumes=[],produces=[],custom=[]}" onto public com.app.domain.Address com.app.controller.AddressController.addressEntry(java.lang.String,java.lang.String)
19:09:22,607 INFO  [org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerMapping] (MSC service thread 1-8) Mapped "{[/rest/address/{street}/{zip}],methods=[GET],params=[],headers=[application/json],consumes=[],produces=[],custom=[]}" onto public com.app.domain.Address com.app.controller.AddressController.addressEntry2(java.lang.String,java.lang.String)
19:09:22,640 INFO  [org.hibernate.validator.util.Version] (MSC service thread 1-8) Hibernate Validator 4.2.0.Final
19:09:23,102 INFO  [org.springframework.web.servlet.mvc.annotation.DefaultAnnotationHandlerMapping] (MSC service thread 1-8) Mapped URL path [/rest/address] onto handler 'addressController'
19:09:23,103 INFO  [org.springframework.web.servlet.mvc.annotation.DefaultAnnotationHandlerMapping] (MSC service thread 1-8) Mapped URL path [/rest/address.*] onto handler 'addressController'
19:09:23,104 INFO  [org.springframework.web.servlet.mvc.annotation.DefaultAnnotationHandlerMapping] (MSC service thread 1-8) Mapped URL path [/rest/address/] onto handler 'addressController'
19:09:23,105 INFO  [org.springframework.web.servlet.mvc.annotation.DefaultAnnotationHandlerMapping] (MSC service thread 1-8) Mapped URL path [/rest/address/{street}/{zip}] onto handler 'addressController'
19:09:23,106 INFO  [org.springframework.web.servlet.mvc.annotation.DefaultAnnotationHandlerMapping] (MSC service thread 1-8) Mapped URL path [/rest/address/{street}/{zip}.*] onto handler 'addressController'
19:09:23,107 INFO  [org.springframework.web.servlet.mvc.annotation.DefaultAnnotationHandlerMapping] (MSC service thread 1-8) Mapped URL path [/rest/address/{street}/{zip}/] onto handler 'addressController'
19:09:23,124 INFO  [org.springframework.web.context.ContextLoader] (MSC service thread 1-8) Root WebApplicationContext: initialization completed in 1169 ms
19:09:23,173 INFO  [org.apache.catalina.core.ContainerBase.[jboss.web].[default-host].[/app]] (MSC service thread 1-8) Initializing Spring FrameworkServlet 'dispatcher'
19:09:23,173 INFO  [org.springframework.web.servlet.DispatcherServlet] (MSC service thread 1-8) FrameworkServlet 'dispatcher': initialization started
19:09:23,177 INFO  [org.springframework.web.context.support.XmlWebApplicationContext] (MSC service thread 1-8) Refreshing WebApplicationContext for namespace 'dispatcher-servlet': startup date [Thu Jan 16 19:09:23 EST 2014]; parent: Root WebApplicationContext
19:09:23,180 INFO  [org.springframework.beans.factory.xml.XmlBeanDefinitionReader] (MSC service thread 1-8) Loading XML bean definitions from ServletContext resource [/WEB-INF/dispatcher-servlet.xml]
19:09:23,250 INFO  [org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor] (MSC service thread 1-8) JSR-330 'javax.inject.Inject' annotation found and supported for autowiring
19:09:23,290 INFO  [org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerMapping] (MSC service thread 1-8) Mapped "{[/rest/address],methods=[GET],params=[],headers=[application/json],consumes=[],produces=[],custom=[]}" onto public com.app.domain.Address com.app.controller.AddressController.addressEntry(java.lang.String,java.lang.String)
19:09:23,291 INFO  [org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerMapping] (MSC service thread 1-8) Mapped "{[/rest/address/{street}/{zip}],methods=[GET],params=[],headers=[application/json],consumes=[],produces=[],custom=[]}" onto public com.app.domain.Address com.app.controller.AddressController.addressEntry2(java.lang.String,java.lang.String)
19:09:23,471 INFO  [org.springframework.web.servlet.mvc.annotation.DefaultAnnotationHandlerMapping] (MSC service thread 1-8) Mapped URL path [/rest/address] onto handler 'addressController'
19:09:23,472 INFO  [org.springframework.web.servlet.mvc.annotation.DefaultAnnotationHandlerMapping] (MSC service thread 1-8) Mapped URL path [/rest/address.*] onto handler 'addressController'
19:09:23,473 INFO  [org.springframework.web.servlet.mvc.annotation.DefaultAnnotationHandlerMapping] (MSC service thread 1-8) Mapped URL path [/rest/address/] onto handler 'addressController'
19:09:23,473 INFO  [org.springframework.web.servlet.mvc.annotation.DefaultAnnotationHandlerMapping] (MSC service thread 1-8) Mapped URL path [/rest/address/{street}/{zip}] onto handler 'addressController'
19:09:23,474 INFO  [org.springframework.web.servlet.mvc.annotation.DefaultAnnotationHandlerMapping] (MSC service thread 1-8) Mapped URL path [/rest/address/{street}/{zip}.*] onto handler 'addressController'
19:09:23,475 INFO  [org.springframework.web.servlet.mvc.annotation.DefaultAnnotationHandlerMapping] (MSC service thread 1-8) Mapped URL path [/rest/address/{street}/{zip}/] onto handler 'addressController'
19:09:23,507 INFO  [org.springframework.web.servlet.DispatcherServlet] (MSC service thread 1-8) FrameworkServlet 'dispatcher': initialization completed in 333 ms
19:09:23,514 INFO  [org.jboss.web] (MSC service thread 1-8) JBAS018210: Registering web context: /app
19:09:23,515 INFO  [org.jboss.as] (MSC service thread 1-4) JBAS015951: Admin console listening on http://127.0.0.1:9990
19:09:23,516 INFO  [org.jboss.as] (MSC service thread 1-4) JBAS015874: JBoss AS 7.1.1.Final "Brontes" started in 6174ms - Started 245 of 324 services (78 services are passive or on-demand)
19:09:23,868 INFO  [org.jboss.as.server] (DeploymentScanner-threads - 2) JBAS018559: Deployed "appEAR.ear"

【问题讨论】:

    标签: json spring rest annotations


    【解决方案1】:

    也许你应该定义方法应该产生什么。 您可以通过为 RequestMapping 注释设置生产属性来做到这一点。

    示例: @RequestMapping(value="address/{street}/{zip}", method=RequestMethod.GET, 产生 = { MediaType.APPLICATION_JSON_VALUE })

    【讨论】:

    • 我接受了您的输入并添加了 products = { MediaType.APPLICATION_JSON_VALUE },删除了 headers="application/json" 属性,然后我得到了一个 http 406。当我把两个标头都放入并产生时,我仍然获取 404..
    • 如果我从浏览器中显示我的请求标头和响应标头有帮助吗? Content-Type 在任何地方都没有说“application/json”。不知道这是否有影响,如果有,我该如何解决?响应标头 Content-Length 956 Content-Type text/html;charset=utf-8 日期 Fri, 17 Jan 2014 03:55:29 GMT 服务器 Apache-Coyote/1.1 请求标头接受 text/html,application/xhtml+xml,application /xml;q=0.9,/;q=0.8
    • 如您所见,标头字段accept 不包括application/json,并且由于该方法返回JSON,您会得到406。因此,如果您需要json,则需要在其中定义accept application/json接受标头字段,或者如果您可以使用 xml,只需执行此操作 @RequestMapping(value="address/{street}/{zip}", method=RequestMethod.GET,produces = { MediaType.APPLICATION_JSON_VALUE, MediaType.APPLICATION_XML_VALUE })跨度>
    • 当我有@RequestMapping(value="/address", method=RequestMethod.GET, headers={MediaType.APPLICATION_JSON_VALUE},produces={MediaType.APPLICATION_JSON_VALUE})。我没有得到 406,但是 404 .. 13:26:55,520 WARN [org.springframework.web.servlet.PageNotFound] (http-localhost-127.0.0.1-8080-1) 没有找到带有 URI 的 HTTP 请求的映射 [ /app/rest/address/] 在 DispatcherServlet 中,名称为“dispatcher”。我从上面的代码中遗漏了什么?
    • 我认为这是错误的,但我不确定 headers={MediaType.APPLICATION_JSON_VALUE}
    【解决方案2】:

    尝试将 headers 属性值更改为

    "Accept=application/json"
    

    【讨论】:

    • 据我了解,设置 headers={"Accept=application/json"} 意味着客户端的浏览器必须能够接受 application/json。你知道让/强制客户端的浏览器接受 application/json 的方法吗?