JSON body in POST

16 views
Skip to first unread message

Rick Mann

unread,
Oct 6, 2016, 11:41:25 PM10/6/16
to barat...@googlegroups.com
I'm trying to implement a webapp that has, among other things, Users. Via the web, my customers need to be able to register (create users with some amount of validation), login, look themselves up, make changes. Typical stuff.

I see the user example in github (https://github.com/baratine/baratine/tree/master/examples/user) and find it baffling. It's far more complex than any of the Getting Started or Tutorial stuff.

So, working from the Getting Started and Tutorial examples, I'm trying to do something like this:

@Service
public
class
App
{
@Post("/users")
public
void
createUser(@Body User inUser,
RequestWeb inRequest)
{
System.out.println("User: " + inUser.login + ", first: " + inUser.first + ", last: " + inUser.last);

inRequest.status(HttpStatus.CREATED);
inRequest.ok(inUser);
}
public
static
void
main(String[] inArgs)
throws
Exception
{
Web.include(App.class);

Web.go(inArgs);
}
}

And I hit that with:

curl -vv -X POST "http://localhost:8080/users" -H "Content-Type: applicatation/json" --data '{"first":"First","last":"Last","email":"m@m.c","password":foo","login":"first"}'

But I get an exception:

java.lang.IllegalStateException: Unknown body type: class com.latencyzero.hoa.User

Now, I have a super-simple User class in there, too (all my code compiles):

public
class
User
{
String login;
String hashedPassword;
String first;
String last;
String email;
}


So, what am I missing?

Followup question: If Baratine is going to create my User object for me, well, that in some ways doesn't work. I never want the User object to have a clear-text password in it, nor do I want a field for that. But I do want to be able to include one in the JSON that's posted. In my user creation processing, I want to hash the password and store that (by the way, that strikes me as something that should be done asynchronously, right?).

So, do I need to create some *other* User object which would be the fields of the expected JSON payload? Is there another way to get individual field values out of a JSON body? I tried [@Body("login") String inLogin] as a parameter to my createUser() method, but it threw an exception saying it expected a form-encoded body. I don't see any reason why the body can't be JSON, especially with the Content-Type set.

--
Rick Mann
rm...@latencyzero.com


Alex Rojkov

unread,
Oct 6, 2016, 11:47:10 PM10/6/16
to Rick Mann, Baratine
Rick, can you check the spelling for content type?

"Content-Type: applicatation/json" --
> --
> You received this message because you are subscribed to the Google Groups "Baratine" group.
> To unsubscribe from this group and stop receiving emails from it, send an email to baratine-io...@googlegroups.com.
> For more options, visit https://groups.google.com/d/optout.

Rick Mann

unread,
Oct 7, 2016, 12:05:31 AM10/7/16
to Alex Rojkov, Baratine
Oops! Okay, that fixed it, thank you. Curl has a way of corrupting my bash shell command history, and I screwed up typing the content-type addition.

Followup question: If Baratine is going to create my User object for me, well, that in some ways doesn't work. I never want the User object to have a clear-text password in it, nor do I want a field for that. But I do want to be able to include one in the JSON that's posted. In my user creation processing, I want to hash the password and store that (by the way, that strikes me as something that should be done asynchronously, right?).

So, do I need to create some *other* User object which would be the fields of the expected JSON payload? Is there another way to get individual field values out of a JSON body? I tried [@Body("login") String inLogin] as a parameter to my createUser() method, but it threw an exception saying it expected a form-encoded body. I don't see any reason why the body can't be JSON, especially with the Content-Type set.




--
Rick Mann
rm...@latencyzero.com


Reply all
Reply to author
Forward
0 new messages