【问题标题】:Spring Boot fails to return JSON for a object but not for list of objectsSpring Boot 无法为对象返回 JSON,但不能为对象列表返回 JSON
【发布时间】:2019-07-12 01:42:32
【问题描述】:

我正在开发我的第一个 Spring Boot 应用程序,但遇到了一个奇怪的问题。配置很基础:

    <?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>

    <groupId>com.pawsec</groupId>
    <artifactId>kitchen</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <packaging>jar</packaging>

    <name>kitchen</name>
    <description>The Kitchen restaurant system</description>

    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.1.0.RELEASE</version>
        <relativePath/> <!-- lookup parent from repository -->
    </parent>

    <properties>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
        <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
        <java.version>1.8</java.version>
    </properties>

    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-aop</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-jdbc</artifactId>
        </dependency> 
        <dependency>
            <groupId>mysql</groupId>
            <artifactId>mysql-connector-java</artifactId>
            <scope>runtime</scope>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>
        <dependency>
            <groupId>com.pawsec</groupId>
            <artifactId>common</artifactId>
            <version>1.0</version>
        </dependency>
    </dependencies>

    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
                <configuration>
                    <executable>true</executable>
                </configuration>
            </plugin>
        </plugins>
    </build>


</project>

我们在调用这两个服务的页面上有一些 Javascript 代码。当控制器在第一个方法中返回一个 Guy 对象时,我们得到一个空响应:

    {data: "", status: 200, statusText: "", headers: {…}, config: {…}, …}
config: {adapter: ƒ, transformRequest: {…}, transformResponse: {…}, timeout: 0, xsrfCookieName: "XSRF-TOKEN", …}
data: ""
headers: {}
request: XMLHttpRequest {onreadystatechange: ƒ, readyState: 4, timeout: 0, withCredentials: false, upload: XMLHttpRequestUpload, …}
status: 200
statusText: ""
: Object

然而,当我们从第二种方法返回一个 Guy 对象列表时,我们得到了完整的 Json 结构

back:
{data: Array(3), status: 200, statusText: "", headers: {…}, config: {…}, …}
config: {adapter: ƒ, transformRequest: {…}, transformResponse: {…}, timeout: 0, xsrfCookieName: "XSRF-TOKEN", …}
data: Array(3)
0: {guyId: 1, name: "Walter Sobchak", age: 45}
1: {guyId: 2, name: "Jeffrey Lebowski", age: 42}
2: {guyId: 3, name: "Theodore Donald Kerabatsos", age: 39}
length: 3
: Array(0)
headers: {content-type: "application/json;charset=UTF-8", cache-control: "private", expires: "Thu, 01 Jan 1970 00:00:00 GMT"}
request: XMLHttpRequest {onreadystatechange: ƒ, readyState: 4, timeout: 0, withCredentials: false, upload: XMLHttpRequestUpload, …}
status: 200
statusText: ""
: Object

控制器如下所示:

package com.pawsec.kitchen.controller;

import java.util.ArrayList;
import java.util.List;

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.RestController;

import com.pawsec.kitchen.model.Guy;

@RestController
public class GuyController {

    @RequestMapping(value="/get/guy/{guyId}", method=RequestMethod.GET,
            headers={"Accept=application/json"})
    public Guy getGuy(@PathVariable("guyId") int guyId) {
        Guy someGuy = new Guy(guyId, "Walter Sobchak", 45);
        return someGuy;
    }

    @RequestMapping(value="/get/guys", method=RequestMethod.GET,
            headers={"Accept=application/json"})
    public List<Guy> getGuys() {
        Guy walter = new Guy(1, "Walter Sobchak", 45);
        Guy theDude = new Guy(2, "Jeffrey Lebowski", 42);
        Guy donny = new Guy(3, "Theodore Donald Kerabatsos", 39);
        List<Guy> guys = new ArrayList<Guy>();
        guys.add(walter);
        guys.add(theDude);
        guys.add(donny);
        return guys;
    }

}

奇怪的是,如果我从浏览器调用这两个服务,我会得到两个调用的正确 Json 结构。

当我运行 mvn 依赖项:树时,基本引导项目附带的预期 Jackson 依赖项就在那里。

这是 JavaScript 代码的样子:

