相关疑难解决方法(0)

在Spring Boot中将JSON对象作为响应返回

我在Spring启动时有一个示例Rest Controller:

@RestController
@RequestMapping("/api")
class MyRestController
{

    @GetMapping(path = "/hello")
    public JSONObject sayHello()
    {
        return new JSONObject("{'aa':'bb'}");
    }


}
Run Code Online (Sandbox Code Playgroud)

我正在使用json库:org.json

当我点击api /你好时,我得到一个异常说:

servlet [dispatcherServlet]的Servlet.service()与path []的上下文引发了异常[请求处理失败; 嵌套异常是java.lang.IllegalArgumentException:没有为根本原因找到类型为:class org.json.JSONObject的返回值的转换器

java.lang.IllegalArgumentException:找不到类型为:class org.json.JSONObject的返回值的转换器

问题是什么 有人可以解释究竟发生了什么.我是SpringBoot的新手.

提前致谢 :)

java json spring-boot

54
推荐指数
7
解决办法
20万
查看次数

Spring Boot应用程序:找不到类型返回值的转换器

我根据编写一个简单的REST API 这个弹簧引导教程.在我的本地开发机器(Ubuntu 15.04和Windows 8.1)上,一切都像魅力一样.

我有一个旧的32位Ubuntu 12.04 LTS服务器,我想要部署我的REST服务.

启动日志没问题,但是一旦我向/ user/{id}端点发送GET请求,我就会收到以下错误:

java.lang.IllegalArgumentException: No converter found for return value of type: class ch.gmazlami.gifty.models.user.User
Run Code Online (Sandbox Code Playgroud)

然后在堆栈跟踪中:

java.lang.IllegalArgumentException: No converter found for return value of type: class java.util.LinkedHashMap
Run Code Online (Sandbox Code Playgroud)

整个堆栈跟踪都发布在这里.

我查看了一些引用此错误的答案,但这些似乎并不适用于我的问题,因为我使用的是Spring-Boot,没有任何xml配置.

受影响的控制器是:

    @RequestMapping(value = "/user/{id}", method = RequestMethod.GET)
public ResponseEntity<User> getUser(@PathVariable Long id){
    try{
        return new ResponseEntity<User>(userService.getUserById(id), HttpStatus.OK);
    }catch(NoSuchUserException e){
        return new ResponseEntity<>(HttpStatus.NOT_FOUND);
    }
}
Run Code Online (Sandbox Code Playgroud)

任何帮助将不胜感激.这是非常奇怪的,因为完全相同的东西在其他机器上工作.

提前致谢!

rest ubuntu converters spring-boot

16
推荐指数
4
解决办法
4万
查看次数

Spring Boot Rest - 如何配置404 - 找不到资源

我得到了一个有效的弹簧靴休息服务.当路径错误时,它不会返回任何内容.完全没有回应.同时它也不会抛出错误.理想情况下,我期望404找不到错误.

我有一个GlobalErrorHandler

@ControllerAdvice
public class GlobalErrorHandler extends ResponseEntityExceptionHandler {

}
Run Code Online (Sandbox Code Playgroud)

ResponseEntityExceptionHandler中有此方法

protected ResponseEntity<Object> handleNoHandlerFoundException(NoHandlerFoundException ex, HttpHeaders headers,
                                                     HttpStatus status, WebRequest request) {

    return handleExceptionInternal(ex, null, headers, status, request);
}
Run Code Online (Sandbox Code Playgroud)

我已error.whitelabel.enabled=false在我的属性中标记

我还必须为此服务做些什么才能将404未找到的响应返回给客户端

我提到了很多线索,并没有看到任何人面临这种​​麻烦.

这是我的主要应用程序类

 @EnableAutoConfiguration // Sprint Boot Auto Configuration
@ComponentScan(basePackages = "com.xxxx")
@EnableJpaRepositories("com.xxxxxxxx") // To segregate MongoDB
                                                        // and JPA repositories.
                                                        // Otherwise not needed.
@EnableSwagger // auto generation of API docs
@SpringBootApplication
@EnableAspectJAutoProxy
@EnableConfigurationProperties

public class Application extends SpringBootServletInitializer {

    private static Class<Application> appClass = Application.class;

