Java spring boot - 返回json响应

时间:2017-09-05 01:13:14

标签: java json spring

我想为新的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);
        }

1 个答案:

答案 0 :(得分:-2)

**@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);
     }
}