Getting a value in application.properties file using javascript in spring boot - javascript

In my index.html page, a variable in the script is hard coded. I want to get it from application.properties file but have no idea how to. It would helpful if anyone could provide me a solution.

I have attached the example. Hope to help.
Application
#SpringBootApplication
public class Application {
public static void main(String... args) {
SpringApplication.run(Application.class);
}
}
PropertiesController
#RestController
public class PropertiesController {
#Autowired
private UIProperty uiProperty;
#RequestMapping("properties")
public UIProperty getProperties() {
return uiProperty;
}
}
UIProperty
#Component
#ConfigurationProperties(prefix = "ui.label")
public class UIProperty {
private String user;
private String password;
public void setUser(String user) {
this.user = user;
}
public String getUser() {
return user;
}
public void setPassword(String password) {
this.password = password;
}
public String getPassword() {
return password;
}
}
application.properties
ui.label.user=user
ui.label.password=password
database.user=
database.password=

I'd create a RestController to expose ConfigurationProperties. But be sure to properly secure it as well as limit in its scope not to disclose confidential data like db access credentials.

Related

Send List from Angular to Spring

Iam trying to send an Object from angular to my spring to add a new product. The product has a list of prices and sizes. This is how it looks like in backend:
#Setter
#Getter
public class ProductDto {
private Long id;
private Long secondId;
private String name;
private String description;
private MultipartFile img;
private byte[] returnedImg;
private Long categoryId;
private String categoryName;
private Long restaurantId;
private Long userId;
private List<SizePriceDto> sizePrices;
}
Controller class:
#PostMapping("product/{categoryId}/{restaurantId}/{userId}")
public GeneralResponse addProduct(#PathVariable Long categoryId, #PathVariable Long restaurantId, #PathVariable Long userId, #ModelAttribute ProductDto productDto) {
GeneralResponse response = new GeneralResponse();
try {
return restaurantService.addProduct(categoryId, restaurantId, userId, productDto);
} catch (Exception ex) {
response.setStatus(HttpStatus.BAD_REQUEST);
response.setMessage("Sorry Something Wrong Happened.");
return response;
}
}
Frontend:
this.sizes.controls.forEach((sizeGroup, index) => {
sizesAndPrices.push({
size: sizeGroup.get('size').value,
price: sizeGroup.get('price').value
});
});
formData.append('sizePrices', '[{"size":"30","price":343},{"size":"frefgv","price":343}]');
And this is the error that i get when i submit the form in angular:
.w.s.m.s.DefaultHandlerExceptionResolver : Resolved [org.springframework.validation.BindException: org.springframework.validation.BeanPropertyBindingResult: 1 errors<EOL>Field error in object 'productDto' on field 'sizePrices': rejected value [[{"size":"34","price":2342},{"size":"f4ef","price":3434}]]; codes [typeMismatch.productDto.sizePrices,typeMismatch.sizePrices,typeMismatch.java.util.List,typeMismatch]; arguments [org.springframework.context.support.DefaultMessageSourceResolvable: codes [productDto.sizePrices,sizePrices]; arguments []; default message [sizePrices]]; default message [Failed to convert property value of type 'java.lang.String' to required type 'java.util.List' for property 'sizePrices'; nested exception is java.lang.IllegalStateException: Cannot convert value of type 'java.lang.String' to required type 'com.restaurant.dto.SizePriceDto' for property 'sizePrices[0]': no matching editors or conversion strategy found]]
SizePriceDto Class
#Getter
#Setter
public class SizePriceDto {
private String size;
private Long price;
}
SizePrice Class:
#Entity
#Getter
#Setter
public class SizePrice {
#Id
#GeneratedValue(strategy = GenerationType.IDENTITY)
private Long id;
private String size;
private Long price;
#ManyToOne
#JoinColumn(name = "product_id")
private Product product;
}
My request from angular:
https://i.postimg.cc/zfnXT4mT/image.png
I tried alot of methods but nothing works and i dont have any idea how to fix this error.

Retrofit + GSON = entering "onFailure"

