SignalR .Net Core 3.1 unable to send object in SendAsync method from service class - json

Previously I was using .Net Core 2.2, I was able to send a json object from a service class using hub context and SendAsync method to a front end web client. Im having issues after I've upgraded my project framework to 3.1. If i call the SendAsync method using a json object, it will hit an error stating "{"The collection type 'Newtonsoft.Json.Linq.JObject' is not supported."}", if I send any other class object it will directly go to OnDisconnected.
Sample of the method i use
Using Json Object: await _hubContext.Clients.Group(groupName).SendAsync("NotificationResponse", jsonObject);
Using Class Object: await _hubContext.Clients.Group(groupName).SendAsync("NotificationResponse", notificationObject);
I've tried sending object directly from the hub, I was able to send a normal class object but not a json object. I did the testing on my project and also sample from this tutorial: https://learn.microsoft.com/en-us/aspnet/core/tutorials/signalr?view=aspnetcore-3.1&tabs=visual-studio

I posted this question on github and got the answer from BrennanConroy. Thanks alot !
His answer was:
2.1 was using Newtonsoft internally for Json, 3.1 uses System.Text.Json. If you're using features that don't work with System.Text.Json you can switch back to Newtonsoft https://learn.microsoft.com/aspnet/core/migration/22-to-30?view=aspnetcore-3.1&tabs=visual-studio#switch-to-newtonsoftjson
After doing this it fixed the issue.

Related

Spring Boot to return JSON String from an external API

I have a simple Spring boot project that uses controller mappings to get hard coded information from a class in my project.
For example, if I run the request : localhost:8080/topics, A JSON response is returned with the list of Topic Objects that i have previously created
I want to take this one step further and have a class who's variables are populated by calling this API and parsing the response : https://www.alphavantage.co/query?function=TIME_SERIES_INTRADAY&symbol=MSFT&interval=1min&apikey=demo
I believe this can be done in Java by creating a HTTP connection and reading the data from an input stream, but is the an easier way of doing this with spring boot? Im not fully sure of the name of this procedure hence Im having trouble finding solutions online
Since you are using Spring Boot, making use of Spring's RestTemplate makes sense. It comes with several message converters out of the box, and uses Jackson by default for json content.
Spring has published a good Getting Started page for consuming RESTful web services.
However, the json content returned by that services doesn't look like it will map well to a Java object, so you may have to deserialize it to a HashMap to get to the data you want.
I did an attempt to create something like this.
https://github.com/StanislavLapitsky/SpringSOAProxy
The idea is to register controller interfaces. Each of the interfaces are mapped to some URL. For the interfaces a dynamic proxy are generated (if the implementations are not available locally). So developer just call controller's interface method. The method is invoked for dynamically generated proxy. The proxy uses RestTemplate to call remote URL. It sends and receive JSON and deserializes the returned JSOn to POJO objects returned from the controller.
You need to declare contract - controller interfaces plus DTO to exchange data as well as mapping to understand which URL should be called for each controller.

REST API with JSON representations using Apache CXF without Spring

I want to support both XML and JSON representations of Resources in my REST API. I have no choice but to use CXF implementation of Jax-RS without using Spring. I am using JAXB to marshall and unmarshall objects. I have the following methods defined that return different representations of the same resource.
#GET
#Produces({MediaType.APPLICATION_XML, MediaType.WILDCARD})
#ElementClass(response = MyList.class)
public Response listXML(){
MyList list = getList();
return Response.status(
Response.Status.FOUND).
entity(list).type(MediaType.APPLICATION_XML).build();
}
#GET
#Produces({MediaType.APPLICATION_JSON})
#ElementClass(response = MyList.class)
public Response listJson(){
MyList list = getList();
return Response.status(
Response.Status.FOUND).
entity(list).type(MediaType.APPLICATION_JSON).build();
}
This works fine for the XML representation. But if I set the Accept header of my HTTP request to application/json I get the following message.
No message body writer has been found for response class MyList.
What am I missing here? The MyList class is generated by JAXB from XSDs and has all the necessary annotations Looks like I need to configure CXF to use a JSON provider. I haven't been able to find good documentation on configuring the JSON provider in the web.xml for a webapplication that doesn't use Spring. If anyone has got this work, please guide me.
I got it figured out. I needed to configure JSONProvider as one of initparams for NonSpringServlet in the deployment descriptor. This wasn't working for me before as I was missng the cxf extensions library and the jettison library. These dependencies don't get automatically pulled by maven or gradle if you only have a dependency on cxf front end jars.

How can I serialize Exceptions using Json.NET on WinRT?

This works fine on classic .NET:
string json = JsonConvert.SerializeObject(new Exception("Test"));
But it fails on WinRT (RP) with:
Error getting value from 'TargetSite' on 'System.Exception'.
The API 'System.Exception.get_TargetSite()' cannot be used on the current platform. See http://go.microsoft.com/fwlink/?LinkId=248273 for more information.
EDIT:
I also need to transfer the Exception over the wire and deserialize it on the back end side.
Anyone has any workarounds?

JSON issue in Spring 3 and

I am using Spring 3.0 and ExtJS. I have been trying to send a Map object from my controller to jsp. When I putting a pojo in HashMap and sending that HashMap to view.
From controller it is returning a Map but in ExtJS it is not able to read the response and gives below error.
HTTP Error code: 406
message
description The resource identified by this request is only capable of generating responses with characteristics not acceptable according to the request "accept" headers ().
Can anyone tell that how this can be resolved?
I dont think you can just shove any POJO into a map and return it via an HttpResponse. That's not how JSON works.
In order to send JSON from Java, you have to do the equivalent of serializing it using a JSON API (or roll your own). FlexJSON is one I use, as it ships in Spring Roo and is pretty easy.

JBoss/JaxWS/JaxB: SOAP and JSON binding to one webservice

Hello.
I've written a SOAP-webservice using JAX-WS and JAX-B, running on a JBoss 5.
Due to the overhead of SOAP I now try to enable the access to the webservice via JSON (but it should still be available via SOAP).
My question now is the following:
Is it possible to use the same webservice class (with the, via JaxB defined, bean class as parameter) for both endpoints?
Can I also use JAX-B to parse the JSON input / output at the same bean as I use it for the SOAP transfer?
And if it is possible: How? Or where should I start?