ASP.NET WEB API PUT method Nullreference Exception - json

I'm trying to pass an object named "User" with user data in HTTP PUT method. I've created a custom controller method, gave it a [HttpPut]. In my application i'm calling a proper route with PUT header, but when i'm trying to pass an User object in JSON format my WEB API is throwing nullreference exception, which indicates that user object isn't passed. MY other custom POST methods are working fine, I'm having problem only with PUT method. My object in JSON format has proper formatting, I double-checked. What may be causing the problem?

I managed to fix the problem. In my User model constructor i deleted initialization of lists of other objects and it worked.

Related

Razor Page, object list prop losing all members between RedirectToPage and the OnGet call

I'm using the Xero API to do some calculations on employees. This requires an OAuth2Token, specifically the tenantId and the accessToken. This is gotten by redirecting the user to log in and then providing them with the token
The tenantId is a generic list of Tenant. I grab the token through API means and then I assign it to a variable as a XeroOAuth2Token.
Then I send it as the route value. Using breakpoints I can see that the variable looks fine as it is sent to RedirectToPage.
Yet the immediate OnGet that is called, everything is the same except the TenantList no longer has any members?
The solution to this is to serialize the object and send it as a prop inside an anonymous object, I want to know why I have to do that. Thank you for any insight

Grails - Do you still need parseRequest for JSON binding to work in controller?

Ok, this is my Nth question regarding this topic, and I'm getting really frustrated with Grails. Please have a quick look on one of my earlier questions for more details.
Among other things, my problem is that sending JSON formatted data to the controller when testing doesn't seem to work. The controller doesn't get null object, but the argument passed is practically empty--the JSON properties don't get set.
Aside from the controller code from the link above, I also tried,
def save() {
def model = new MyModel(request.JSON)
model.save()
}
but it still fails to set properties.
From my Web searches, I read that in older versions, parseRequest must be set to true in UrlMapping.groovy so that request data formatted in XML, JSON, etc. would automatically be parsed and passed as controller method argument. I'm working on Grails 2.3.9, and I'm not sure if it's still necessary to do that.
The time I thought I'd save if I use Grails on this project is being spent on looking for an answer to this seemingly simple task of testing a RESTful Web service.
No since 2.3.0 the parseRequest option doesn't do anything. The request is parsed lazily only when request.XML or request.JSON is accessed or when binding to a command object.

AutoBean Compile Error: "Parameterization is not simple..."

I'm trying to use AutoBean on the server and client to send and receive json data through AppEngines channel API. I don't want to store this data in the datastore. I already have a Proxy for this object that I use for the RequestFactoryServlet (which underneath just uses AutoBean anyways), so this should be doable. Instead of writing up a new Proxy for the object that exactly duplicates the Proxy for the RequestFactoryServlet, I'd like to just use the proxy that I use for the RequestFactoryServlet. The only problem is that I get an error while compiling that comes from my AutoBeanFactory.
Invoking generator
com.google.web.bindery.autobean.gwt.rebind.AutoBeanFactoryGenerator
[ERROR] The com.wmba.wmbaapp.shared.ObjectProxy parameterization is not simple, but the obj method does not provide a
delegate
So I'm not really sure what to do here. It seems like before I added the client side in, it's able to serialize the object into JSON just fine, but for some reason it doesn't like this. It sounds like it wants a delegate from me, but I can't find anything on this from the internet.
Anyone have any ideas?
Note: I also tried the same thing with EntityProxy (which is the base of the RequestFactory framework from what I read on the AutoBean page, but I get the same error).
The issue is that EntityProxy defines the stableId method which is not a getter (name doesn't start with get). That makes it a not simple bean, for which AutoBeans require a real bean instance to be wrapped in the created AutoBean (the delegate, passed as an argument of the type of the AutoBean –ObjectProxy in your case– to your obj method of the AutoBeanFactory).
In other words, AutoBeans expects your obj method to be of the form:
AutoBean<ObjectProxy> obj(ObjectProxy toWrap);
The simplest solution is to not try to reuse the entity proxy with AutoBeans.
You might be able to make it work though by annotating your AutoBeanFactory with:
#Category(EntityProxyCategory.class)
You might have to add #NoWrap(EntityProxyId.class) too, see http://code.google.com/p/google-web-toolkit/source/browse/trunk/user/src/com/google/web/bindery/requestfactory/vm/InProcessRequestFactory.java
It turned out for me that I had a property setter that had an empty parameter list in my Ojbect interface. It didn't have anything to do with the factory, except for the interface the factory was trying to create a proxy for:
interface Factory {
AutoBeans<MyObject> createObject();
}
interface MyObject {
String getProperty();
void setProperty();
}
A bone-headed mistake but held me up with this precise compiler error. Adding the Category annotation as mentioned in the previous answer identified the faulty property setter.

How, in Sinatra, to pass a Request Body to other classes?

I'm using a Sinatra app to receive server requests and I want to dissect them in a separate class I call "request", but when I pass the request object the body gets dropped. Trying to read the request.body in the main class works but trying to read it in the new class generates a JSONparser octet error.
In the main Sinatra file, this test call generates the correct response:
puts JSON.parse request.body.read
after, I pass the request to the Request Class with the code below.
req=Request.new(request)
But in the Request class initialization def, the same "puts" code above generates the error:
JSON::ParserError - A JSON text must at least contain two octets!:
Both files include the JSON requirement.
A work around is fairly simple but I would prefer the more elegant solution if I could figure out why it is not working as I expect. Any thoughts are appreciated.
from my tests
the Request.new constructor doesn't seem to clone from Request object
request.clone works proper
you need to do the thorough object inspection if you need anything extreme

How to construct objects and related objects from JSON request using Spring

I'm POSTing a JSON request to a Spring 3.0 controller. The method signature is...
#RequestMapping(value="/add", method=RequestMethod.POST)
public #ResponseBody Map<String, ? extends Object> add(#RequestBody Entry)
The JSON looks like this...
{"user":"1"}
The Entry object has one attribute of type User.
When a request is submitted this error is thrown,
org.codehaus.jackson.map.JsonMappingException: Can not construct instance of com.x.y.z.Entry, problem: no suitable creator method found
I'm guessing the error is due to the fact that user on Entry is of type User rather than String ("1" is being passed in on the JSON).
Is there a way of taking the "1" coming in and using it to create a real User object (by looking it up in the database in this case)?
Does Entry have a parameterless constructor?
That's your first place to look. Normally errors of this nature occur because the code is looking for a parameterless constructor to create Entry with.
Your idea, that is to create User as the real user is fine, but it should be done after this method is called, in some other layer or something. You wanna keep things simple by not interfering with the marshalling of the json. You can add onto this with another layer.