相关疑难解决方法(0)

如何使用@ResponseBody从spring Controller返回JSON数据

Spring版本4.2.0,Hibernate 4.1.4 这是我的Controller功能:

@RequestMapping(value = "/mobile/getcomp", method = RequestMethod.GET)
@ResponseBody
public List<Company>  listforCompanies() {      
    List<Company> listOfCompanies= new ArrayList<Company>();        
    listOfCompanies = companyManager.getAllCompanies();
    return listOfCompanies;
}
Run Code Online (Sandbox Code Playgroud)

杰克逊JSON映射器依赖Pom.xml:

    <!-- Jackson JSON Mapper -->
    <dependency>
        <groupId>org.codehaus.jackson</groupId>
        <artifactId>jackson-mapper-asl</artifactId>
        <version>${jackson.version}</version>
    </dependency>
Run Code Online (Sandbox Code Playgroud)

获取列表ArrayList,但返回时会显示以下错误:

SEVERE: Servlet.service() for servlet [dispatcherServlet] in context with path [/IrApp] threw exception [Request processing failed; nested exception is java.lang.IllegalArgumentException: No converter found for return value of type: class java.util.ArrayList] with root cause
    java.lang.IllegalArgumentException: No converter found for return …
Run Code Online (Sandbox Code Playgroud)

java spring json spring-mvc spring-4

48
推荐指数
5
解决办法
9万
查看次数

Spring RestController 将错误返回为 html 而不是 json

我有一个 spring boot webapp,我在其中定义了 REST API。我正在使用 Spring Security 来保证 REST API 安全。

我用 RestController 注释了我的控制器类。我最近将 spring boot、mvc 和 security 更新到最新版本。

我现在看到在我的负面情况下,在更新之前它返回 json 错误响应,但现在更新后,它返回 html 错误响应。

在更新之前,它给出了以下错误响应-

{
  "timestamp": "2018-05-21T18:22:37.105+0000",
  "status": 500,
  "error": "Internal Server Error",
  "message": "Error message.",
  "path": "<API path>"
}
Run Code Online (Sandbox Code Playgroud)

更新后,它给出了以下响应。

<!DOCTYPE html>
<html>
    <head>
        <title>Apache Tomcat/8.0.51 - Error report</title>
        <style type="text/css">H1 {font-family:Tahoma,Arial,sansserif;color:white;background-color:#525D76;font-size:22px;} H2 {font-family:Tahoma,Arial,sans-serif;color:white;background-color:#525D76;font-size:16px;} H3 {font-family:Tahoma,Arial,sans-serif;color:white;background-color:#525D76;font-size:14px;} BODY {font-family:Tahoma,Arial,sans-serif;color:black;background-color:white;} B {font-family:Tahoma,Arial,sans-serif;color:white;background-color:#525D76;} P {font-family:Tahoma,Arial,sans-serif;background:white;color:black;font-size:12px;}A {color : black;}A.name {color : black;}.line {height: 1px; background-color: #525D76; border: none;}</style>
    </head>
    <body>
        <h1>HTTP …
Run Code Online (Sandbox Code Playgroud)

java spring spring-mvc spring-security spring-boot

9
推荐指数
1
解决办法
3734
查看次数