spring-mvc

Spring boot - setting default Content-type header if it's not present in request

情到浓时终转凉″ 提交于 2021-02-08 16:59:13
问题 I'm having the following problem: suppose I sometimes receive POST requests with no Content-type header set. In this case I want to assume that Content-type=application/json by default. Can I achieve this somehow using spring boot features and not using filters? Thanks 回答1: As of Spring Boot 2.x, you need to create a class that extends the WebMvcConfigurer interface, e.g.: @Configuration class WebMvcConfiguration implements WebMvcConfigurer { @Override public void configureContentNegotiation(

Getting details of binding errors for a JSON form in Spring MVC

北城余情 提交于 2021-02-08 15:57:18
问题 I use Spring MVC 3.2.4. I have a controller method that receives data from an HTML form. The method has the following signature: @RequestMapping(...) public String updateProduct(@Valid Product product, BindingResult bindingResult) In the method, I can get information about both data binding (e.g., a non-integer value in an integer field) and validation (JSR-303) errors in the following way: if (bindingResult.hasErrors()) { List<FieldError> errors = bindingResult.getFieldErrors(); for

Spring mvc - How to map all wrong request mapping to a single method

天大地大妈咪最大 提交于 2021-02-08 15:01:28
问题 I am using a spring(version 3.2.5) annotation driven mvc application, basically I need be able to catch any wrong url to a single method then display an error message. For instance, if my correct url www.url.valid/login maps to a controller, if the user mistakenly insert loginw or any invalid url it should map to a default controller. I have tried the following way: package com.controller; import static com.mns.utils.Constants.REGISTER_FAIL; import javax.servlet.http.HttpServletRequest;

Change @RequestMapping order

断了今生、忘了曾经 提交于 2021-02-08 11:38:51
问题 I have two methods mapped with @GetMapping: @GetMapping(path = "/**", produces = MediaType.APPLICATION_JSON_VALUE, params = "test") public String getTest(@RequestParam("test") String test) { ... } @GetMapping(path = "/some/path", produces = MediaType.APPLICATION_JSON_VALUE) public String getTest() { ... } Is it possible to request to endpoint /some/path?test=string be mapped to first method, and request to endpoint some/path be mapped to second method? 回答1: If I understand your issue

Spring MVC + Ajax JSON post

你离开我真会死。 提交于 2021-02-08 11:20:26
问题 I have a problem with send JSON to the Controller . I can't understand my problem. So, url - /notes/{username}/add Ajax : $.ajax({ type: "POST", contentType : 'application/json; charset=utf-8', dataType : 'json', url: window.location.pathname, data: JSON.stringify({ title: $("#title").val(), text: $("#text").val() }), success : function() { $("#title").val(""); $("#text").val(""); } }); Controller : @RequestMapping(value = "/{username}/add", method = POST) public void add(@RequestBody Note

Spring MVC doesn't show images

柔情痞子 提交于 2021-02-08 10:35:53
问题 Good day. I have created mvc-dipatcher-servlet.xml <beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:context="http://www.springframework.org/schema/context" xmlns:mvc="http://www.springframework.org/schema/mvc" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context

Error creating transaction manager bean

空扰寡人 提交于 2021-02-08 10:29:38
问题 Hi Im creating a sample project for springMVc with hibernate with oracle as backend . I am stuck with one issue seems like some configuration issue but unable to identify whats wrong. Any guidance will be of great help. org.hibernate.service.UnknownUnwrapTypeException: Cannot unwrap to requested type [javax.sql.DataSource] The stacktrace: WARNING: Exception encountered during context initialization - cancelling refresh attempt: org.springframework.beans.factory.BeanCreationException: Error

Sharing parent spring context with child in spring 5

拜拜、爱过 提交于 2021-02-08 10:28:22
问题 How to share parent context with child in spring 5? Using spring 4, we could pass locatorFactorySelector as context-param <context-param> <param-name>locatorFactorySelector</param-name> <param-value>classpath:refFactory.xml</param-value> </context-param> This support is removed from Spring 5 onward. What is the alternative to pass the parent context in web context? 回答1: The loading of the parent context based on locatorFactorySelector were handled at ContextLoader#loadParentContext() . But

How to transfer data from HTML to controller Spring

僤鯓⒐⒋嵵緔 提交于 2021-02-08 10:28:13
问题 I work with Spring Boot. I do not have a web.xml and jsp file I have a controller that processes the data and writes it to the database. @RequestMapping(value = "/registration", method = RequestMethod.POST, consumes = MediaType.APPLICATION_JSON_VALUE) @ApiOperation(value = "Registration user", response = Account.class) public AccountDto registration(@Valid @RequestBody RegistrationDto registration, HttpServletResponse response) { Account account = new Account(registration.getEmail(),

The prefix “beans” for element “beans:beans” is not bound

百般思念 提交于 2021-02-08 10:14:02
问题 I am trying to add CSS and JS to JSP page in a spring MVC project so that I have included the reference of js/css folder in dispatcher-servlet.xml as below: <?xml version="1.0" encoding="UTF-8"?> <beans:beans xmlns="http://www.springframework.org/schema/beans" xmlns:mvc="http://www.springframework.org/schema/mvc" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:context="http://www.springframework.org/schema/context" xmlns:p="http://www.springframework.org/schema/p" xmlns:tx="http:/