简体   繁体   中英

response not return using spring rest annotations

Strange issue that spring rest is not returning any response. I have tried using spring boot with @RestController annotation.

According to docs, it includes @ResponseBody and @controller however, on client the response is still pendin as xhr(pending)

I am not sure what is the issue. Looks simple but pretty bad I say.

@RestController
public class MyUserController {

    @Autowired
    private UserService userService;

    @RequestMapping(path = "/registration", method = RequestMethod.POST)
    public ResponseEntity registration(@RequestBody User userBean) throws Exception {

        HttpHeaders headers = new HttpHeaders();

        if (userService.isUserExist(userBean)) {
            System.out.println("user already exists");
            return new ResponseEntity(headers, HttpStatus.CONFLICT);
        } else {
            userService.saveUser(userBean);
            return new ResponseEntity(headers, HttpStatus.OK);
        }
    }
}

Client side javascript(node js) to send ajax request:

var express = require('express');

app.post('/registration', registration);

function registration(req, res, next){
    request({
        url:'http://localhost:8084/RootScopeIT_Riot/registration',
        method:'POST',
        json: {
            "username": req.body.username, "password": req.body.password
        },
        function(error, response, body){
            if(error){
                console.log('err while registration');
                return res.send(error);
            }else{
                console.log('success registration');
                return res.send({body});
            }
        }
    });
}

I don't think you can do return of res.send . Just remove return statement and let me know if that works.

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