return dispatch => {
        dispatch(fetchMenuStart());
        const url = 'https://boot.ourcompany.com:8443/get/guy/1'; 
        const headers = {
            headers: {
                'Content-Type': 'application/json'
            }
        }
        axios.get(url, headers)
            .then(res => {
                console.log(res); 
                dispatch(fetchMenuSuccess(res.data.categories, res.data.restaurant));
            })
            .catch(error => {   
                console.log("error", error);
                const errorMsg = 'There was an error fetching the menu';
                dispatch(fetchMenuFail(errorMsg)); 
            });
    };

任何人都可以提出可能导致此问题的原因或测试步骤以找出问题吗?

新的javascript示例代码:

const doesNotWork = 'https://boot.exmpledomain.com:8443/get/guy/1'; 
const doesWork = 'https://boot.exmpledomain.com:8443/get/guys'; 
const headers = {
    headers: {
    'Content-Type': 'application/json;charset=UTF-8'
    }
}
axios.get(doesNotWork, headers)
    .then(res => {
        console.log(res); 
    })
    .catch(error => {   
        console.log("error", error);
        const errorMsg = 'There was an error fetching the menu';
    });

【问题讨论】:

  • 你在ajax调用中是否将content-type设置为application/json?
  • 如果您从浏览器得到正确的响应,但从 js 代码中得到不正确的响应,则显然 js 代码存在问题。能否请您添加您正在使用的js代码sn-p?
  • ..也许向我们展示“一些 Javascript 代码”。
  • @MatsAndersson,因为您在通过浏览器调用它时会得到正确的响应。问题出在您的前端代码中。你能添加完整的前端代码吗?另外,也请添加其他请求的代码
  • @MatsAndersson - 如果它在浏览器中工作,那么您的问题不在于后端。 JSON 上线的那一刻,没有类型 Guy,没有“自定义 bean”等等。 json 是纯文本,你的 JS 前端应该知道如何使用它。您可以为请求和响应添加拦截器并将输出粘贴到两者上吗? github.com/axios/axios#interceptors

标签: javascript java spring-boot controller jackson


【解决方案1】:

我终于通过禁用 CORS 解决了这个问题,使用以下类:

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Profile;
import org.springframework.web.servlet.config.annotation.CorsRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;

@Profile("devel")
@Configuration
public class WebConfig {

    @Bean
    public WebMvcConfigurer corsConfigurer() {
        return new WebMvcConfigurer() {
            @Override
            public void addCorsMappings(CorsRegistry registry) {
                registry.addMapping("/**");
            }
        };
    }

}

我还添加了 @Profile 注释以仅在开发时禁用 CORS。

顺便说一下,问题的原因似乎在以下内容中进行了解释:

https://chromium.googlesource.com/chromium/src/+/master/services/network/cross_origin_read_blocking_explainer.md#Protecting-JSON

当返回一个对象时,它被解释为一个非空的 JSON 对象(例如{"key": "value"})。返回列表时,相同的文本会被括在方括号中,并且它通过了保护

【讨论】:

  • 感谢您的意见,本杰明·瓦莱罗
【解决方案2】:

好的,非常感谢你们的努力。事实证明,@mpromonet 建议的解决方案(在控制器上添加 CrossOrigin 注释)解决了这个问题。如果这是一个跨域问题,我仍然很想知道为什么返回 List 的方法有效而返回 Guy 的方法无效。这似乎不合逻辑,并且使问题更难弄清楚。