    @Override …
Run Code Online (Sandbox Code Playgroud)

java rest spring spring-mvc spring-boot

11
推荐指数
1
解决办法
2万
查看次数

HttpMessageNotWritableException:未找到类型返回值的转换器:Spring MVC 中的类 java.util.HashMap

我试图在控制器的方法中使用 HashMap 返回 json

//list all users
    @RequestMapping(value = "/user",method = RequestMethod.GET)
    public ResponseEntity<Map<String,Object>> handleRequest() throws Exception{
        List<User> users = userService.list();          
        Map<String, Object> message = new HashMap<String, Object>();

        message.put("severity", "info");
        message.put("user", users);
        message.put("summary", "Not successfully.");
        message.put("code", 200);

        Map<String, Object> json = new HashMap<String, Object>();
        json.put("success", true);
        json.put("message", message);

        return new ResponseEntity<Map<String,Object>>(message,HttpStatus.CREATED);
    }
Run Code Online (Sandbox Code Playgroud)

当我在 PostMan 中点击此方法时,出现此错误

org.springframework.http.converter.HttpMessageNotWritableException: No converter found for return value of type: class java.util.HashMap
org.springframework.web.servlet.mvc.method.annotation.AbstractMessageConverterMethodProcessor.writeWithMessageConverters(AbstractMessageConverterMethodProcessor.java:226)
org.springframework.web.servlet.mvc.method.annotation.RequestResponseBodyMethodProcessor.handleReturnValue(RequestResponseBodyMethodProcessor.java:180)
org.springframework.web.method.support.HandlerMethodReturnValueHandlerComposite.handleReturnValue(HandlerMethodReturnValueHandlerComposite.java:82)
org.springframework.web.servlet.mvc.method.annotation.ServletInvocableHandlerMethod.invokeAndHandle(ServletInvocableHandlerMethod.java:119)
org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter.invokeHandlerMethod(RequestMappingHandlerAdapter.java:877)
org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter.handleInternal(RequestMappingHandlerAdapter.java:783)
org.springframework.web.servlet.mvc.method.AbstractHandlerMethodAdapter.handle(AbstractHandlerMethodAdapter.java:87)
org.springframework.web.servlet.DispatcherServlet.doDispatch(DispatcherServlet.java:991)
org.springframework.web.servlet.DispatcherServlet.doService(DispatcherServlet.java:925)
org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:974)
org.springframework.web.servlet.FrameworkServlet.doGet(FrameworkServlet.java:866)
javax.servlet.http.HttpServlet.service(HttpServlet.java:635)
org.springframework.web.servlet.FrameworkServlet.service(FrameworkServlet.java:851)
javax.servlet.http.HttpServlet.service(HttpServlet.java:742) …
Run Code Online (Sandbox Code Playgroud)

java spring spring-mvc

3
推荐指数
2
解决办法
3万
查看次数

Java - Spring返回JSON对象/数组

我有一个基本的Rest Controller,它将json中的模型列表返回给客户端:

@RestController
public class DataControllerREST {

    @Autowired
    private DataService dataService;

    @GetMapping("/data")
    public List<Data> getData() {
        return dataService.list();
    }

}
Run Code Online (Sandbox Code Playgroud)

以这种格式返回数据:

[

    {
        "id": 1,
        "name": "data 1",
        "description": "description 1",
        "active": true,
        "img": "path/to/img"
    },
    // etc ...

]
Run Code Online (Sandbox Code Playgroud)

这非常适合开始,但我想要返回这种格式的数据:

[
    "success": true,
    "count": 12,
    "data": [
        {
            "id": 1,
            "name": "data 1",
            "description": "description 1",
            "active": true,
            "img": "path/to/img"
        },
        {
            "id": 2,
            "name": "data 2",
            "description": "description 2",
            "active": true,
            "img": "path/to/img"
        },
    ]
    // etc ... …
Run Code Online (Sandbox Code Playgroud)

java rest spring json controller

0
推荐指数
1
解决办法
5219
查看次数

标签 统计

java ×4

rest ×3

spring ×3

spring-boot ×3

json ×2

spring-mvc ×2

controller ×1

converters ×1

ubuntu ×1