नीचे दिए गए चरणों का पालन करें।
पोम फ़ाइल में निर्भरता जोड़े
<dependencies>
<dependency>
<groupId>com.yammer.dropwizard</groupId>
<artifactId>dropwizard-core</artifactId>
<version>0.6.2</version>
</dependency>
बनाएं विन्यास वर्ग
import com.yammer.dropwizard.config.Configuration;
public class BlogConfiguration extends Configuration{
}
बनाएं सेवा वर्ग
import com.yammer.dropwizard.Service;
import com.yammer.dropwizard.config.Bootstrap;
import com.yammer.dropwizard.config.Environment;
public class BlogService extends Service<BlogConfiguration> {
public static void main(String[] args) throws Exception {
new BlogService().run(new String[] { "server",
"C:\\LocalEnv\\Workspace\\dropwizarddemo\\configuration.yml" });
}
@Override
public void initialize(Bootstrap<BlogConfiguration> bootstrap) {
bootstrap.setName("blog");
}
@Override
public void run(BlogConfiguration configuration,
Environment environment) throws Exception {
environment.addResource(new IndexResource());
}
}
नोट: वर्तमान निर्देशिका
# HTTP-specific options.
http:
# The port on which the HTTP server listens for service requests.
port: 8079
# The port on which the HTTP server listens for administrative
# requests.
adminPort: 8179
# Maximum number of threads.
maxThreads: 100
# Minimum number of thread to keep alive.
minThreads: 10
4. लिखें सूचकांक संसाधनों में configuration.yml फ़ाइल नीचे डाल दिया।
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import javax.ws.rs.GET;
import javax.ws.rs.Path;
import javax.ws.rs.Produces;
import javax.ws.rs.core.MediaType;
import com.yammer.metrics.annotation.Timed;
@Path("/")
public class IndexResource {
@GET
@Produces(value = MediaType.APPLICATION_JSON)
@Timed
public List<Blog> index() {
return Arrays.asList(new Blog("for Java Developers",
"http://stackoverflow.com/questions/13345693/looking-for-a-dropwizard-
example”));
}
@Path("/service")
@GET
@Produces(value = MediaType.APPLICATION_JSON)
@Timed
public List<Users> users() {
List<Users> list = new ArrayList<Users>();
list.add(new Users(25,"Sambhu","SA"));
list.add(new Users(35,"Amit","VP"));
list.add(new Users(45,"Sanket","AVP"));
return list;
}
}
- ब्लॉग के लिए
public class Users {
Integer id;
String name;
String designation;
public Users(Integer id, String name, String desination){
this.id=id;
this.name=name;
this.designation=desination;
}
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getDesignation() {
return designation;
}
public void setDesignation(String designation) {
this.designation = designation;
}
@Override
public String toString() {
return "Users [id=" + id + ", name=" + name + ", designation="
+ designation + "]";
}
भागो BlogService तरह POJO और उपयोगकर्ता
लिखें जो जेट्टी सर्वर शुरू कर देंगे और पोर्ट के साथ स्थानीय होस्ट मारा http://localhost:8079/ जैसे
क्या * यदि आप * इस सवाल का जवाब अर्जित करने के लिए कोशिश? –
मैं खुद को उत्तर देने की प्रक्रिया में हूं क्योंकि मुझे लगता है कि इसकी आवश्यकता है। अगर कोई और जवाब नहीं देता है तो मैं एक प्रस्तुत करूंगा। –
क्या आप इसे हल करने में सक्षम थे? –