Tuesday, June 26, 2018

Saving data to file using SpringBoot postman

pom.xml:
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>

<groupId>com.boot</groupId>
<artifactId>raja-boot</artifactId>
<version>1.0.1-Release</version>
<packaging>war</packaging>

<name>spring-boot</name>
<url>http://maven.apache.org</url>

<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<java.version>1.8</java.version>
</properties>

<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>1.5.1.RELEASE</version>
</parent>

<dependencies>

<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<!-- <dependency> -->
<!-- <groupId>org.springframework.boot</groupId> -->
<!-- <artifactId>spring-boot-starter-data-jpa</artifactId> -->
<!-- </dependency> -->

<!-- https://mvnrepository.com/artifact/org.springframework.boot/spring-boot-starter-json -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-json</artifactId>
<version>2.0.3.RELEASE</version>
</dependency>

</dependencies>
<build>
<plugins>
<!-- Package as an executable jar/war -->
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>

2. SpringBootApp.java
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

@SpringBootApplication(scanBasePackages={"com.boot"})
public class SpringBootApp {

public static void main(String[] args) {
SpringApplication.run(SpringBootApp.class, args);
System.out.println("spring boot application is running");
}
}

3. RestController.java
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStreamWriter;

import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import com.boot.entity.Student;

@org.springframework.web.bind.annotation.RestController
@RequestMapping("/api")
public class RestController {
@RequestMapping(value = "/ping/", method = RequestMethod.GET)
public String ping() {
  return "successsss !!";
}
@RequestMapping(value = "/create/", method = RequestMethod.POST)
@ResponseBody 
public String create(@RequestBody Student data) throws IOException {
File file = new File("/home/rajar/demo.txt");
FileOutputStream stream = new FileOutputStream(file);
OutputStreamWriter obj = new OutputStreamWriter(stream);
BufferedWriter br  =new BufferedWriter(obj);
br.write(data.getName().toString());
br.append("\n");
br.write(data.getId());
br.close();
  return "<h2>rajjjjjjaaaaaa !!</h2>";
}
}

4. Student.java

import java.io.Serializable;

public class Student implements Serializable {

private String id;
private String name;

public String getName() {
return name;
}

public void setName(String name) {
this.name = name;
}

public String getId() {
return id;
}

public void setId(String id) {
this.id = id;
}
}

2 comments:

How to run standalone mock server on local laptop

 Please download the standalone wiremock server from Direct download section at the bottom of the page.  Download and installation Feel fre...