繁体   English   中英

Java Spring Boot-返回json响应

[英]Java spring boot - returning a json response

我想为新的Java Spring Boot应用程序创建一个自定义json响应。 目前,我只想创建一个虚拟json对象-开始控制输出json响应的结构。

当我这样做时,它想为.puts创建强制转换前缀-我曾经使用过Mongodb-BasicDBObject-但现在我在这里还没有mongo。

 DBObject obj = new BasicDBObject();
 obj.put( "foo", "bar" );

我该怎么办?

-

@RequestMapping(value = "/login", method = RequestMethod.GET)
@CrossOrigin(origins = {"*"})
public ResponseEntity<?> login(
        @RequestParam(value="email", required=false, defaultValue="email") String email,
        @RequestParam(value="password", required=false, defaultValue="password") String password, 
        HttpServletRequest request
        ) throws  Exception {

                Object response = new Object();
                    response.put("information", "test");
                    response.put("id", 3);
                    response.put("name", "course1");

                return new ResponseEntity<>(response, HttpStatus.OK);
        }
**@ResetController**   // add this
public class YourClass{

    @RequestMapping(value = "/login", method = RequestMethod.GET)
    @CrossOrigin(origins = {"*"})
    public ResponseEntity<?> login(){
        Object response = new Object();
                    response.put("information", "test");
                    response.put("id", 3);
                    response.put("name", "course1");

        return new ResponseEntity<>(response, HttpStatus.OK);
     }
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM