- For simplicity, our example application only sends JSON back and forth, but the application should be architectured in such a way that you can easily change the format of the data, to tailor for different clients or user preferences.
Sunday, June 24, 2018
rest api
Saturday, June 23, 2018
mockito
- https://medium.com/@gustavo.ponce.ch/spring-boot-restful-junit-mockito-hamcrest-eclemma-5add7f725d4e
- can we use same mock in two different test methods? what would be its implications ie. pros and cons?
- what layer is best suitable for being tested through mockito? controller or service? or both?
- https://dzone.com/articles/spring-boot-unit-testing-and-mocking-with-mockito
- @Mock vs @InjectMocks?
error-selection-does-not-contain-a-main-type
- https://stackoverflow.com/questions/31250359/git-repository-how-to-recover-deleted-files
- check the folder structure
- do you have src/main/java structure and inside it do you have Main class?
github cloning a repository from a specific commit
- clone a repository as usual ie
- git clone repository url
- user name and password if it is private
- git reset --hard a0f64dcd987669aa15d59a39a51f583b3cqd05k2
- in the above command the value after hard flag is the SHA of commit at that particular instant of repository
- import as maven project in sts
github recovering a deleted file from previous commit
- click on total number of commits
- here you can view all the commits
- now go to a commit where a file existed and click on < > symbol and if you find the file just download it
Friday, June 8, 2018
redirecting request on port 80 to 8080 on ubuntu
sudo iptables -A PREROUTING -t nat -p tcp --dport 80 -j REDIRECT --to-ports 8080
the above command will redirect the requests coming on port 80 to port 8080
Thursday, June 7, 2018
spring boot file upload to amazon s3 on ubuntu
- https://medium.com/oril/uploading-files-to-aws-s3-bucket-using-spring-boot-483fcb6f8646
- first sign up or login to your amazon account and create bucket in s3. then create a user and give permission ie programmatic access
- @Configuration
public class S3Config {
@Value("${userid.aws.access_key_id}")
private String awsId;
@Value("${userid.aws.secret_access_key}")
private String awsKey;
@Value("${userid.s3.region}")
private String region;
@Bean
public AmazonS3 xyz(){
System.out.println("Inside s3Client of S3Config ");
BasicAWSCredentials awsCreds = new BasicAWSCredentials(awsId, awsKey);
AmazonS3 s3Client = AmazonS3ClientBuilder.standard()
.withRegion(Regions.fromName(region))
.withCredentials(new AWSStaticCredentialsProvider(awsCreds))
.build();
return s3Client;
}
} - @Controller
public class AController {
@Autowired
private S3Service s3service;
@PostMapping("/uploadFile")
public String uploadFile(@RequestPart(value = "xyz") MultipartFile multipartFile,Model map,HttpServletRequest request) {
System.out.println("Inside upload file of bucket controller ");
try
{
String filePath = request.getServletContext().getRealPath("/");
System.out.println("filePath is "+filePath);
File f1=new File(filePath+"/"+multipartFile.getOriginalFilename());
multipartFile.transferTo(f1);
System.out.println("f1 is "+f1);
String fileName=f1.getName();
String fileUrl=s3service.uploadFile(fileName, f1);
/**
* we should be sure that the file has indeed been uploaded
*/
map.addAttribute("fileUrl", fileUrl);
f1.delete();
/* for rest request
return fileUrl;
*/
return "uploadSuccess";
}
catch(IOException e)
{
e.printStackTrace();
return e.toString();
}
} - @Service
public class S3ServicesImpl implements S3Service {
private Logger logger = LoggerFactory.getLogger(S3ServicesImpl.class);
/* (non-Javadoc)
* @see com.epilen.androidPatient.service.aws.s3.S3Service#downloadFile(java.lang.String)
*/
@Autowired
private AmazonS3 s3client;
@Value("${userid.s3.bucket}")
private String bucketName;
@Value("${s3.endpointUrl}")
private String endpointUrl;
/**
* convert multipart to file
* @param file
* @return
* @throws IOException
*/
@Override
public void downloadFile(String keyName) {
try {
System.out.println("Downloading an object");
S3Object s3object = s3client.getObject(new GetObjectRequest(bucketName, keyName));
System.out.println("Content-Type: " + s3object.getObjectMetadata().getContentType());
Utility.displayText(s3object.getObjectContent());
logger.info("===================== Import File - Done! =====================");
} catch (AmazonServiceException ase) {
logger.info("Caught an AmazonServiceException from GET requests, rejected reasons:");
logger.info("Error Message: " + ase.getMessage());
logger.info("HTTP Status Code: " + ase.getStatusCode());
logger.info("AWS Error Code: " + ase.getErrorCode());
logger.info("Error Type: " + ase.getErrorType());
logger.info("Request ID: " + ase.getRequestId());
} catch (AmazonClientException ace) {
logger.info("Caught an AmazonClientException: ");
logger.info("Error Message: " + ace.getMessage());
} catch (IOException ioe) {
logger.info("IOE Error Message: " + ioe.getMessage());
}
}
/**
* this method will return the fileurl
*/
@Override
public String uploadFile(String fileName, File file) {
System.out.println("Inside upload file of S3ServiceImpl");
try {
s3client.putObject(new PutObjectRequest(bucketName, fileName, file));
logger.info("===================== Upload File - Done! =====================");
String fileUrl = endpointUrl + "/" + bucketName + "/" + fileName;
return fileUrl;
} catch (AmazonServiceException ase) {
logger.info("Caught an AmazonServiceException from PUT requests, rejected reasons:");
logger.info("Error Message: " + ase.getMessage());
logger.info("HTTP Status Code: " + ase.getStatusCode());
logger.info("AWS Error Code: " + ase.getErrorCode());
logger.info("Error Type: " + ase.getErrorType());
logger.info("Request ID: " + ase.getRequestId());
return ase.toString();
} catch (AmazonClientException ace) {
logger.info("Caught an AmazonClientException: ");
logger.info("Error Message: " + ace.getMessage());
return ace.toString();
}
}
} - public interface S3Service {
public String uploadFile(String fileName, File file);
}
pom.xml entry->.
<dependencyManagement>
<dependencies>
<dependency>
<groupId>com.amazonaws</groupId>
<artifactId>aws-java-sdk-bom</artifactId>
<version>1.11.327</version>
<type>pom</type>
<scope>import</scope>
</dependency>
</dependencies>
</dependencyManagement>
<dependencies>
<dependency>
<groupId>com.amazonaws</groupId>
<artifactId>aws-java-sdk-s3</artifactId>
</dependency>
<dependency>
<groupId>org.apache.httpcomponents</groupId>
<artifactId>httpclient</artifactId>
<version>4.5.2</version>
</dependency>
the above reduces the jar size from 90 mb to 35 mb and downloads only s3 dependencies- application.properties entries userid.aws.access_key_id= userid.aws.secret_access_key= userid.s3.bucket=
userid.s3.region=
s3.endpointUrl=
Subscribe to:
Posts (Atom)