I am trying to first use the Retrofit to get data to an course. But I figured out that my program is going to the "onFailure" option.
ERROR MESSAGE:
2020-03-07 18:54:04.499 17756-17756/com.example.apirequest I/MainActivity: java.lang.IllegalStateException: Expected BEGIN_OBJECT but was BEGIN_ARRAY at line 1 column 2 path $
Basically I'm trying to get firstly only the "launch_year" from API https://api.spacexdata.com/v3/launches/
Can someone help me to figure out the reason I can't get it right?
In grade I added:
implementation 'com.squareup.retrofit2:retrofit:2.0.0-beta4'
implementation 'com.google.code.gson:gson:2.8.5'
implementation 'com.squareup.retrofit2:converter-gson:2.0.0-beta4'
My codes are bellow:
MainActivity:
public class MainActivity extends AppCompatActivity {
private static final String TAG = "MainActivity";
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Retrofit retrofit = new Retrofit.Builder().
baseUrl(APIService.urlBase)
.addConverterFactory(GsonConverterFactory.create()).
build();
APIService service = retrofit.create(APIService.class);
Call<rocketCatalog> requestModels = service.listCaralog();
requestModels.enqueue(new Callback<rocketCatalog>() {
#Override
public void onResponse(Call<rocketCatalog> call, Response<rocketCatalog> response) {
rocketCatalog catalog = response.body();
for (Rockets rocket : catalog.rocket){
Log.i(TAG,String.format("%s",rocket.getLaunch_date()));
}
}
#Override
public void onFailure(Call<rocketCatalog> call, Throwable t) {
Log.i(TAG,String.format("============= Failure =============="));
}
});
LinearLayoutManager layoutManager = new LinearLayoutManager(MainActivity.this);
}
}
APIService:
public interface APIService {
public static final String urlBase = "https://api.spacexdata.com/v3/";
#GET("launches")
Call<rocketCatalog> listCaralog();
}
rocketCatalog:
public class rocketCatalog {
public List<Rockets> rocket;
public List<Rockets> getRocket() {
return rocket;
}
public void setRocket(List<Rockets> rocket) {
this.rocket = rocket;
}
}
Rockets:
public class Rockets {
String launch_date;
public String getLaunch_date() {
return launch_date;
}
public void setLaunch_date(String launch_date) {
this.launch_date = launch_date;
}
}
Your endpoint returns a List so you need a List type for your response.
public static final String urlBase = "https://api.spacexdata.com/v3/";
#GET("launches")
Call<List<Rockets>> listCaralog();
also make sure the field launch_date in your Rockets Class exists in the response.
with your help I figured it out what was happening.
Indeed I was receiving a List, but I had to re struct, I've made what Mohamed Mohsin suggested and modified my rocketCatalog.java to:
rocketCatalog:
String launch_year;
public String getLaunch_year() {
return launch_year;
}
public void setLaunch_year(String launch_date) {
this.launch_year = launch_date;
}
}
Also, I changed "launch_date" which was wrong to "launch_year"
Thank you so much, again!

Angular 7 Service Returns Undefined from restful API

