Servicestack migration to core, fallback router and HandlerFactoryPath - razor

Im migrating my code to a core application.
So far so good. i got it all running, but there is one problem.
I had a ui (with razor) and using the CatchAllHandlers. And for the api i used HandlerFactoryPath to prefix the urls.
Now i have 2 problems:
It seems CatchAllHandlers isn't used?
Route is only processed if route starts with HandlerFactoryPath?
The second issue is fixable but how would i go around the first one?
Do i make my own middle-ware or does servicestack support other ways of doing this?

CatchAllHandlers are executed in .NET Core, it's also what ServiceStack's new MVC RazorFormat uses to process Content Pages.
Specifying a HandlerFactoryPath, e.g:
SetConfig(new HostConfig {
HandlerFactoryPath = "api"
});
Tells ServiceStack that you only want to listen to requests from the /api path info. All other requests are passed through ServiceStack who calls the next module in .NET Core pipeline as per .NET Core's convention when it's not configured to handle a route.
Not sure if it's relevant to your solution but in ServiceStack .NET Core you can register a ServiceStack Handler in .NET Core module pipeline so you could for instance return a /default.cshtml Content Razor Page for each request that's not handled by ServiceStack, by registering it after ServiceStack:
app.UseServiceStack(new AppHost());
app.Use(new RazorHandler("/default"));

Related

How does restangular talk to MySQL database

I am total JS newbie working on a project build in Grail 2.4.4, a web-app. It's a working app, build by a developer whom is not available anymore.
To get it to work locally I had to upgrade it to Grails 3.2.0. I got it almost working in Netbeans. But I got stuck at getting the data from the MySQL database.
The Chrome inspector says:
angular.min.js GET http://localhost:8080/<app>/currency/allCurrencies 404 ()
The controllers are written in Restangular which call the above URL.
What am I missing?
Firstly, Restangular is an Angular library which simplifies and standardizes making calls to a REST backend (which in your case is a Grails app). So, Restangular does not directly retrieve data from your a database, it invokes a web service which (in some cases) may retrieve data from a database.
In your case, Restangular is attempting to retrieve data from the endpoint http://localhost:8080/<app>/currency/allCurrencies but you are getting a 404 response, indicating that there is no endpoint mapped to this URL.
HTTP REST helps you connect to the API easily. Restangular can handle that by sending standard methods [Get, Post, Delete, Put] to the api like what you see.
This mean StudentController > Get()
localhost:2045/api/student
This mean StudentController > Get(Guid id)
localhost:2045/api/student/8ae37cfa-905b-4c71-ad03-bf416d93bdf8
This mean StudentController > POST(Guid id) ... if you send Post method to the API, it will detect it, this work also on put method
localhost:2045/api/student
use this module to get easily rest api.
Http-Rest-Service

Mock a web server for ajax in IntelliJ environment

IntelliJ can deploy HTML/JS code at localhost:63342 on the fly. I can see how the frontend behaves immediately.
Now I have a web application.
http://localhost:8080/frontend
http://localhost:8080/backend
The frontend will fetch data from its backend. Now I cannot start a real backend at localhost:63342. How can mock some ajax response at localhost:63342. Is there any magic plugin of IntelliJ can help?
My current solution is put all ajax call in an Angular service module and replace it with a mocked service module. I believe there must be some better solutions.
If you want to mock up some endpoints for you to test with I would suggest you use Mockjax. This will allow you to do exactly what you want and you won't have to run two separate apps to do so. I'm not familiar with IntelliJ, but Mockjax only has a dependency on jQuery and you should be able to easily set it up with any project that at least uses jQuery.

Is there a way to host Razor pages in console application using ServiceTask?

I'm trying to make a console application to expose JSON services.
In addition I'd like to host html and js pages to use them.
I put the *.md (even *.htm) files into Views folder, but I can't reach them.
If I add a route ".Add("/Test")" (where MyMarkdownView : MarkdownViewBase), i even get a "KeyNotFoundException The given key was not present in the dictionary." exception.
Is it generally possible, or I should make my own service (similar to https://github.com/jimschubert/blogs/blob/master/2012-07-15/RazorExample/Main.cs) to host them?
You might be interested in the Razor Rockstars Console Application that uses embedded resource MVC Razor views that work with (or without) existing ServiceStack web services.
i.e. the example project shows the 3 different ways to host MVC Razor pages in ServiceStack:
Service/Controller and Model - Using the Response DTO from the Web Service as a view model
No Service/Controller - Directly using the Request DTO as the view model
No Service/Controller or Model - Directly by using a dynamic view model

Tomcat not using JAXB for JSON marshalling correctly?

I'm working on a RESTful Web-service using Jersey v1.9.1. Some methods return JSON. When I want to Debug my application I start within a grizzly server, otherwise for production I build a war file and place it in a TomCat v7 installation. My projects are all Maven2 projects.
Now, I noticed that for a method that returns List<CustomObj>, where CustomObj has appropriate JAXB annotations, i.e. #XmlRootElement(name="CustomObj"), and getter/setters for all relevant members:
Using grizzly, I get something like {"CustomObj":[{<fields-of-customObj>},{<fields-of-customObj>},{<fields-of-customObj>}]} (when the list has 3 elements). Parsing this with GSON works fine.
Using TomCat, however, I get this: [{<fields-of-customObj>},{<fields-of-customObj>},{<fields-of-customObj>}] -> so as you can see, the "root" is missing somehow
I have the impression that the jersey-json module (which I included into my Maven2 dependencies) are not used at all in TomCat, even though they should be used (they are used in Grizzly for sure). Also, creating my own #Provider for a ContextResolver<JAXBContext> as described here only works in grizzly, in TomCat the getContext() method will never be called.
Is there anything I need to consider with TomCat?
Cheers!

rpc lib for blackberry / j2me ( json / xml / * )

I'm trying to setup an rpc server so that a blackberry mobile app can make calls to it. Thought of trying out json first.
I've setup a working server side impl using http://jsonrpcphp.org/ .
Couldn't find any direct libs for blackberry/j2me. android-json-rpc looked interesting, but the blackberry SDK complains
"The type java.net.URI cannot be resolved. It is indirectly referenced from required .class files" at this line
HttpPost request = new HttpPost(serviceUri);
I'm using v4.1 of apache http core and client to make android-json-rpc work.
Looks like the URI class isn't bundled with the j2me/blackberry standard lib.
Is there a quick and dirty way to get rpc working on blackberry ? I don't mind xml or any thing else for the encoding, http is the transport I'm interested in.
BlackBerry 6 has built-in the JSON parser and some better APIs for making HTTP requests. Prior to that, though, you have to compile the JSON parser into your app and use the Java ME HttpConnection class to make and get the HTTP requests. So it depends what version you're targeting.
If you are targeting to < BB OS 6.0 devices.
You can also use org.json.me lib in your app.
Here is the sample named Implementing JSON in your application. It's very simple to use.
Good luck