MVC4 Model Binding - Null Values - json

I know there are a bunch of questions on this already - I'm having a hard time sorting out which ones are related to problems with versions, and which ones are related to jQuery (which I'm not using), etc. I have the MVC4 RC (4.0.20505.0), Visual Studio 2010 Ultimate SP1.
I have a complex type in my model:
public HttpResponseMessage Post([FromUri]Person person)
{
TableStorageHelper personHelper = new TableStorageHelper();
personHelper.Save(personHelper.GetTableNameForType("Person"), person);
var response = Request.CreateResponse<Person>(HttpStatusCode.Created, person);
return response;
}
I am passing in this JSON string - using Fiddler mostly, but also trying from code in another controller (trying to do all testing locally just to verify that I can get values in the object received by the controller):
The JSON:
{"FirstName":"Andy","LastName":"Schultz","PartitionKey":"USW","RowKey":"per-928c8f74-2efd-4fc2-a71c-fb3ea8acc6d7","NickName":null,"FullName":"Andy Schultz","Description":null,"ImageLocation":null,"Region":"USW","CommentsAboutMe":{"Comments":[]},"CommentsByMe":{"Comments":[]}}
All of the properties here do exist in the class.
The code from the other controller:
HttpWebRequest request = HttpWebRequest.Create("http://127.0.0.2:8080/api/persons/") as HttpWebRequest;
request.Method = "POST";
request.ContentType = "text/json";
using (var writer = new StreamWriter(request.GetRequestStream()))
{
Person person = new Person("Andy", "Schultz", "USW");
Formatting formatting = new Formatting();
JsonSerializerSettings settings = new JsonSerializerSettings();
settings.ReferenceLoopHandling = ReferenceLoopHandling.Ignore;
string json = JsonConvert.SerializeObject(person, formatting, settings);
writer.Write(json);
}
Every time, I get an empty Person object in my controller (I'm debugging on the very first line there). Every value is NULL.
You may have noticed the [FromUri] attribute on my controller - I had to do that to get around an error telling me there was no ModelBinder for an undefined type, but I'm not sure that it's correct - I'm not passing any info in the Uri, it's in the body, as you can tell.

Thank you Mike Stall: http://blogs.msdn.com/b/jmstall/archive/2012/04/16/how-webapi-does-parameter-binding.aspx
The [FromUri] attribute did indeed tell my controller to read the uri and not the request body looking for the parameter for my controller method. It wasn't there, so everything was null.
The error that adding that attribute fixed, which said there was no formatter defined for a type of content Undefined, was caused by my improperly declaring the content-type of the request. The correct way was "Content-Type: text/json; charset=utf-8

Related

PostAsJsonAsync not calling the Controller when <Tvalue> has null fields

My TValue object has foreign key related objects, which has null values when posting; I am having the logic to set the FK objects in the repository. The issue I am facing is that API controller is not getting called when FK objects have all fields null. Please see screenshot. The same code works if I set the value for all but the ID field of the FK objects from the front end.
Is the issue because Json serializer checking for nulls? I have also tried to set the null check ignore option. I am not getting an error on PostAsJsonAsync and the control simply goes to the next line of code
return await result.Content.ReadFromJsonAsync();
without calling the API controller and send an exception
public async Task<SubContract> AddSubContract(SubContract subContract)
{
/* On the injected httpClient, call the PostAsJsonAsync method and pass the subContractObject
* We also need to specifi the api Uri in the parameter list */
JsonSerializerOptions option = new()
{
DefaultIgnoreCondition = JsonIgnoreCondition.WhenWritingNull
};
var result = await httpClient
.PostAsJsonAsync<SubContract>("api/SubContracts", subContract, option);
//Use the content object and ReadFromJsonSync method and typecast it to <SubContract>
return await result.Content.ReadFromJsonAsync<SubContract>();
}
Screenshot
--- Further observations ---
#Serge Thanks for the response. You are right, I am using .Net 6. I have now commented out the nullable but I still have the same issue. Further, I tried to change the function to PostAsync instead of PostAsJsonAsync; below is the new code
// ---- Post Asysc Option -----
var subContractSeralized = JsonSerializer.Serialize(subContract, option);
var stringContent = new StringContent(subContractSeralized,
encoding: System.Text.Encoding.UTF8, "application/json");
var response = await httpClient.PostAsync("api/subcontract", stringContent);
return await response.Content.ReadFromJsonAsync<SubContract>();
I initially thought it was a serialization issue because of the nulls in the nested object but when I debug the new code, I get the below result
subContractSerealized = '{"Id":0,"Name":"Aquatic-Repairs","Status":"In-Progress","WorkTypeId":1002,"WorkType":{"Id":0},"SiteId":3,"Site":{"Id":0},"OrganizationId":3,"Organization":{"Id":0}}'
If you compare this with the Debug screen shot in my first post, you can see that the null value fields in the nested objects are omitted out
Response StatusCode = “Not Found-404”
I am not sure how Response Status code is obtained as the API is not called. I.e. httpClient.PostAsync does not transfer control to the API and my debug breakpoint is not hit.
I tried the same code for an Entity model that has no nested foreign key related objects and it works fine and I am able to add the record to the DB. I have the “Required” validation set on the field properties of the entity models; however, after the API call, I have my repository that is taking care of it. So, I doubt that is an issue. In any case, the code is not even hitting the API and simply returns an 404 NotFound on httpClient.PostAsync.
you must be using Net 6 API, and it causes a validation error. Try to comment Nullable in your API project (your serializer option is not working in this case)
<PropertyGroup>
<TargetFramework>net6.0</TargetFramework>
<!--<Nullable>enable</Nullable>-->
<ImplicitUsings>enable</ImplicitUsings>
</PropertyGroup>

Solr custom query parser "fl" parameter not returning desired result

I am currently working on replacing an existing searching platform with Solr. I am new to Solr and currently using Solr Version 6.6.0. Legacy system accepts a JSON object with search parameters. Thus I was asked to implement a custom query parser which accepts a query string in JSON format.
Essentially NO other parameter is passed to Solr except "q" which contains all the required parameters encoded in a JSON formatted string. All the common parameters and local parameters required to execute the query is then extracted by the query parser itself.
This feat was successfully achieved except the "fl" parameter will not give the expecting results. Which means query result composes of all the fields that are present in the document. Code looks like this;
public static final String FIELD_LIST = "fl";
Collection for storing the key value pairs.
HashMap<String,Object> paramMap = new HashMap<String, Object>();
Decoding and formatting the field list.
String ifld = getFieldList();
Parameters are put in the collection thus.
paramMap.put(Constants.FIELD_LIST,ifld);
SolrParams are then generated.
return SolrParams.toSolrParams(new NamedList<>(paramMap));
Each and every query parameter is processed this way and finally in the query parsers parse() method i have this.
//calls the factory method to get the appropriate request type translator
JSONRequestTranslator jrt = (JSONRequestTranslator)JSONQParser.getTranslator(requestType);
//converts the arguments to solrparams.
qstr = jrt.getQueryString();
localParams = jrt.getLocalParams();
params = jrt.getParams();
req.setParams(SolrParams.wrapDefaults(params, this.req.getParams()));
return jrt.parse(qstr, localParams, params, req);
parse() method in classes implementing the JSONRequestTranslator interface looks like this
#Override
public Query parse(String qstr, SolrParams localParams, SolrParams params, SolrQueryRequest req) throws SyntaxError {
return new DisMaxQParser(qstr, localParams, params, req).parse();
}
This works fine but the "fl" parameter seems to have no effect. I don't have a default setting in solrconfig.xml file and while i'm debugging i always find the "fl" parameter set to correct value. I have tried several other approaches like writing custom search component plugins but please explain how to fix this and what am i missing here. Thank you.

Cannot access Restlet service using POST from SOAP UI

I created a series of REST services in Java using Restlets. The majority of these services use JSON, and I have no problem accessing them using SOAP UI via a GET request. However, when I try to access POST based services using SOAP UI, the Representation entity parameter is always null. I have searched Stack Overflow as well as the web, but could find nothing which I either haven't already done, or which addresses my problem.
Here is the code for a POST resource which always seems to receive a null entity:
public class CreateAccountResource extends ServerResource {
#Post("json")
public Representation createAccount(Representation entity) throws IOException {
String message = null;
boolean result = true;
try {
String post = entity.getText();
Object obj = new JSONParser().parse(post);
JSONObject jsonObject = (JSONObject) obj;
String username = (String) jsonObject.get("username");
String password = (String) jsonObject.get("password");
String email = (String) jsonObject.get("email");
// more code
}
catch (Exception e) {
// handle exception here
}
}
}
And here is a screen shot from my SOAP UI showing the configuration I used when sending the request:
In case you are wondering, I am using IntelliJ in debug mode to inspect the value of the entity, and the project uses Maven.
I never use Restlet however I think that since you specify #Post("json") annotation for your createAccount method; the method is waiting for a json in the POST body instead of passing the values as a query parameters.
So probably you must change your actual POST with the query parameters to a POST call to your URL http://localhost:8080/MyApp/service/createAccount passing the parameters in the body as json:
{
"username" : "tim",
"password" : "password",
"email" : "tim#me.com"
}
In SOAPUI could be something like:
Hope it helps,

Restlet POST Form to send to client resource via JSON

I am getting a 415 Error when sending a form entry to another client resource via JSON. The target URI in my code below ("/message") works when not using the form (i.e. hit "/message" with a test mock object).
Here is my code to get the values of the form and do the post to the target resource. Am I missing something that needs to be done?
I am using the following:
Restlet: 2.1 RC5
GAE: 1.6.1
Form Restlet:
#Post
public void handlePost(Representation entity) {
final Form webForm = new Form(entity);
MessageEntity newMessage = new MessageEntity();
String subject = webForm.getFirstValue("subject");
String sendto = webForm.getFirstValue("email");
String message = webForm.getFirstValue("message");
newMessage.setCategoryID(subject);
newMessage.setAccountID(sendto);
newMessage.setMessageText(message);
ClientResource cr = new ClientResource(getRootRef()+ "/message");
cr.post(newMessage, MediaType.APPLICATION_JSON);
}
Target Resource ("/message")
#Post("json")
public void HandleRequest(MessageEntity messageEntity) {
// Logic here
}
Please let me know if you need more information
Thanks!
I have code that is very similar to yours that works fine. I am also running similar versions of Restlet and GAE. First question I have is are there other #Post methods in your Target Resource as sometimes the ordering matters.
Here are two versions of code that I have that work....
1)
public Representation postHandler() {
Reference commitsRef = new Reference(Consts.RESOURCE_BASE + "commitments/");
ClientResource commitsResource = new ClientResource(getContext(), commitsRef);
....
Representation commitsRep = commitsResource.post(commitForm);
That is posting a form to a Target resource that handles both #Post("json") and #Post("form")
2)
public Representation doPostFromGet() {
Reference takeActRef = new Reference(Consts.RESOURCE_BASE + "commitment/"
+ commitmentId + "/userActs/");
ClientResource takeActResource = new ClientResource(getContext(), takeActRef);
...
Representation takeActRep = takeActResource.post(newAct);
That is posting a Java object to a form that uses what I call the "Peierls magic". See:
http://tembrel.blogspot.com/2012/03/converting-forms-in-restlet-to-pojos.html
It allows you to have one post() in the Target and accept both forms and pojos.
On a minor note, if you are doing a post to add a new message, should the url be "/messages/" (plural) - and perhaps there is a typo somewhere? (An unlikely possibility, but I thought I would mention it).
Good luck,
RB

javax.xml.bind.UnmarshalException when request rest json service with jersey client

I have a rest webservice (with jersey) which returns json list, if i call it directly it returns exactly this :
[{"success":false,"uri":"foo:22","message":"Unknown host : foo"},{"success":true,"uri":"localhost:8082","message":null}]
generated by this snippet :
#GET
#Path("/opening/")
public List<OpeningResult> testOpenings(#QueryParam("uri") List<String> uris) {
LOG.debug("testOpenings request uris :[" + uris + "]");
List<OpeningResult> openingResults = infoService.testOpenings(uris);
return openingResults;
}
It's a Collection of Pojo which look like this :
#XmlRootElement(name = "OpeningResult")
public class OpeningResult {
attributes
...
getter/setter
}
this Pojo is shared through a common jar between the server and the client.
i call the web service with this snippet :
Client client = Client.create();
WebResource resource = client.resource("http://localhost:8080/scheduler/rest/opening");
MultivaluedMap<String, String> params = new MultivaluedMapImpl();
for (String uri : uris) {
params.add("uri", uri);
}
List<OpeningResult> results = newArrayList(resource.queryParams(params).get(OpeningResult[].class));
I add some trace on the server side, i see that my rest service is called with the good parameters, buth on client side, i have this error :
Caused by: javax.xml.bind.UnmarshalException: unexpected element (uri:"", local:"success"). Expected elements are <{}OpeningResult>
I don't find where it comes from ?
Modify your code to set up your client like this:
ClientConfig clientConfig = new DefaultClientConfig();
clientConfig.getFeatures().put(JSONConfiguration.FEATURE_POJO_MAPPING, true);
Client client = Client.create(clientConfig);
I had the exact same problem until this question and its answers pointed me in the right direction.
The situation is caused by the default jersey-json module used for serialization to and from JSON, which does not handle certain JSON constructs properly.
You can set the FEATURE_POJO_MAPPING flag to use the Jackson library's JacksonJsonProvider for JSON serialization instead.
Check out the Jersey Client side doc on using JSON. It looks like you're at least missing the annotation:
#Produces("application/json")
But you could also be missing the POJO Mapping feature filters for both client and server side. These all seem to be minor configuration changes.