I'm trying to return a list of tasks from and Angular service that gets the data from a restful .NET API. The API returns the proper data, but when console.log the result from the subscribed to function it is only undefined. Please help!
Here is the API method that is functioning correctly:
TaskController.cs
[HttpGet, Route("")]
public IEnumerable<GetAllTasksAndInfoDto> GetAll()
{
IEnumerable<GetAllTasksAndInfoDto> tasks = _taskRepository.GetAllTasksAndInfo();
return tasks;
}
I have two services on called task.service.ts and one called email-notification.service.ts. Task is to return all the tasks in a collection. Email-Notification is to retrieve the tasks from the task service, then iterate through them to check a for a specific property value in the task data.
Here is the method from task.service.ts:
export class TaskService {
constructor(private http: HttpClient, private messageService: MessageService) {}
getTasks(): Observable<getAllTasksAndInfo[]> {
return this.http.get<getAllTasksAndInfo[]>(http://localhost:xxxxx/api/task)
.pipe(
tap(_=>this.log('fetched tasks')),
catchError(this.handleError<getAllTasksAndInfo[]>('getTasks', [])));
}
I'm not sure how to check the data at this ^ point, but it's not showing an error.
Here is the code from email-notification that is returning "Undefined":
export class EmailNotificationService{
tasks: getAllTasksAndInfo[];
constructor(private http: HttpClient, private messageService: MessageService, public taskService: TaskService)
{
this.getTasks().subscribe(result => this.tasks = result);
}
getTasks(): Observable<getAllTasksAndInfo[]>{
return this.taskService.getTasks();
}
this.tasks.forEach((task) => {
console.log(`task: ${JSON.stringify(task)}`);
}
});
Here I am getting an error in the console for trying to iterate through and undefined object.
Here is my typescript class:
export class getAllTasksAndInfo {
taskId: number;
stepName: string;
officeName: string;
csrName: string;
taskType: string;
daysActive: number;
}
and here is my API class:
public class GetAllTasksAndInfoDto
{
public int taskId { get; set; }
public string stepName { get; set; }
public string officeName { get; set; }
public string csrName { get; set; }
public string taskType { get; set; }
public int daysActive { get; set; }
}
Any help appreciated. Thank you!
It might be because you have not subscribed to the Observable or the initial data that is the tasks array is preset to undefined
have you tried using rxjs to subscribe to your observable in your EmailNotificationService sometimes it might come in undefined because the initial value of the array is undefined to begin with maybe setting it up with an empty array might help
private tasks: getAllTasksAndInfo[] = [];
constructor(private http: HttpClient, private messageService: MessageService, public taskService: TaskService)
{
this.tasks = this.getTasks();
}
getTasks() {
this.taskService.getTasks().subscribe((tasks: getAllTasksAndInfo[]) => {
console.log(tasks);
return tasks;
});
}

displaying data from restful service in react UI through nodejs

I've developed a restful webservice which displays a simple data using below code.
package com.mike;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;
#RestController
public class MessageController {
#RequestMapping(value="/message",method=RequestMethod.GET)
public Message print(){
return new Message(1,"Hello Mike!!");
}
}
and the bean is as follows....
package com.mike;
public class Message {
private int id;
private String message;
public Message(int id, String message){
this.id=id;
this.message=message;
}
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getMessage() {
return message;
}
public void setMesssage(String message) {
this.message = message;
}
}
now I want to display this data in an Nodejs application, so I wrote a file named app.js and wrote the following code to display data in the console.and it worked fine. but i don't know how to integrate this nodejs application with reactjs UI so that i can display in on a web page. so please kindly help me, I am new to both node and react.the code for app.js is below...
var http=require('http');
var extServerOptions={
host:'localhost',
port:'8080',
path:'/message',
method:'GET'
};
var mes=[];
var x;
var text="";
function get(){
http.request(extServerOptions,function (res){
res.setEncoding('utf8');
res.on('data',function(data){
mes=JSON.parse(data);
console.log(mes);
for(x in mes){
text+=mes[x]+" ";
};
console.log(text);
});
}).end();
};
get();
please kindly say me the stepps i need to do in order to display data in a react page.

How to set up retrofit for my application?

I am just learning about Retrofit and Android development. What I would like to do is send a fairly complex JSON object to a server from a website and be able to retrieve it using Retrofit as a Java Object for my Android Application.
So basically something like this,
Website JSON --Ajax Call--> Server --Retrofit--> Android Application (Java Object / Collection)
Which server would be the best to set this up? Also are there any good references on how to accomplish this?
Thank you
With retrofit and android, you only need a couple of things
A java model
public class User {
private String name;
private String password;
public User(String name, String password) {
this.name = name;
this.password = password;
}
//Getters and setters
//...
}
A retrofit interface
public interface APIService {
#FormUrlEncoded
#Headers("Accept: application/json")
#POST("register")
Call<AuthRegister> createUser(
#Field("name") String name,
#Field("password") String password
);
}
A callback for retrofit
public class AuthRegister {
#SerializedName("message")
#Expose
private String message;
#SerializedName("errors")
#Expose
private Errors errors;
public String getMessage() {
return message;
}
public Errors getErrors() {
return errors;
}
}
A network client
public class NetworkClient {
public static Retrofit retrofit;
/*
This public static method will return Retrofit client
anywhere in the appplication
*/
public static Retrofit getRetrofitClient() {
//If condition to ensure we don't create multiple retrofit instances in a single application
if (retrofit == null) {
//Defining the Retrofit using Builder
HttpLoggingInterceptor interceptor = new HttpLoggingInterceptor();
interceptor.level(HttpLoggingInterceptor.Level.BODY);
OkHttpClient client = new OkHttpClient.Builder()
// .addInterceptor(interceptor)
.connectTimeout(30, TimeUnit.MINUTES)
.build();
retrofit = new Retrofit.Builder()
.baseUrl(Config.BASE_URL) //This is the only mandatory call on Builder object.
.client(client)
.addConverterFactory(GsonConverterFactory.create()) // Convertor library used to convert response into POJO
.build();
}
return retrofit;
}
}
The call inside the activity you are to display the response or save the data
private void saveUser(String name, String password){
Retrofit retrofit = NetworkClient.getRetrofitClient();
APIService service = retrofit.create(APIService.class);
Call<AuthRegister> call = service.createUser(name, password);
call.enqueue(new Callback<AuthRegister>() {
#Override
public void onResponse(Call<AuthRegister> call, Response<AuthLogin> response) {
if (response.code() == 200) {
if (response.body().getMessage() != null) {
Toast.makeText(mContext, "Success", Toast.LENGTH_SHORT).show();
} else {
Toast.makeText(mContext, "Could not save user", Toast.LENGTH_LONG).show();
}
} else {
Toast.makeText(mContext, "Could not save user", Toast.LENGTH_LONG).show();
}
}
#Override
public void onFailure(Call<AuthRegister> call, Throwable t) {
new PrefManager(mContext).clearUser();
Log.e(TAG, t.toString());
Toast.makeText(mContext, "Could not save user", Toast.LENGTH_SHORT).show();
}
});
}
You can use any server as you need. Any complex JSON can be handled by retrofit library. Check the Following link Retrofit android example web services

Categories