I have a spring mvc application which i use as only a server. I have deployed some html and javascript files on my hosting account and sending post requests to my server only. Retrieve data from database and send back the data to the html or javascript pages. I don't use any servlets,jsp or jsf. Everywhere i look it says that i should use them however. Am i doing something wrong? It feels like bad practice but i don't know the right way to do it i guess. Any help would be appreciated.
As I already mentioned in my comments, you can look into creating a webservice. Since you are using Spring already, try this guide on Building a RESTful Web Service.
After creating the service, you can call it up like this, where you pass in input parameters to your rest endpoints,
http://localhost:8080/greeting?name=User
Now your service will respond back to your GET request and you'll be output a JSON/xml string which you can process later at the client side. The sample json response looks like this,
{"id":1,"content":"Hello, User!"}
Here's another example blog article on Spring Restful Web Service Example with JSON, Jackson and Client Program
Well, I think you should first expand your question a bit since one needs to assume a lot to give you an answer.
IMO, JSF is not great and there are other better options for UI and JSPs are dead. But I'm biased since I used last time JSF 1.X and then went with Spring MVC and Angular or Spring and Apache Wicket more recently.
I used for instance Spring MVC to implement an HTTP RPC API with JSON payloads to which an Angular frontend connected and it worked just fine. I also had the feeling that everyone is doing that nowadays:).
I also guess you are using at least one servlet to configure Spring DispatcherServlet. Right?
Related
So I have a Spring Boot app, with an Angular Front-end. My plan is to embed Tomcat in the Spring Boot app for routing, reverse proxies, etc. What I want to happen is the user types in the URL, the Spring Boot tomcat server figures out if there is a domain linked to that request and so on, however if the user types in the specified IP and Port it brings up the Admin Panel which is the Angular CLI application which already has injections, and Authentication and session control. I don't want people being routed to the Admin Panel unless that condition is filled. I would like the http request to go to the tomcat server and if the condition is met then it pulls up the Angular App. However, I am not quite sure how one would go about this, is it possible to call the Angular app from the Spring application?
Update:
After hearing some feedback I made a diagram to help explain what I am looking for:
Routing Diagram, so in this a client or user would type in "https://example.com/" in their browser, it would first go to angular being the frontend, angular realizes it does not have the example.com resource so it requests just example.com/ from the backend, which knows where the resource is and then injects the html file back into the front end. The only time in which a user would be greeted with the login page I have constructed is if they have typed in the primary IP address of the server, which I would probably end up injecting by some variable from the backend as the front end wouldn't know what the IP is... thats a different issue I would have to solve. Is there a better way to do this? Should I be using React.js instead of angular? so far my application structure is very similar to this github repository: https://github.com/liliumbosniacum/spring-security-angular I have modified it and fixed some of the code and am looking into better ways but this was my starting point. I really hope this clarifies, I am looking for if there is a better way to do this or if this is not how it should be done how should it be done? like just an explanation with a diagram on what a better way would be? and Is Angular the Right front end for me?
What I understand from your problem is that you want your Spring boot app to route requests to views created by your Angular program and need some authentication features.
For me, I'd rather divide the project into frontend and backend and use JWT tokens, but for your situations you can have a look on this official Spring documentation. It's a detailed post that integrates Spring boot and Angular, and it also deals with Spring security.
I have been contemplating on building a test jar for a community of developers in order to expose a preview of a next release of an API (having stubs returning expected response with exact format etc). We do have both REST and SOAP API. I guess it won't be any problem building the REST Service as the net is flooded with example. It was quite surprising there isn't much of concrete example of how to build SOAP service (JAXWS) with spring boot with embedded jetty.
What I expect to achieve is one single jar with both APIs. I am rather comfortable developing a java first services. I have seen a post in stackoverflow but it doesn't clearly outline steps to achieve that. I know it's possible because dropwizard guys have similar project.
I will be grateful if there is any resource with example on how to handle SOAP web services in spring boot.
Thanks you in advance
Spring already supports JAXWS through *JaxWsServiceExporter and SpringBeanAutowiringSupport (in spring-web). The *Exporter approach doesn't quite mesh with the REST stuff because it isn't in the embedded container. You'd end up with an app listening on 2 ports (one for XML and one for JSON). If either of those works then you have a solution. If you don't really care that much about SOAP and just want XML representations, you can use normal content negotiation features (e.g. #ResponseBody or #RestController for everything).
I am new to Spring MVC3 framework in java but I am familiar with java coding.
I want to write two application using this framework.
First application recieves requests through a SOAP web services and sends response in form of SOAP XML Object.
Second application have a simple servlet to recieve request and send responces.
I have studied Java MVC3 framework. It requires view to be called who are mapped against which controller will handles its request. But,
How I can do this using a webservice, so that when a specific method using SOAP services is called, I can forward that request to its relevant servlet and sends response back as a SOAP xml file.
How can I do this for my second application as well that recieves request through a servlet.
I hope all this make sense.
regards,
Aqif
If you want to stick with Spring, you can use Spring Web Services for application 1. Application 2 would be a more traditonal Spring Web app (uses a servlet, but framework does not require you to work in the servlet...instead you will work in more fine grained components).
If you dont want to stick with Spring for the web services, you can always use something like Apache Axis
The usual structure is as follows:
you have spring-mvc controllers to handle your browser requests
you have other components that handle the SOAP requests
both of the above invoke the same underlying services which serve them with the data that is to be sent to the user. The data is in java objects, which are later transformed to whatever is required
For the 2nd point you can pick some JAX-WS implementation, like CXF (it has nice spring support as well)
Spring Web Services specifically supports a Spring MVC-like model for responding to SOAP calls, as you describe.
the second one is Spring MVC directly. Heck, it sounds like - though I can't be sure without more information - that you're trying to build RESTful web services. There, too, Spring MVC is the right choice.
I am looking for a way to setting up a JSON proxy client in a spring framework way.
We are going to use Spring MVC on the server side. We don't like XML as they are overkill and heavy. JSON seem to be a lightweight and effective message container for us.
However, I've search around and read http://static.springsource.org/spring/docs/3.0.x/spring-framework-reference/html/remoting.html many times and I don't find any hits to put my spring client into a JSON client.
They provided RMI, Http, JAX-WS, SOAP and others. But nothing related to as a client of MVC (which I guess it could be common as we don't want to write it twice)
RestTemplate looks good but I am wonder is it the suggested way to do in spring 3.0.
The RestTemplate is indeed the preferred way of accessing rest services.
I've been in the same position as you - looking through the Spring docs for how to implement a simple JSON client API. I ended up implementing it myself, as I only needed it for a few RPC-like calls to another webapp. IIRC Jax-RS has this capability so you might want to invest in implementing it - for my needs it seemed overkill.
All there is to it:
write a simple method to perform the HTTP GET to the JSON web service and return a String (I used Apache HttpClient)
pass the String to Jackson to deserialize into a Java object (see mapper.readValue())
This assumes you already know what kind of object you expect to get back from a given JSON web service.
As an aside, the other thing I needed from my Spring MVC JSON web service was the ability to do JSONP (cross site callback) for consumption in the browser with JQuery (note: JSONP is not secure so use at your own risk). The automagic Spring JSON webservices that Bozho outlined does not provide an option for JSONP. The easiest way to provide JSONP is to implement a simple servet filter.
We are seeing Spring in school right now but we don't have the time to wait till the end of the semester to start developing an application. We continue using an app we made last year, and are writing the service layer right now.
The problem is our "client" wants to have a desktop client and a webpart, which used the same dtatabase. This would be no problem if we hook up a server that can handle RMI. So basically we want to be able to retrieve/send data to the server that runs our service layer, and use the objects on the client side as well.
I have no idea where to start digging in Spring to figure out how to do this, so some help would be appreciated.
PS: At this point I do not need MVC yet. MVC is handled from within the desktop app where we have views and controllers.The model is the same from the one on the service layer. How do we use the same model without copying it?
Check out spring remoting: http://static.springsource.org/spring/docs/3.0.x/spring-framework-reference/html/remoting.html
It's easy to expose your spring beans remotely, using a variety of protocols.
You might want to take a look at the REST paradigms. With this in mind you could have a web server running your server part of the application and communicating with clients through the HTTP protocol. A simple client could be a webpage in the browser which gets the corresponding HTML pages from the server, or a Swing client which communicates over JSON with the server.
The server can implement different methods for JSON or HTML communication and the server can decide what implementation to use by looking at the Accept Header of the Request objects sent to it, that's what they call Content Negotiation
JSR-311 is implemented as Project Jersey which is a framework for RESTful webservices. You might want to take a look at that.
hope that helped