0

Can someone please help me how to get a JSON String in a Webservice. I's sending JSON to my /api/register that looks like:

{"name":"MyName","surname":"MySurename","email":"[email protected]","street":"MyStreet","number":"3","zip":"12345","city":"myCity","pass":"myPassword"}

Here is my register.java file:

@Path("/register")
@Stateless
public class RegisterWS {

    @EJB
    UserBS userBS;

    @POST
    @Consumes(MediaType.APPLICATION_JSON)
    public void createUser(){

        // code to get data from json

        userBS.createUser(name, surename, email, adress, number, zip, city, password);

    }
}

My AngularJS Controller and Service. The Data comes from a form, that is parsed to a JSON object.

app.service('RegisterService', function ($http) {

    return {        

        registerUser : function(user) {

            $http.post('http://localhost:8080/myApp/api/register')
                    .success(function (user) {
                        return user;
                    })
                    .error(function (data) {
                        // failed

                    });
        }

    }

});

app.controller('RegisterCtrl', function($scope, RegisterService) {

    $scope.register = function(){
        RegisterService.registerUser(angular.toJson($scope.user));
    }

});

2 Answers 2

1

You should have a POJO, which maps to the received JSON object, for example a User class. In this case this would be a very simple Java Bean, with mostly String properties for each field in the JSON.

@XmlRootElement
public class User {
    String name;
    String surname;
    String email;
    String street;
    Integer number;
    String zip;
    String city;
    String pass;
}

Of course you would use private fields, with getters and setters, but I did not want to add clutter. By the way the @XmlRootElement is a JAXB annotation, and JAX-RS uses JAXB internally.

After you have this, you just need to change your method like this

@POST
@Consumes(MediaType.APPLICATION_JSON)
public void createUser(User user) {
    ...
}

You should not need to change anything on the AngularJS side, as the default for the $http.post method is JSON communication.

Sign up to request clarification or add additional context in comments.

1 Comment

I have a POJO, just didnt put the @XmlRootElement on top of it. thanks a lot!
0

For your Java code, you have to add a User POJO, I dont know if you will use some persistence API or not, so the user POJO must implement serializable to output user object as JSON.

Here's a an example of REST app with EJB ... : http://tomee.apache.org/examples-trunk/rest-on-ejb/README.html

For your client app, you need to specify the content type : "Content-Type" = "application/json"

See this questions: change Content-type to "application/json" POST method, RESTful API

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.