#RefreshScope Annotation return an empty data - java

I Was following this tutorial on YouTube I have been able to successfully run the config server where I host two properties files here File hosted. and on the client side when I tried to consume the value I get an empty response here is the dummy controller I have created.
import org.springframework.beans.factory.annotation.Value;
import org.springframework.cloud.context.config.annotation.RefreshScope;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
#RestController
#RequestMapping("/api/test")
#RefreshScope
public class TestController {
#Value("${test.name}")
private String product;
#GetMapping
public String test() {
return product;
}
}
but when I send a get request to route /api/test, I get a response of 200OK with no actual test value. name, What am I doing wrong?.

The tutorial in youtube uses the default naming convetion but you probably have changed it and now spring cloud config server does not know which property file expects your service to have.
In cloud config server the file is saved as product.properties.
For this reason if your client service has some other name, this will not work. To correct it go to the client application and in bootstrap.yaml add the property spring.application.name: product.

Related

How do I address this mapping problem for Spring MVC?

package com.coding;
import javax.validation.Valid;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
#Controller
#RequestMapping("/customer")
public class CustomerController {
#RequestMapping(value="/showForm")
public String showForm(Model theModel) {
theModel.addAttribute("customer",new Customer());
return "customer-form";
}
#RequestMapping("/processForm")
public String processForm
(#Valid #ModelAttribute("customer") Customer theCustomer,BindingResult theBindingResult) {
if(theBindingResult.hasErrors()) {
return"customer-form";
}else {
return "customer-confirmation";
}
}
}
New to Spring!:)
I am trying to build a dynamic project using Spring MVC which is "form validation" . I tried everything possible to solve this problem "WARNING: No mapping for GET /spring-mvc-demo/customer/showForm" but its not working . Any solution please?
As you can see this warning clearly tells the path does not exist and that it's not able to recognize at this path any handlers are available.
What you can do is, Make sure the path matches your controller method mappings.
Have you checked localhost:8080/customer/showForm? Try this URL it will work?
Hii Anjita I tried to reConstruct your problem it is working fine for me I have created a CustomerController class like this.
I created a login form like this
And I made a request from a browse with the URL "http://localhost:8080/customer/showForm". And the output is
Whenever I tried to give a URL "localhost:8080/spring-mvc-demo/customer/showForm" it is showing a Whitelabel error try removing your project name in URL and try once.

How to send GET request to Azure FHIR from Spring-boot?

I am trying to send GET request to Azure API for FHIR using Spring Boot.
I have created an application having permission as "FHIR Data Contributor", and have its client-id, tenant-id and client-secret-value.
I have written the code below to get the access_token. It is returning the access_token, but how do I make use of the token and send GET request to FHIR ?
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.security.oauth2.client.OAuth2AuthorizedClient;
import org.springframework.security.oauth2.client.annotation.RegisteredOAuth2AuthorizedClient;
#RestController
public class HelloController {
#GetMapping("/token")
#ResponseBody
public String token(#RegisteredOAuth2AuthorizedClient("graph") OAuth2AuthorizedClient client) {
String accessToken=client.getAccessToken().getTokenValue();
return accessToken;
}
}
The application.yml contains:
azure:
activedirectory:
tenant-id: XXXXXXX-xxxxx-XXXXXXXX
client-id: XXXXXXX-xxxxx-XXXXXXXX
client-secret: XXXXXXX-xxxxx-XXXXXXXX
authorization-clients:
graph:
scopes: https://graph.microsoft.com/User.Read
I am able to do all this using Postman.

Getting data from Swapi API https://swapi.co/api/people/ using spring boot

when I use the RestTemplate and the Getforobject() method I get an error 500 code when running my spring boot. How can I consume this API using springboot?
package nl.qnh.qforce.controller;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.client.RestTemplate;
import java.util.Arrays;
import java.util.List;
#RestController
public class PersonController {
private static String url = "https://swapi.co/api/people/";
#Autowired
private RestTemplate restTemplate;
#GetMapping("/people")
public List<Object> getPeople(){
Object[] people = restTemplate.getForObject(url, Object[].class);
return Arrays.asList(people);
}
}
I would advise first checking if manually calling provided url returns expected response. You can use curl, postman or any other similar tool. In case call on provided url returns response, provide us with more context from your application, so we can assess which part is responsible for 500 error.

How to identify whether web service is REST or SOAP?

I am learning spring MVC and have wrote following code. I read some articles about SOAP and REST but in the beginner level controller code I have written I am not able to distinguish whether SOAP or REST is used. My controller code is as follows:
import java.util.ArrayList;
import java.util.List;
import javax.servlet.http.HttpServletRequest;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import com.model.Customer;
#Controller
public class SelectController {
#RequestMapping("/")
public String display(){
System.out.println("Inside controller");
return "demo";
}
#RequestMapping("/index")
public String indexpage(HttpServletRequest req, Model m){
String name = req.getParameter("name");
String pass = req.getParameter("pass");
String random = req.getParameter("abc");
System.out.println("Index page"+name+pass+random);
Customer cust = new Customer();
cust.setUsername(name);
cust.setPassword(pass);
System.out.println("Index page"+name+pass);
m.addAttribute("customer", cust);
return "hello";
}
The Controller that you have written is
neither REST nor SOAP.
Its a MVC Controller.
In your case, your returning "hello" string at the end of controller method, which in-turn gets translated/mapped to a page(hello.jsp or hello.html based on the configuration) and returns the same. So, at the end, what you get is Page rendered in a beautiful way with all the necessary Stylings and scripts applied.
In contrast, REST and SOAP doesn't work in that way. Its main purpose is for transferring the data alone(Yet you can send HTML page also)
Writing a REST Service is almost similar to what you have currently and is fairly easy. If you use Springboot then all you have to do is just replace the #Controller annotation with #RestController and return Customer object directly. In REST Controller you wont have HttpServletRequest & Model arguments.
But writing a SOAP service is another topic which may seem entirely different.
There are tons of examples and tutorials scattered around the web, which you can find easily on these topics.
References :
Controller vs RestController in Spring
Difference between Controller & RestController in Spring
SOAP vs REST
Hope this gives some high level idea of what those three are.

Invoking a spring web service

I'm trying to invoke a spring web service, using below url in browser the service "myservice" should return XML, ie based on the #RequestMapping annotations is the below URL correct?
> http://localhost:8080/mywebapp/myservice/feeds/allFeeds.xml/
import org.springframework.http.MediaType;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
#Controller
#RequestMapping("myservice")
public class TheController {
private TheService TheServiceWS;
public TheController(TheService TheServiceWS) {
this.TheServiceWS = TheServiceWS;
}
#RequestMapping(value = "feeds/allFeeds.xml", produces = MediaType.APPLICATION_XML_VALUE)
#ResponseBody
public String getValues() {
return TheServiceWS.getAllFeeds();
}
}
The problem for me was :
The #RequestMapping annotation value "myservice" was incorrect
should have been "mywebservice"
If the web service return as XML, it is the original the SOAP web service. In this case, you couldn't build the web service with #RequestMapping. The #RequestMapping is used when you want to build a REST web service.
In this case, you should use the Spring WS. You have to annotate the class with #Endpoint to create an web service endpoint. In the this endpoint, you create your request mapping with #Payloadroot. Please refer to this

Categories