I am looking to use thrift for my web service calls. From what I've read so far from thrift documentation is that I'll have to write a thrift file containing my POJOs and services that I want to expose. This file then needs to be compiled using a thrift compiler to generate the Java classes. And then client and servers have to be written using these.
Is there a simpler way to achieve this (any annotation based or Spring framework integrations available)?
I wrote an article about Spring Boot and Thrift integration with detailed explanation how to combine them together : ) You can find my article here:
Java.DZone: Building Microservices with Spring Boot and Apache Thrift.
In general, you should create Servlet bean like this:
#Bean
public TProtocolFactory tProtocolFactory() {
return new TBinaryProtocol.Factory();
}
#Bean
public Servlet calculator(TProtocolFactory protocolFactory, CalculatorServiceHandler handler) {
return new TServlet(new TCalculatorService.Processor<CalculatorServiceHandler>(handler), protocolFactory);
}
where TCalculatorService is your Thrift service
Nope, there's no custom binding between spring and thrift. Once you've created your .thrift files, you will generate Java classes that will form the thrift communication layer.
For example, I've created a Java server that calls SQL over hibernate (this is one layer) and returns data over thrift (another layer). Unfortunately, there has to be some Java code that will process moving abstract data from one layer into another.
You can use this project for integration between SpringBoot and Apache Thrift https://github.com/aatarasoff/spring-thrift-starter.
As it is described in README you simply connect starter and create your handler like if you are using #RestController:
#ThriftHandler("/api")
public class TGreetingServiceHandler implements TGreetingService.Iface {
#Override
public String greet(TName name) throws TException {
// your logic
}
}
The following project seems to work on it
https://github.com/joshlong/spring-advanced-marhshallers-and-service-exporters/
Related
I am new to Java so apologies if this is a simple thing. I have built decorators in Python for authorizing RESTFul endpoints in Flask and have just built my first Java Webserver but am unable to figure out how to create a similar decorator in Java.
I want to do some pre-checks before running the method (i.e. is the user allowed to access this route). Ideally this would be a decorator like #authorize that, if authorized, will execute the method, but if unauthorized then it would through a 403 error instead.
#Path("/")
public final class HelloWorld {
#GET
#Path("/hello")
#authorize // How would I implement this?
public String sayHelloWorld() {
return "Hello World!";
}
}
EDIT: I am using Grizzly as the web Framework and I will be using an external Policy Management System (Apache Ranger) for managing authorization.
First of all: defining such custom annotations is exactly how you can approach such things in Java. The JAX-RS specification provides all the things you need for such kind of method binding.
The thing that is slightly more complicated: how to nicely do that for the framework that you are using.
With JAX-RS and Jersey for example, creating your own annotations is well documented. And Jersey might be a good starting point, as that is simply a straight forward way to get JAX-RS working.
So, first you start by learning how to use Jersey in general, for example from vogella. Next: you can start to add your custom annotations, see here for an example.
There is even an existing question about using custom annotations for access validation.
I'd like to design a portable application that is using Spring with JPA, but doesn't deal with Hibernate to make it possible to easily to port it to Google Apps Engine. In case of data persistence everything can be designed well, while I don't see any "spring data way" to instrument the classes for embedded internal Lucene usage (i.e without using of Solr as external webservice on the different URL). So, i'd like to have the local Lucene embedded into my Spring app (that will make possible to replace it with Luceneappsengune implementation), while having all boilerplate code to be handled on the way it is done with solr-spring-data. Hibernate Search annotations also doesn't work for me in this case. Any thoughts, how to do it?
So in examples I see that the almost the only bean configured is
#Bean
public SolrClient solrClient() {
return new HttpSolrClient("http://localhost:8983/solr");
}
Why don't we have the similar adapter for the internally used Lucene?
I am currently experimenting with the support for WebSockets added in Spring 4.0, as described in this guide. As demonstrated in the guide, methods annotated with #MessageMapping can be added to any Spring MVC controller, which may also contain #RequestMapping methods.
The spring-test module has support for writing integration tests for #RequestMapping methods (as described here) in a very simple and fluid way:
#Test
public void getAccount() throws Exception {
this.mockMvc.perform(get("/accounts/1").accept(MediaType.parseMediaType("application/json;charset=UTF-8")))
.andExpect(status().isOk())
.andExpect(content().contentType("application/json"))
.andExpect(jsonPath("$.name").value("Lee"));
}
Is there similar support for testing #MessageMapping methods using WebSockets? I have not found anything in any of the Spring modules, and none of the WebSocket guides contain any tests. If not, would I need to actually deploy the application and use a WebSocketConnectionManager to connect a test client? Or is there some API I can build on from spring-test?
This sample project contains such a small test client, but I would prefer to integrate this into the actual tests without requiring me to deploy the application and hardcode the deployed path in the tests.
There is nothing like Spring MVC Test for #MessageMapping methods yet. However, a similar approach to testing should be possible even without the fluent API. There is a ticket in JIRA (see https://jira.spring.io/browse/SPR-11266) to provide documentation so watch that ticket for more details in the very near future.
I have been doing some reading up on web services programming with Java, Eclipse, etc. and I found one particular example where the person created the web service and client by doing the following:
define the web service java class (interface + impl)
deploy the web service using Endpoint.publish
grab the wsdl from the url of the web service (eg, localhost://greeting?wsdl)
use wsimport to generate stubs
create a client class using generated stubs
Is there another way to generate the wsdl without having to publish the web service and download it? Perhaps a maven plugin to auto-generate wsdl and client stubs?
Update: Rather than creating a new question I am just going to piggyback on this one.
I have created my web service by defining an interface:
#WebService
public interface HelloWorldWs {
#WebMethod
public String sayHello(String name);
}
and an impl class:
#WebService(endpointInterface = "com.me.helloworldws.HelloWorldWs")
public class HelloWorldWsImpl implements HelloWorldWs {
#Override
#WebMethod
public String sayHello(String name) {
return "Hello World Ws, " + name;
}
}
When I run wsgen I get the following error:
The #javax.jws.WebMethod annotation cannot be used in with #javax.jws.WebService.endpointInterface element.
Eclipse seems to be okay with it.
Any idea why?
Note, I originally did not have the annotation but when I tried to call my webservice I got the following error:
com.me.helloworldws.HelloWorldWsImpl is not an interface
The JSR 224 says in 3.1 section:
An SEI is a Java interface that meets all of the following criteria:
Any of its methods MAY carry a javax.jws.WebMethod annotation (see 7.11.2).
javax.jws.WebMethod if used, MUST NOT have the exclude element set to true.
If the implementation class include the javax.jws.WebMethod, then you cant put #WebMethod(exclude=true) and that in not possible, according to specification.
Depends of custom version of Eclipse, shows a warning for this. e.g. Rational Application Developer for Websphere shows:
JSR-181, 3.1: WebMethod cannot be used with the endpointInterface
property of WebService
While programming/building a project (with some advanced IDE) normally you should be able to find it between auto-generated stuff - the IDE should generate it. Just check carefully.
First, I developed a Java EE application with a Adobe Flex frontend and I used BlazeDS.
So I had in the Java backend this structure:
IServiceX -> ServiceImplX -> IDaoX -> DaoImplX
So the frontend called a service like "addUser(User u)". The service implementation calls the Dao interface which implements a Dao to a database and the User is added. The return value is the new User ID.
Now I want to use instead of Adobe Flex HTML5 with AJAX. So I found this example project:
https://src.springframework.org/svn/spring-samples/mvc-ajax/
I have the problem how to integrate this into my existing architecture.
In this case I have a domain object called Account.java and a controller AccountController.java:
https://src.springframework.org/svn/spring-samples/mvc-ajax/trunk/src/main/java/org/springframework/samples/mvc/ajax/account/AccountController.java
Is it okay to create a Controller and this controller calls the already existing services?
Or should I update/change my architecture (but I do not know how...)?
Thank you in advance & Best Regards.
You are right - simply create a XController, annotated with #Controller and inject your existing service there. Take a look at Spring MVC docs. Also check the ajax simplifications article for spring mvc 3.0