I am using the following configuration for controller to redirect to external url.
My application base url is http://www.testcebs.com:8080/SpringSecDemo11/
#RequestMapping(value = "/tryIt", method = RequestMethod.GET)
public String goToGoogle() {
String redirectUrl = "www.google.com";
return "redirect:" + redirectUrl;
}
On calling the "/tryIt" url it show 404. and Url goes to
http://www.testcebs.com:8080/SpringSecDemo11/www.google.com
Please suggest any way to get out of it.
Regards,
Pranav
Prefix your url string with the protocol, i.e. http://www.google.com.
final String redirectUrl = "redirect:http://www.google.com";
return redirectUrl;
Related
Here is my controller class code
#PreAuthorize("hasRole('Users')")
#RequestMapping("/")
public String helloWorld() {
return "Hello World!";
}
In azure portal I have set the redirect uri as http://localhost:8080/login/oauth2/code/azure
When I hit on localhost:8080 it returns me hello world. Along with this, it returns a code on redirect uri.
How can I retrieve that code.
You can get it by using getParameter,
private final static String CODE = "code";
String code = httpRequest.getParameter(CODE);
Here is an example.
I have url like http://www.example.com/api/{uriparam}/something
How should I replace the uriparam with my parameter call test?
So that it will be like http://www.example.com/api/test/something
In rest API its called as path, to do that, here is sample of controller using path. here is sample with jax rs.
#Path("/testUrl/{data}")
#GET
public String checkPending(#PathParam("data") String data) {
String yourUrl = "http://www.example.com/api/" + data + "/something";
return yourUrl;
}
You can use org.springframework.web.util.UriComponentsBuilder as below :-
Map<String, String> parameters = new java.util.HashMap<String,String>();
parameters.put("uriparam","test");
String url = UriComponentsBuilder.fromHttpUrl("http://www.example.com/api/{uriparam}/something".trim()).buildAndExpand(parameters).encode().toString();
it will return you the required url
I have a website on which I added a dropdown menu and a button. When the dropdown menu changes it calls a Requestmapping "/Filter".
The following code is underlying the Requestmapping "/Filter"
#Controller
public class FilterController
{
#RequestMapping(value = "/Filter")
public String selectmenu(#RequestParam String selectmenu, HttpServletRequest request)
{
System.out.println(selectmenu);
String URL = makeUrl(request);
System.out.println(URL);
return “ABC";
};
public static String makeUrl(HttpServletRequest request)
{
return request.getRequestURL().toString() + "?" + request.getQueryString();
}
}
This returns the following URL: http://localhost:8080/Filter?null
I understand why this URL is returned. But the URL in the address bar is different, namely: http://localhost:8080/Name?id=1
I want to retrieve the URL in the address bar, how can this be accomplished?
I'm a little bit confused. I'm writing an MVC application and have a simple controller like this:
#Controller
public class ProfileController {
final String DEFAULT_MALE_AVATAR = "../resources/graphics/avatarMan.PNG";
final String DEAULT_FEMALE_AVATAR = "../resources/graphics/avatarWoman.PNG";
#Autowired
UserService userService;
#RequestMapping(value = "/profile", method = RequestMethod.GET)
public String index() {
return "user/profile";
}
#RequestMapping(value = "profile/getavatar", method = RequestMethod.GET)
public #ResponseBody String getLoggedUserAvatar() {
String userMail = SecurityContextHolder.getContext()
.getAuthentication().getName();
User loggedUser;
if (userMail != null) {
loggedUser = userService.findUserByEmail(userMail);
return loggedUser.getAvatar();
} else {
return DEFAULT_MALE_AVATAR;
}
}
I've also got a simple js file called with "onload" in my body html tag while entering /profile section.
function init() {
var url = "profile/getavatar";
$.ajax({
url : url
}).then(function(data) {
avatarLink = data;
loadAvatar(avatarLink);
});
function loadAvatar(avatarLink){
$("#userAvatar").attr("src", avatarLink);
}
}
And for some strange reason I get ridirected to "profile/getavatar" and the page contains text with value returned by getLoggedUserAvatar(). The funny thing is I've also got some other controllers to other sections with almost the same js files and controllers - and they work like a charm.
What am I missing?
I hope when you hit the URL directly, you are getting expected response. If that is not happening, then there is something else wrong. If you are getting proper response when you directly hit the url in browser, then try doing the below when doing the ajax call. It passes the content type that is expecting back from the server.
function init() {
var url = "profile/getavatar";
$.ajax({
url : url,
dataType: "json"
}).then(function(data) {
avatarLink = data;
loadAvatar(avatarLink);
});
function loadAvatar(avatarLink){
$("#userAvatar").attr("src", avatarLink);
}
}
If you are using spring 4, Please make sure that you have Jakson jars in your dependency library. framework will automatically pickup the content negotiator as JSON and will find for the Jakson jars in the background to transport JSON to server and get JSON data back from server
use JAXB jars , in case you need to handle XML as content negotiator.
I have the following string in a Spring MVC Controller action. I wanted the controller action to render a view page that takes this following string and then does the redirection.
String redirectUrl = "http://www.yahoo.com"
My controller action looks like the following:
#RequestMapping(method = RequestMethod.GET)
public String showForm(HttpServletRequest request, ModelMap model)
{
String redirectUrl = "http://www.yahoo.com";
model.addAttribute("redirectUrl", redirectUrl);
return "loginSuccess"; //this is my view JSP file
}
Is there a way in JSP view to do this redirect without using JSTL? I want a clean redirect and not send any query string parameters.
Thanks,
Maybe I misunderstood but if you want a redirect, you'll have to use a RedirectView.
String redirectUrl = "http://www.yahoo.com";
return "redirect:" + redirectUrl;
Or use a RedirectView instance
#RequestMapping(method = RequestMethod.GET)
public View showForm(HttpServletRequest request, ModelMap model)
{
String redirectUrl = "http://www.yahoo.com";
RedirectView view = new RedirectView(redirectUrl );
return view;
}