简体   繁体   中英

Java spring boot - returning a json response

I want to create a custom json response for a new Java Spring Boot application. At the moment I just want to create a dummy json object -- to start controlling the structure of the output json response.

When I do this it wants to create cast prefixes to the .puts -- I used to use Mongodb -- BasicDBObject -- but now I've not got mongo here.

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

what do I do instead?

--

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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