Recently I've started developing with Java, and was introduced to the Dropwizard framework. But what's got me stumped here, is that I'm not getting any resources online which would explain how to set it up a Jetty server with my Dropwizard application (I previously made use of Apache Tomcat, but was told that Jetty is a much better alternative). Also, what is use of Embedded-jetty in it?
(I realize that the nature of the question is rather amateurish, but I couldn't come across any online resource that would explain this succinctly :( ...)
The application part:
import io.dropwizard.Application;
public class App extends Application<AppConfiguration> {
public static void main(String[] args) throws Exception {
new App().run(args);
}
#Override
public void run(AppConfiguration configuration, Environment environment) {
final AppResource resource = new AppResource();
environment.jersey().register(resource);
}
The resource with a dummy API to get version:
public class AppResource {
#GET
#UnitOfWork(readOnly = true)
#Path("/version")
#ApiOperation(
value = "Retrieve the version")
#Timed
public Version getVersion() {
return new Version();
}
}
Related
I am trying to upgrade the Jetty from 8 to 9.3. As the default value of _asyncSupported become false so it will show the following error.
java.lang.IllegalStateException: !asyncSupported: stackDumperFilter
at org.eclipse.jetty.server.Request.startAsync(Request.java:2248) at
org.eclipse.jetty.continuation.Servlet3Continuation.suspend(Servlet3Continuation.java:188)
at
org.eclipse.jetty.servlets.ProxyServlet.service(ProxyServlet.java:659)
The servlet is loaded in the code through Google guice's ServletModule in the following way.
public class ProxyModule extends ServletModule {
#Override
protected void configureServlets() {
serve("/someurl/*").with(ProxyServlet.class);
}
}
#Singleton
public static class ProxyServlet extends SuperHttpProxyServlet {
#Inject
public ProxyServlet(#Named("something.proxy") Transparent proxy) {
super(proxy);
}
}
After Jetty 9 upgrade, it will take the default value of _asyncSupported which become false. So it will give exception for the following reason in the jetty library file (Package : org.eclipse.jetty.server).
public AsyncContext startAsync() throws IllegalStateException
{
if (!_asyncSupported)
throw new IllegalStateException("!asyncSupported");
_async.startAsync();
return _async;
}
So how do I make the ProxyServlet asyncSupported (true) when it is called by Google Guice's ServletModule ?
I have tried with annotation but it won't work.
#WebServlet(urlPatterns={"/someurl/*"}, asyncSupported=true, loadOnStartup = 1)
#Singleton
public static class ProxyServlet extends SuperHttpProxyServlet {
#Inject
public ProxyServlet(#Named("something.proxy") Transparent proxy) {
super(proxy);
}
}
But it got failed with the same error.
java.lang.IllegalStateException: !asyncSupported: stackDumperFilter at org.eclipse.jetty.server.Request.startAsync(Request.java:2248) at
Set your stackDumpFilter to be asyncSupported=true
The rule of thumb is, if anything in your filter chain (all filters + servlet) uses async, then all of those filters and servlet must be set to asyncSupported=true.
I'm starting in the play framework with Java and downloaded the project from the official website: https://github.com/playframework/play-java-rest-api-example/tree/2.5.x
I joined the eclipse as it says at: https://www.playframework.com/documentation/2.5.x/IDE
And run the command: sbt run
It worked. However, if I send a post to the given address, I get the message "Unauthorized".
Does anyone know if I have to do anything else?
Thank you very much in advance.
This is workin for me .
#Singleton
public class Filters implements HttpFilters {
private final Environment env;
private final EssentialFilter exampleFilter;
#Inject
public Filters(Environment env, ExampleFilter exampleFilter) {
this.env = env;
this.exampleFilter = exampleFilter;
}
#Override
public EssentialFilter[] filters() {
if (env.mode().equals(Mode.DEV)) {
return new EssentialFilter[] { exampleFilter };
} else {
return new EssentialFilter[] {};
}
}
}
I found out. I have not yet studied Filters, but I left it that way and it worked:
another way is to configure it in the application.conf file, just add the lines
play.filters.disabled +="play.filters.cors.CORSFilter"
I am using Spring Data Solr in my project. In some cases generated queries to Solr are too big (e.g.15Kb+) and cause Solr exceptions. This solution: http://codingtricks.fidibuy.com/participant/join/54fce329b760506d5d9e7db3/Spring-Data-Solr-cannot-handle-long-queries
still fails for some queries.
Since directly sending those queries to Solr via POST works fine, I chose to work in this direction. I failed to find in Spring Data Solr any way to configure the preferred method (GET/POST) for queries. Therefore, I came to the following solution: I extended SolrServer
public class CustomSolrServer extends HttpSolrServer {
public CustomSolrServer(String home, String core) {
super(home);
setCore(core);
}
#Override
public QueryResponse query(SolrParams params) throws SolrServerException {
METHOD method = METHOD.GET;
if (isBigQuery(params)) {
method = METHOD.POST;
}
return new QueryRequest( params, method ).process( this );
}
}
(some details skipped, setCore() and isBigQuery() are trivial and skipped as well)
and use it as SolrServer bean in SolrConfiguration.class:
#Configuration
#EnableSolrRepositories(basePackages = { "com.vvy.repository.solr" }, multicoreSupport=false)
#Import(value = SolrAutoConfiguration.class)
#EnableConfigurationProperties(SolrProperties.class)
public class SolrConfiguration {
#Autowired
private SolrProperties solrProperties;
#Value("${spring.data.solr.core}")
private String solrCore;
#Bean
public SolrServer solrServer() {
return new CustomSolrServer(solrProperties.getHost(),solrCore) ;
}
}
This works OK, but has a couple of drawbacks: I had to set multiCoreSupport to false. This was done because when Spring Data Solr implements repositories from the interfaces, with multiCoreSupport on it uses MultiCoreSolrServerFactory and tries to store a server per core, which is done by cloning them to the holding map. Naturally, it crashes on a customized SolrServer, because SolrServerUtils doesn't know how to clone() it. Also, I have to set core manually instead of enjoying Spring Data extracting it from #SolrDocument annotation's parameter on the entity class.
Here are the questions
1) the main and general question: is there any reasonable way to solve the problem of too long queries in Spring Data Solr (or, more specifically, to use POST instead of GET)?
2) a minor one: is there a reasonable way to customize SolrServer in Spring Data Solr and yet maintain multiCoreSupport?
Answer for Q1:Yes, u can using POST instead of GET.
Answer for Q2:Yes, u already have done a half.Except following:
1)u have to rename 'CustomSolrServer' to 'HttpSolrServer',u can check method
org.springframework.data.solr.server.support.SolrServerUtils#clone(T, java.lang.String)
for reason.
2)u don't have to specify concrete core name.U can specify core name using annotation
org.springframework.data.solr.core.mapping.SolrDocument
on corresponding solr model.
3)set multicoreSupport = true
According to your sample of classes, they should look like as following:
package com.x.x.config;
import org.apache.solr.client.solrj.SolrRequest;
import org.apache.solr.client.solrj.SolrServerException;
import org.apache.solr.client.solrj.request.QueryRequest;
import org.apache.solr.client.solrj.response.QueryResponse;
import org.apache.solr.common.params.SolrParams;
public class HttpSolrServer extends org.apache.solr.client.solrj.impl.HttpSolrServer {
public HttpSolrServer(String host) {
super(host);
}
#Override
public QueryResponse query(SolrParams params) throws SolrServerException {
SolrRequest.METHOD method = SolrRequest.METHOD.POST;
return new QueryRequest(params, method).process(this);
}
}
#Configuration
#EnableSolrRepositories(basePackages = { "com.vvy.repository.solr" }, multicoreSupport=true)
#Import(value = SolrAutoConfiguration.class)
#EnableConfigurationProperties(SolrProperties.class)
public class SolrConfiguration {
#Autowired
private SolrProperties solrProperties;
#Bean
public SolrServer solrServer() {
return new com.x.x.config.HttpSolrServer(solrProperties.getHost()) ;
}
}
ps: Latest spring-data-solr 3.x.x already support custom query request method,see post issue
I'm trying to Spring Data Cassandra 1.1.2 to work with Cassandra 2.1.2 and Spring 4.0.2. Java is 1.7
Everything works fine - as far as I have tested - except for the tables/columnfamily not being created automatically. I have tried to enable it with:
session.setSchemaAction(SchemaAction.RECREATE_DROP_UNUSED);
but it doesn't even try to create the tables. At least, with ALL logging enabled, I can't see anything.
I have tried to find some sample code but nothing worked. Any pointers or sample code would be very welcome.
make your cassandra configuration class extend AbstractCassandraConfiguration
and override
#Override
#Bean
public CassandraSessionFactoryBean session() throws Exception {
CassandraSessionFactoryBean bean = new CassandraSessionFactoryBean();
bean.setCluster(cluster().getObject());
bean.setConverter(cassandraConverter());
bean.setSchemaAction(getSchemaAction());
bean.setKeyspaceName(getKeyspaceName());
return bean;
}
#Override
public SchemaAction getSchemaAction() {
return SchemaAction.RECREATE_DROP_UNUSED;
}
#Override
public String[] getEntityBasePackages() {
return new String[] {"com.example"}; //com.example package contains the bean with #table annotation
}
I am still working on a jax-rs server, and I faced some new problems recently. I do not understand where I define the name of my webserver. I searched everything in my workspace, but couldn't find anything.
Let's roll out the problem a bit further:
I always reach my server's #GET method via http://XXXXXX.XXXXX.XXX-XXXXXXX.de/android/
This is the structure of my server class:
#Path("/users")
public class UserResource {
Connection dbconn = null;
public UserResource() {
userIds = new ArrayList<Integer>();
userIds.add(1);
userIds.add(2);
userIds.add(3);
}
#GET
#Path("/login/{id}")
#Consumes("application/xml")
public StreamingOutput getTests(#PathParam("id") int id, InputStream is) {
return new StreamingOutput() {
public void write(OutputStream outputStream) throws IOException,
WebApplicationException {
getTests(outputStream);
}
};
}
As you see, the path of my class is "/users", and the path of the #GET method is "/login/1" (for example id = 1). Now I tried to call the method via
http://XXXXXX.XXXXX.XXX-XXXXXXX.de/android/users/login/1
But this does not work. I get an error (unknown source). And my error.log says that it couldn't find the resource at
http://XXXXXX.XXXXX.XXX-XXXXXXX.de/users/users/login/1
My 1st question: Where does the double "/users" come from? I have no idea. When I leave away the "/users" in my request url, there will be only 1 "/users" in the error.log, but still the resource is not found.
And there is another thing I did not find out yet: How do I change the name of my service? Atm, it's "android", but how could I change this? I searched my whole workspace, found "android" in my pom.xml, but when i change it to, let's say "testandroid", upload everything, build and run the server, the name is still android. No idea why this is the case.
Thx for your help guys!
EDIT:
This is my "main" class:
package com.restfully.services;
import javax.ws.rs.core.Application;
import java.util.HashSet;
import java.util.Set;
public class ServerApplication extends Application {
private Set<Object> singletons = new HashSet<Object>();
private Set<Class<?>> empty = new HashSet<Class<?>>();
public ServerApplication() {
singletons.add(new UserResource());
}
#Override
public Set<Class<?>> getClasses() {
return empty;
}
#Override
public Set<Object> getSingletons() {
return singletons;
}
}
I am using Eclipse and Maven. The application runs on a jetty-server. If you could use any further information, let me know.
You can look in the following places
Pom.xml file for context root the following entry;
<configuration>
<context>yourWarName</context>
</configuration>
Using Netbeans check Run Category context path under project properties.
Context Path: /yourWarName
Have a look in your web.xml as well.
When using jax-rs you normally define a config class;
#ApplicationPath("resources")
public class RestConfig extends Application{
}
From there you define your other paths;
#Stateless
#Path("/login")
public class LoginResource
public Response login(Credentials credentials) {
Credentials result = this.loginService.login(credentials);
return Response.status(Response.Status.OK).entity(result).build();
}
The path to the following is: http://domain.com/MyApp/resources/login
where MyApp is the context root.
It might be that there is a path specified in config or root with the name users that you are getting the double users.