1

I have set up a spring RESTful web service for taking in the username and password from a user. Been following the tutorial on Spring IO

My service is set up to accept user name and password as shown below:

 @Controller
   @RequestMapping("/users")
   public class UserCommandController {
        private static Logger log = LoggerFactory.getLogger(UserCommandController.class);
        @RequestMapping(method = RequestMethod.POST)
        public ResponseEntity  createUser(@RequestBody UserDetail userDetail, UriComponentsBuilder builder) {
            User newUser = new User();
            newUser.setEmail(userDetail.getEmail());
            newUser.setPassword(userDetail.getPassword());
            newUser.setUserName(userDetail.getUsername());
            try {
                UserFactory.getInstance().saveNewUser(newUser);
            } catch(UserException ue) {
                log.error("Saving user failed. Exception: "+ue.getMessage());
            }
            return new ResponseEntity(HttpStatus.OK);
        }
    }

I am sending POST parameters to the service as a test through Google chrome plugin POSTMAN but I get "HTTP: 415..The server refused this request because the request entity is in a format not supported by the requested resource for the requested method."

enter image description here

Does anyone have an idea what I am doing wrong ?

Raedwald
  • 46,613
  • 43
  • 151
  • 237
tsure
  • 315
  • 3
  • 6
  • 16
  • tsure: where you able to resolve it? I am seeing the same issue. – Himalay Majumdar Jan 23 '14 at 18:29
  • possible duplicate of [Spring MVC 3.1 REST services post method return 415](http://stackoverflow.com/questions/14222681/spring-mvc-3-1-rest-services-post-method-return-415) – Raedwald Aug 06 '14 at 18:30

2 Answers2

1

Set the header:

Content-Type=application/json

This solved my problem!

Paresh Mayani
  • 127,700
  • 71
  • 241
  • 295
dhrubo
  • 171
  • 1
  • 9
0

The HTTP 415 response code means that the server expected data posted with a different content type. It appears you simply posted a form with username, password and email as parameters. That would result in a content-type of application/x-www-form-urlencoded.

Try posting with a content-type of application/xml or application/json. In your post body, you will need to put your data in the corresponding format. For example, if you use application.xml, the XML body should look something like:

<userDetail>
    <userName>xxx</userName>
    <password>xxx</password>
    <email>xxx</email>
</userDatail>

Of course the exact format (i.e. element names) depends on the XML bindings. In fact, whether or not the expected format is XML or JSON or something else is also likely a server configuration.

Posting a request of this type cannot easily be done with a browser. You will need some other HTTP client. A tool like SOAP-UI might be a good bet.

EJK
  • 12,332
  • 3
  • 38
  • 55
  • I tried JSON through POSTMAN which is a REST Client - Google chrome extension, still no luck, still get the same response from the server. – tsure Nov 24 '13 at 14:18
  • Can you create a method that (a) maps a URI to a GET request, (b) simply returns a single pre-populated UserDetail object in the response. Hit that URI in your browser. What it returns will hopefully show you the format that server is configured for. – EJK Nov 24 '13 at 16:15