0

This is a follow up question to this question:

Passing custom type query parameter

I got a class which includes this method:

public static MyClass fromString(String json) 
{
   Gson gson = new Gson();
   MyClass user = gson.fromJson(json, MyClass.class);
   return user;
}

The full class:

public class MyClass
{
    public String name;
    public PortalNameEnum  portalName;
    public PortalUserTypeEnum portalUserType;
    public String   notes;

public MyClass(String name, PortalNameEnum portalName,
        PortalUserTypeEnum portalUserType, String notes)
{
    super();
    this.portalName = portalName;
    this.portalUserType = portalUserType;
    this.name = name;
    this.notes = notes;
}

public static MyClass fromString(String json) 
{
   Gson gson = new Gson();
   PortalUserInfo user = gson.fromJson(json, PortalUserInfo.class);
   return user;
}

public PortalNameEnum getPortalName()
{
    return portalName;
}

public void setPortalName(PortalNameEnum portalName)
{
    this.portalName = portalName;
}

public PortalUserTypeEnum getPortalUserType()
{
    return portalUserType;
}

public void setPortalUserType(PortalUserTypeEnum portalUserType)
{
    this.portalUserType = portalUserType;
}

public String getName()
{
    return name;
}

public void setName(String name)
{
    this.name = name;
}

public String getNotes()
{
    return notes;
}

public void setNotes(String notes)
{
    this.notes = notes;
}

}

I got a resource which got a method:

@Path("/myclasscall")
@GET
@UnitOfWork
public String registerPortalUser(@Context HttpServletRequest req, @QueryParam("callback") String callback, @QueryParam("myclass") MyClass recordData) throws Throwable
{ .. }

It seems like the fromString method is not called and the resource method is always null, even though I see in the console the request itself and I do see a string that has been passed. Why is that?

Community
  • 1
  • 1
CodeMonkey
  • 11,196
  • 30
  • 112
  • 203
  • Can you post the full MyClass – Paul Samsotha Sep 13 '16 at 07:52
  • edited with the class – CodeMonkey Sep 13 '16 at 07:57
  • Also you might want to post a complete runnable example. Preferrably in a single class with [Jersey Test Framework](https://jersey.java.net/documentation/latest/test-framework.html). Other thing you can do is start with the simplest case, i.e. a class with nothing but a single field, and the from String method. In the resource method have nothing but that one parameter. It will work. Start adding things in to try to narrow down the problem – Paul Samsotha Sep 13 '16 at 08:00
  • My guess would be it's a problem with the Gson (you having no default constructor might be causing an exception that gets swallowed) – Paul Samsotha Sep 13 '16 at 08:04
  • I got a ParamConverterProvider which does get called, but the value string it gets is always null for some reason. I can't eliminate the other parameters from the resouce method since its JsonP and it must send a callback parameter – CodeMonkey Sep 13 '16 at 08:14
  • Make a different one for testing. Simplest case first. Then start adding stuff to find out what causes the problem. Other than that I would need a complete test case. Can't do much for you with just the information provided – Paul Samsotha Sep 13 '16 at 08:17
  • And you don't know without testing why would the param converter receive null as its string value even though the request does include another parameters after the callback parameter? – CodeMonkey Sep 13 '16 at 08:18
  • No. I will put together a starter test for you. Give me a few minutes – Paul Samsotha Sep 13 '16 at 08:19
  • [Here](https://gist.github.com/psamsotha/af6dae41ea9174cb057fbed7e0425179). See if you can reproduce the problem, and post it here. – Paul Samsotha Sep 13 '16 at 08:33
  • How do i run it? tried running it as junit test and the configurations method were called but it did not reach the doit method at all. Also got these messages: Trying to load META-INF/validation.xml for XML based Validator configuration. DEBUG o.h.v.i.xml.ValidationXmlParser - No META-INF/validation.xml found. Using annotation based configuration only. – CodeMonkey Sep 13 '16 at 08:49
  • I don't know I never got that. I just ran it from my IDE. Try creating a new clean project, and just adding that one dependency from the comment – Paul Samsotha Sep 13 '16 at 08:52
  • Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/123223/discussion-between-peeskillet-and-yonatan-nir). – Paul Samsotha Sep 13 '16 at 08:53

1 Answers1

0

The problem was with the client. Instead of passing a single parameter called "myclass", he passed all the fields separately. After merging them together into a single Json instance, it was fixed.

CodeMonkey
  • 11,196
  • 30
  • 112
  • 203