Create
cancel
Showing results for 
Search instead for 
Did you mean: 
Sign up Log in

Custom Rest API JSON input?

Ubisoft
Rising Star
Rising Star
Rising Stars are recognized for providing high-quality answers to other users. Rising Stars receive a certificate of achievement and are on the path to becoming Community Leaders.
July 30, 2015

I'm trying to build an extension to the rest api for jira, and I'm hitting a roadblock in trying to get it to accept my json input:

It's essentially built to create users while being able to select a directory, from a json input.

 

@Path("/user")
public class MyRestAPI {
    private static final Logger log = LoggerFactory.getLogger(MyRestAPI.class);
    @POST
    @Produces({MediaType.APPLICATION_JSON, MediaType.APPLICATION_XML})
    @Path("/create")
    @PublicApi
    public Response createUser(MyUserWriteBean scuwb) {
        String username = scuwb.getName();
        log.debug(username);
        User newuser;
        try {
            String directory = scuwb.getDirectory();
            UserUtil usutil = ComponentAccessor.getUserUtil();
            String fullname = username.split("@")[0].replace(".", " ");
            log.debug(directory + fullname+ username);
            newuser = usutil.createUserNoNotification(username, null, username, fullname, Long.parseLong(directory));
        } catch (Exception e) {
            return Response.ok(new MyRestModel("REST Create user error "+e.getMessage())).build();
        }
        return Response.ok(new MyRestModel("User Created "+newuser.getName())).build();
    }

the bean class is something that I thought was necessary by reverse engineering what I round in the source code for the existing API.  

{
    "username":"my.name@ubisoft.com",
    "directory":"1"
}

 

When I try to text my rest function in the rest api browser, i'm getting an unsupported format error, and my logs in the actual class are returning null.

How can I get this to work?

Thank you,

Eric

1 answer

1 accepted

Comments for this post are closed

Community moderators have prevented the ability to post new answers.

Post a new question

1 vote
Answer accepted
Volodymyr Krupach
Rising Star
Rising Star
Rising Stars are recognized for providing high-quality answers to other users. Rising Stars receive a certificate of achievement and are on the path to becoming Community Leaders.
July 30, 2015

Try to add @Consumes(MediaType.APPLICATION_JSON) above public Response createUser(MyUserWriteBean scuwb)

Ubisoft
Rising Star
Rising Star
Rising Stars are recognized for providing high-quality answers to other users. Rising Stars receive a certificate of achievement and are on the path to becoming Community Leaders.
July 30, 2015

Thanks! I've gotten past the first error, but the data from my json request is still not making it to my function. I'm getting null values across the board. Where does that input data end up?

Volodymyr Krupach
Rising Star
Rising Star
Rising Stars are recognized for providing high-quality answers to other users. Rising Stars receive a certificate of achievement and are on the path to becoming Community Leaders.
July 30, 2015

Your bean should have either public getters and setters or public fields. Here is snippet from a bean that comes as param to my custom JSON: @XmlRootElement @XmlAccessorType(XmlAccessType.FIELD) public class ExternalAppExecutorInModel { @XmlElement(name = "devlead") public String devlead; @XmlElement(name = "key") public String key; ... }

Ubisoft
Rising Star
Rising Star
Rising Stars are recognized for providing high-quality answers to other users. Rising Stars receive a certificate of achievement and are on the path to becoming Community Leaders.
July 30, 2015

And it's working now!!!! You have my eternal thanks. :-) Eric

TAGS
AUG Leaders

Atlassian Community Events