0

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

Nicholas K
  • 15,148
  • 7
  • 31
  • 57
wonza
  • 302
  • 1
  • 4
  • 15
  • 2
    Annotate your controller with RestController, and just return a UserCreateResponse response from your method instead of a ModelAndView. Also, if this method is supposed to register a new user, why is it named createFleet(), and why does it use GET instead of POST? – JB Nizet Dec 03 '18 at 21:05
  • That did it, thanks. I meant to change to POST, I just copied it from another controller and needed to update it. – wonza Dec 03 '18 at 23:46

1 Answers1

0

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());        

    }
}
hardeep thakur
  • 311
  • 3
  • 9