简体   繁体   中英

ModelAndView returning method object

I have this code:

@Controller
public class createUserController {

    @RequestMapping(value = "/user/registerUser", method = RequestMethod.GET, produces = "application/json")
    public ModelAndView createFleet(HttpServletRequest request, HttpServletResponse response, User user) {

        ModelAndView model = new ModelAndView();
        model.setView(new MappingJackson2JsonView());


        model.addObject("userCreated", new UserCreateResponse(user.getId(), user.getAccessCode()));

        return model;

    }
}

With response:

   {
  "user": {
    "id": null,
    "userName": "3",
    "password": "eccbc87e4b5ce2fe28308fd9f2a7baf3",
    "email": "3",
    "accessCode": "3"
  },
  "userCreated": {
    "id": null,
    "accessCode": "3"
  }
}

I don't want the main User to be included in the response. I just wanted the cut down details, so I'm not sending back the original request. I've tried .clear() but that gives a 404 error

You can change your controller as below. As you are trying to register users, you should use POST instead GET.

@RestController
public class createUserController {

    @PostMapping("/user/registerUser")
    public UserCreateResponse createFleet(HttpServletRequest request, HttpServletResponse response, User user) {

      return new UserCreateResponse(user.getId(), user.getAccessCode());        

    }
}

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