I have this simple example setup to unit test spring Controller ModelAndView with MockMvc, but the response is always empty, even though in debug I can see that the controller code is executed.
Here's the code:
/src/main/java/controller/MvcController.java
package controller;
import java.util.HashMap;
import java.util.Map;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;
#Controller
public class MvcController {
#RequestMapping("/hello/{id}")
public ModelAndView hello(#PathVariable("id") String id) {
Map<String, String> model = new HashMap<>();
ModelAndView modelAndView = new ModelAndView("hello", model);
return modelAndView;
}
}
/src/main/webapp/WEB-INF/jsp/hello.jsp
<%# taglib prefix="spring" uri="http://www.springframework.org/tags"%>
<%# taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%>
<%# taglib prefix="e"
uri="https://www.owasp.org/index.php/OWASP_Java_Encoder_Project"%>
<%# page contentType="text/html; charset=UTF-8"%>
<!DOCTYPE html>
<html>
<head>
</head>
<body>HELLO WORLD
</body>
</html>
/src/test/java/controller/MvcControllerTest.java
package controller;
import static org.assertj.core.api.Assertions.assertThat;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.mock.web.MockHttpServletResponse;
import org.springframework.test.context.junit4.SpringRunner;
import org.springframework.test.web.servlet.MockMvc;
import org.springframework.test.web.servlet.MvcResult;
import org.springframework.test.web.servlet.RequestBuilder;
import org.springframework.test.web.servlet.ResultActions;
import org.springframework.test.web.servlet.request.MockMvcRequestBuilders;
import org.springframework.test.web.servlet.setup.MockMvcBuilders;
import org.springframework.web.servlet.view.InternalResourceViewResolver;
#RunWith(SpringRunner.class)
#SpringBootTest(classes = { MvcController.class })
public class MvcControllerTest {
#Autowired
private MvcController mvcController;
private MockMvc mockMvc;
#Before
public void init() {
InternalResourceViewResolver resolver = new InternalResourceViewResolver();
resolver.setPrefix("/WEB-INF/jsp/");
resolver.setSuffix(".jsp");
this.mockMvc = MockMvcBuilders.standaloneSetup(mvcController).setViewResolvers(resolver).build();
}
#Test
public void testHello() throws Exception {
RequestBuilder requestBuilder = MockMvcRequestBuilders.get("/hello/XWQHEDg9e72t");
ResultActions result = mockMvc.perform(requestBuilder);
MvcResult mvcResult = result.andReturn();
MockHttpServletResponse mockHttpServletResponse = mvcResult.getResponse();
String response = mockHttpServletResponse.getContentAsString();
assertThat(response).contains("HELLO WORLD");
}
}
The key word in MockMvc is mock. This framework does not set up a full Servlet container with a JSP engine. As such, it doesn't render the view that your controller returns and the MockHttpServletResponse does not contain a body (for this use case).
You can use MockHttpServletResponse#getForwardedUrl() to get the url of the JSP that the Servlet container would've forwarded the request to, constructed from your view name and the prefix and suffix of your InternalResourceViewResolver.
Related
I'm developing a Java Spring Boot Web App where users can register. When they register, they get sent a verification email. This is no longer working when I've tried to get the link in the email to actually confirm the registration because I get the error: "An error happened during template parsing (template: "mail/verifyEmail.html")". I'm not sure why this could be. I've attached below the verifyEmail.html file, and then the accompanying EmailService.java file which has the EmailService() and sendVerificationEmail() methods:
<!DOCTYPE html>
<html>
<th:block xmlns:th="http://www.thymeleaf.org">
<span th:text="'Hello ' + ${name}"></span>
<p>Thank you for registering for the bcoreHW site!</p>
<p>Please click here to verify your email address.</p>
</th:block>
</html>
import java.util.Date;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeMessage;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.mail.javamail.JavaMailSender;
import org.springframework.mail.javamail.MimeMessageHelper;
import org.springframework.mail.javamail.MimeMessagePreparator;
import org.springframework.stereotype.Service;
import org.thymeleaf.TemplateEngine;
import org.thymeleaf.context.Context;
import org.thymeleaf.templateresolver.ClassLoaderTemplateResolver;
#Service
public class EmailService {
#Autowired
private JavaMailSender mailSender;
#Value("${mail.enable}")
private boolean enable;
private TemplateEngine templateEngine;
private void send(MimeMessagePreparator preparator) {
if(enable) {
mailSender.send(preparator);
}
}
#Autowired
public EmailService(TemplateEngine templateEngine) {
ClassLoaderTemplateResolver templateResolver = new ClassLoaderTemplateResolver();
templateResolver.setPrefix("mail/");
templateResolver.setSuffix(".html");
templateResolver.setTemplateMode("HTML5");
templateResolver.setCacheable(false);
templateEngine.setTemplateResolver(templateResolver);
this.templateEngine = templateEngine;
}
public void sendVerificationEmail(final String emailAddress) {
Context context = new Context();
context.setVariable("name", "Bob");
final String emailContents = templateEngine.process("verifyEmail", context);
System.out.println(emailContents);
MimeMessagePreparator preparator = new MimeMessagePreparator() {
// not working for some reason: #Override
public void prepare(MimeMessage mimeMessage) throws Exception {
MimeMessageHelper message = new MimeMessageHelper(mimeMessage);
message.setTo(emailAddress);
message.setFrom(new InternetAddress("no-reply#testSite.com"));
message.setSubject("Please Verify Your Email Address");
message.setSentDate(new Date());
message.setText(emailContents, true);
}
};
send(preparator);
}
}
Does anyone see anything wrong with this? Thanks. Also, here is the error message I'm getting:
Caused by: java.io.FileNotFoundException: ClassLoader resource "mail/verifyEmail.html" could not be resolved
at org.thymeleaf.templateresource.ClassLoaderTemplateResource.reader(ClassLoaderTemplateResource.java:130) ~[thymeleaf-3.0.11.RELEASE.jar:3.0.11.RELEASE]
at org.thymeleaf.templateparser.markup.AbstractMarkupTemplateParser.parse(AbstractMarkupTemplateParser.java:223) ~[thymeleaf-3.0.11.RELEASE.jar:3.0.11.RELEASE]
I found the issue. My prefix "mail/" is incorrect because it's actually in a directory called "mailTemplates". Silly me.
I want then user logined(authorize) in header menu displayed menu with username(login) like at picture.
For this purpose i have the corresponding code in angular:
File header.component.html:
<nav class="navbar">
<a routerLink="" id="nav_logo">
DishOnline
</a>
<div id="nav_right_section">
<a routerLink="/loginPage">
Вход
</a>
<button [hidden]=" receivedAnswer!==undefined && receivedAnswer!==null" mat-button [matMenuTriggerFor]="menu">{{receivedAnswer.login}}</button>
<mat-menu #menu="matMenu">
<button mat-menu-item>Мой кабинет</button>
<button mat-menu-item>Выйти</button>
</mat-menu>
</div>
</nav>
File header.component.ts:
import {Component,OnInit } from '#angular/core';
import { LoginNameService} from '../services/loginName.service';
import {LoginName} from '../classes/loginName';
#Component({
selector: 'app-header',
templateUrl: './header.component.html',
styleUrls : ['./header.component.scss'],
providers: [LoginNameService]
})
export class HeaderComponent implements OnInit
{
receivedAnswer: LoginName;
loginNameObject: LoginName=new LoginName();
constructor(private loginNameService: LoginNameService) {}
submit() {
this.loginNameService.postData()
.subscribe((data: LoginName) => {},
error => console.log(error)
);
}
ngOnInit(): void {
this.receivedAnswer=this.loginNameObject;
this.submit();
}
}
I send on server(backend - Spring Boot) empty POST request, using for this service loginName.service.ts.
File loginName.service.t:
import {Injectable} from '#angular/core';
import {HttpClient, HttpHeaders} from '#angular/common/http';
#Injectable()
export class LoginNameService {
constructor(private http: HttpClient) { }
postData() {
// tslint:disable-next-line:prefer-const
let baseUrl = 'http://localhost:8089';
return this.http.post(baseUrl + '/loginName', null);
}
}
From service i have the answer in JSON:
{"login":"DishOnline"}
But received login not displayed in header.
Code receiving login on backend(Spring Boot):
package com.greatproject.dishonline.controller;
import com.greatproject.dishonline.service.MyUserDetailsService;
import com.greatproject.dishonline.service.MyUserPrincipal;
import org.springframework.security.core.Authentication;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;
import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.security.Principal;
import java.util.HashMap;
import java.util.Map;
#RestController
public class AuthorizationController {
#RequestMapping("/loginName")
public #ResponseBody
Map<String,String> loginName(Model model,
Authentication authentication,
HttpSession session,
MyUserPrincipal principal
) {
String login = "";
Authentication auth = SecurityContextHolder.getContext().getAuthentication();
login = auth.getName(); //get logged in username
Map<String, String> mapLoginName = new HashMap<String, String>();
mapLoginName.put("login", login);
return mapLoginName;
}
}
Please help me resolve this problem.
You can get the username off the principal. Try adding the principal.username to the model and retrieving it on the ui that way.
I am getting the error
"Resolved[org.springframework.web.multipart.support.MissingServletRequestPartException: Required request part 'file' is not present]"
I tried all the options provided on previous posts it didn't work please tell me what I am doing wrong this is my first spring-boot application.
My controller
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseStatus;
import java.io.File;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;
#RestController
public class SpringBootJdbcController {
#Autowired
JdbcTemplate jdbc;
#Autowired
private SpringBootJdbcService springBootJdbcService;
#RequestMapping("/insert")
public String index() {
jdbc.execute("insert into user(name,email)values('javatpoint','java#javatpoint.com')");
return "data inserted Successfully";
}
#CrossOrigin(origins = "http://localhost:4200")
#ResponseStatus(HttpStatus.OK)
#RequestMapping(value = "/uploadFile", method = RequestMethod.POST, consumes = "multipart/form-data")
public void uploadFiles(#RequestParam("file") MultipartFile file) throws Exception {
System.out.println("hello hello hello");
System.out.println("file---------" + file);
springBootJdbcService.readConfigData(file);
}
}
My Application.properties
spring.servlet.multipart.max-file-size=10MB
spring.servlet.multipart.max-request-size=11MB
api call postman image :-
I don't understand what is the problem. Its my first spring-boot application please help.
I'm new to Spring Web MVC and I tried to try Hello world app.
I followed an example from "Spring In Action 4th ed" book which only uses java config (without any XML file). I use Tomcat v7.0 server and servlet 3.
The problem is when I try to go to the home page I get Http 404 error.
The used Java classes are :
DispatcherServlet configuration:
package config;
import org.springframework.web.servlet.support.AbstractAnnotationConfigDispatcherServletInitializer;
public class FirstWebAppInitializer extends AbstractAnnotationConfigDispatcherServletInitializer{
#Override
protected String[] getServletMappings() {
return new String[] { "/" };
}
#Override
protected Class<?>[] getRootConfigClasses() {
return new Class<?>[] { RootConfig.class };
}
#Override
protected Class<?>[] getServletConfigClasses() {
return new Class<?>[] { WebConfig.class };
}
}
Spring MVC configuration :
package config;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.ViewResolver;
import org.springframework.web.servlet.config.annotation.DefaultServletHandlerConfigurer;
import org.springframework.web.servlet.config.annotation.EnableWebMvc;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurerAdapter;
import org.springframework.web.servlet.view.InternalResourceViewResolver;
#Configuration
#EnableWebMvc
#ComponentScan("web")
public class WebConfig extends WebMvcConfigurerAdapter {
#Bean
public ViewResolver viewResolver() {
InternalResourceViewResolver resolver = new InternalResourceViewResolver();
resolver.setPrefix("/WEB-INF/views/");
resolver.setSuffix(".jsp");
resolver.setExposeContextBeansAsAttributes(true);
return resolver;
}
#Override
public void configureDefaultServletHandling(DefaultServletHandlerConfigurer configurer) {
configurer.enable();
}
}
Configuration used to get other Beans (doesn't do any thing in the hello world app)
package config;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.ComponentScan.Filter;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.FilterType;
import org.springframework.web.servlet.config.annotation.EnableWebMvc;
#Configuration
#ComponentScan(basePackages={"lgmi_cr"},excludeFilters={#Filter(type=FilterType.ANNOTATION, value=EnableWebMvc.class)})
public class RootConfig {
}
Home page controller
package web;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
#Controller
public class HomeController {
#RequestMapping(value="/")
public String home() {
return "home";
}
}
A JSP file "home" uneder /WEB-INF/views/
<%# page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<title>First app</title>
</head>
<body>
<h1>Hello world</h1>
</body>
</html>
Since it 404 error, my first impression is that it could not find any controller but I don't know why.
Thank you for any help
I have just found the problem.
Actually, I added the external Spring jar files to the project classpath and I forget to add them to myProject/WebContent/WEB-INF/lib.
When I try to go to link http://localhost:8080/ I get "HTTP Status 404", I use Tomcat 7.0.47, and I get the following error : The requested resource is not available.
WebConfig.Java
package config;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.ViewResolver;
import org.springframework.web.servlet.config.annotation.DefaultServletHandlerConfigurer;
import org.springframework.web.servlet.config.annotation.EnableWebMvc;
import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurerAdapter;
import org.springframework.web.servlet.view.InternalResourceViewResolver;
#EnableWebMvc
#Configuration
#ComponentScan
public class WebConfig extends WebMvcConfigurerAdapter{
#Override
public void addResourceHandlers(ResourceHandlerRegistry registry) {
registry.addResourceHandler("/resources/static/js/**")
.addResourceLocations("/resources/static/js/");
registry.addResourceHandler("/resources/static/css/**")
.addResourceLocations("/resources/static/css/");
registry.addResourceHandler("/resources/static/views/**")
.addResourceLocations("/resources/static/views/");
registry.addResourceHandler("/resources/static/**")
.addResourceLocations("/resources/static/");
registry.addResourceHandler("/webjars/**")
.addResourceLocations("/webjars/");
}
#Bean
public ViewResolver getViewResolver(){
InternalResourceViewResolver resolver = new InternalResourceViewResolver();
resolver.setPrefix("/WEB-INF/jsp/");
resolver.setSuffix(".jsp");
return resolver;
}
#Override
public void configureDefaultServletHandling(DefaultServletHandlerConfigurer configurer) {
configurer.enable();
}
}
WebAppInitalizer.Java
import org.springframework.web.WebApplicationInitializer;
import org.springframework.web.context.support.AnnotationConfigWebApplicationContext;
import org.springframework.web.servlet.DispatcherServlet;
import javax.servlet.ServletContext;
import javax.servlet.ServletException;
import javax.servlet.ServletRegistration;
public class WebAppInitializer implements WebApplicationInitializer {
private static final String CONFIG_LOCATION = "config";
#Override
public void onStartup(ServletContext servletContext) throws ServletException {
System.out.println("***** Initializing Application for " + servletContext.getServerInfo() + " *****");
// Create ApplicationContext
AnnotationConfigWebApplicationContext applicationContext = new AnnotationConfigWebApplicationContext();
applicationContext.setConfigLocation(CONFIG_LOCATION);
// Add the servlet mapping manually and make it initialize automatically
DispatcherServlet dispatcherServlet = new DispatcherServlet(applicationContext);
ServletRegistration.Dynamic servlet = servletContext.addServlet("mvc-dispatcher", dispatcherServlet);
servlet.addMapping("/");
servlet.setAsyncSupported(true);
servlet.setLoadOnStartup(1);
}
}
MainController.Java
package controller;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
#Controller
public class MainController {
#RequestMapping("/")
public String homepage(){
return "homepage";
}
}
My project has the following structure :
try to add this #AnnotationDrivenConfig on top of WebConfig and change this #ComponentScan with this #ComponentScan("controller")
Try to add some text into your requestMapping example:
#RequestMapping("/homePage")
and then go to http://localhost:8080/homePage