How do I address this mapping problem for Spring MVC? - java

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.

Related

#RefreshScope Annotation return an empty data

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.

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.

Error when I try to enter on local adress

When I try to reach the website from http://localhost:8080/Films/showAll i obtain the 404 http error. So I try to other address and from http://localhost:8080/showAll i see 406 http error.
The controller class looks like this:
package Films.controller;
import Films.dao.DirectorDAO;
import Films.dao.FilmDAO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;
import java.util.List;
import java.util.Scanner;
#RestController
// klasa odpowiada za pobieranie oraz przsył danych wykorzystywanych w DAO
public class FilmOperationController {
......
// metoda zwraca listę wszystkich filmów bazie
#RequestMapping(value = "/showAll", produces = MediaType.APPLICATION_JSON_VALUE, method = RequestMethod.GET)
#Transactional(readOnly = true)
public List<Object[]> showAll() {
return filmDao.getAllFilms();
}
....
}
Here is pom.xml and java config file.
https://github.com/lukasz-chojn/films_database/blob/master/pom.xml
https://github.com/lukasz-chojn/films_database/blob/master/src/main/java/Films/component/Config.java
Any ideas what can be wrong?
I spent few hours to debug your issue. There was lot of unused dependencies and code. I refactored the code, and resolved the issue.
Here is the link to download it
https://github.com/deepakjain0812/testrepo
Please run it on your local and let me know if you see any issue.
I will delete this repo once you confirm that you are no longer facing issues with status codes like 500, 401 or 406
http://localhost:8080/showAll
A #RestController is supposed to be consumed by a Rest Client, not a browser.
In order to test this kind of controller, you can use another tool like curl or Postman.
Here is an example of how you would use your API using curl with a terminal: curl http://localhost:8080/showAll

java springBoot - How to render a html page

I am using spring boot to try and build my own mini website.
I have a controller
package hello;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.bind.annotation.RequestMapping;
#RestController
public class HelloController {
#RequestMapping("/greeting")
public String index() {
return "index";
}
}
and a html file resources/templates/index which I am trying to render but I just get the text "index" rendered. How can I return the html file instead of the text?
You have specified #RestController which says the result should be put into the #ResponseBody. You would want to use #Controller instead and then make sure you have a template framework (Thymeleaf, etc) in the classpath. Normally with most template frameworks you have to include the .html on the file that is within the templates folder.

I can't use yoccaddon in Hybris 5.5.1

I am using an Hybris 5.5.1 trial for learning purposes. I want to create a ycommercewebservice addon with yoccaddon like the documentation points, but I don't see that module, just the yaddon.
Iis it the same? I followed the documentation steps but using yaddon insteado of yoccaddon.
I tried with yaddon and after following the documentation and building successfully, my new api resource throws "There is no resource for path /rest/v2/apparel-uk/testing"
My controller has:
package com.test.controllers;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
#Controller
#RequestMapping(value = "/{baseSiteId}/testing")
public class TestController
{
#RequestMapping(method = RequestMethod.GET)
#ResponseBody
public String getTesting()
{
String testText = "Hello Test";
return testText;
}
}
You need to add #ApiVersion("v2) or #ApiVersion("v1).
Hmm... I am not big familiar with the addon logic of hybris, but what I see here is that you do a request on /rest/v2/apparel-uk/testing, but you have only the following Request Mapping:
#RequestMapping(value = "/{baseSiteId}/testing")
So the part /rest/v2/ is missing.
Interesting. I've got hybris 5.5.1 here and I'm trying to find the yoccaddon myself - I can't. Makes me wonder if it's been deprecated (and if so, why the wiki documentation still refers to it...
Does the API respond if you hit /{baseSiteId}/testing ? I'd suggest adding a web module (with the relevant webfoot configured) in your extensioninfo.xml - e.g.
<webmodule jspcompile="false" webroot="/rest/v2"/>
This should then mean that your controller will be available on the expected URL.

Categories