【讨论】:

    【解决方案3】:

    您能否尝试更改标头以在 javascript 中接受

    return dispatch => {
            dispatch(fetchMenuStart());
            const url = 'https://boot.ourcompany.com:8443/get/guy/1'; 
            const headers = {
                headers: {
                    'Content-Type': 'application/json',
                    'Accept': 'application/json'
                }
            }
            axios.get(url, headers)
                .then(res => {
                    console.log(res); 
                    dispatch(fetchMenuSuccess(res.data.categories, res.data.restaurant));
                })
                .catch(error => {   
                    console.log("error", error);
                    const errorMsg = 'There was an error fetching the menu';
                    dispatch(fetchMenuFail(errorMsg)); 
                });
        };
    

    【讨论】:

    • 是的...他缺少客户端中的“Accept”标头以及服务器上的返回类型(产生=MediaType.APPLICATION_JSON_VALUE)
    【解决方案4】:

    由于您的 javascript 与 spring-boot 服务位于不同的域中,因此您需要配置 CORS

    这可以像这样全局添加@CrossOrigin

    @RestController
    @CrossOrigin
    public class GuyController {
    

    【讨论】:

    • 不要认为这是问题所在,因为返回自定义对象列表的请求可以正常工作。
    • @MadhuBhat:我用 spring-boot & axiom 进行了测试,它可以使用 1 个对象和一个从 spring-boot 为 JS 服务的对象列表,没有任何问题。使用不同的网络服务器,我重现了一个 CORB 异常,其中 1 个对象不存在于对象列表中,禁用 CORS 它在两种情况下都有效。
    • 对不起大家,我病了几天了。我可以补充一点,我们有多个基于 Spring 的系统正在运行。他们有数百个控制器方法返回我们自己的类和列表以及其他类。我们使用相同的 javascript 方式调用这些方法,我们以前从未遇到过这个问题。然而,这是我们第一次使用带有内置 Json 支持的 Spring Boot 应用程序。这也是我们第一次从未与服务器端集成的 React 客户端进行调用。我们的其他系统使用 JSP 作为 GUI。
    【解决方案5】:

    如果使用spring,应该使用ResponseEntity,而不是直接返回对象:

    import org.springframework.http.HttpStatus;
    import org.springframework.http.MediaType;
    import org.springframework.http.ResponseEntity;
    

    这就是我编写控制器的方式:

    @RestController
    @RequestMapping(USERS)
    public class UserController {
    
      @Autowired
      private UserService userService;
    
      @Autowired
      private RoleService roleService;
    
      @Autowired
      private LdapUserDetailsManager userDetailsService;
    
      @GetMapping(produces = MediaType.APPLICATION_JSON_VALUE)
      public ResponseEntity<?> list(PagedResourcesAssembler<User> pageAssembler, @PageableDefault(size = 20) Pageable pageable, UserDTO condition) {
        Page<User> page = userService.findAll(pageable, condition);
        PagedResources<?> resources = pageAssembler.toResource(page, new UserResourceAssembler());
        return ResponseEntity.ok(resources);
      }
    
      @GetMapping(value = CoreHttpPathStore.PARAM_ID, produces= MediaType.APPLICATION_JSON_VALUE)
      public ResponseEntity<UserResource> get(@PathVariable("id") Long id) {
        User user = userService.get(id);
        UserResource resource = new UserResourceAssembler().toResource(user);
        return ResponseEntity.ok(resource);
      }
    
      private void preProcessEntity(@RequestBody UserDTO entity) {
        if (null != entity.getPassword()) {
          userDetailsService.changePassword(entity.getOldPassword(), entity.getPassword());
        }
      }
    
      @PostMapping
      @ResponseStatus(HttpStatus.CREATED)
      Long create(@RequestBody User user) {
        userService.insert(user);
        return user.getId();
      }
    
      @PutMapping(CoreHttpPathStore.PARAM_ID)
      @ResponseStatus(HttpStatus.NO_CONTENT)
      void modify(@PathVariable("id") Long id, @RequestBody UserDTO user) {
        user.setId(id);
        preProcessEntity(user);
        userService.updateIgnore(user);
      }
    
      @DeleteMapping(CoreHttpPathStore.PARAM_ID)
      @ResponseStatus(HttpStatus.NO_CONTENT)
      void delete(@PathVariable("id") Long id) {
        userService.delete(id);
      }
    
      @DeleteMapping
      @ResponseStatus(HttpStatus.NO_CONTENT)
      void bulkDelete(@RequestBody Long[] ids) {
        userService.delete(ids);
      }
    }
    

    【讨论】:

    • 使用ResponseEntity 完全没有必要。
    【解决方案6】:

    你必须在你的方法之前添加@ResponseBody注解。

    @ResponseBody
    public Guy ....
    

    【讨论】:

    • @RestController 不需要@ResponseBody - 请参阅this answer
    猜你喜欢
    • 1970-01-01
    • 2017-12-04
    • 2023-04-08
    • 2011-06-28
    • 2021-03-09
    • 2021-06-30
    • 1970-01-01
    • 1970-01-01
    • 2020-02-11
    相关资源
    最近更新 更多