onepseudoxy
onepseudoxy

Reputation: 580

java.lang.IllegalArgumentException: Locale part "en-US,en;q=0.9" contains invalid characters]

I got 400 bad request when I type, in Postman, the following URL http://localhost:8080/hello-world-internationalized enter image description here It's not the case if I specify the "Accept-Language" which is not required In fact I get the following Exception

2020-07-05 12:02:43.617 WARN 7744 --- [nio-8080-exec-1] .m.m.a.ExceptionHandlerExceptionResolver : Resolved [org.springframework.web.method.annotation.MethodArgumentTypeMismatchException: Failed to convert value of type 'java.lang.String' to required type 'java.util.Locale'; nested exception is java.lang.IllegalArgumentException: Locale part "en-US,en;q=0.9" contains invalid characters]

Bellow this is my controller

package com.in28minutes.rest.webservices.restfulwebservices.helloworld;

import java.util.Locale;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.MessageSource;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestHeader;
import org.springframework.web.bind.annotation.RestController;

@RestController
public class HelloWorldController {
    @Autowired
    private MessageSource messageSource;

@GetMapping(path ="/hello-world")
public String helloWorld() {
    return "Hello World!"; 
}

@GetMapping(path ="/hello-world-bean")
public HelloWorldBean helloWorldBean() {
    return new HelloWorldBean("Hello World!"); 
}

@GetMapping(path ="/hello-world-bean/path-variable/{name}")
public HelloWorldBean helloWorldPathVariable(@PathVariable String name) {
    return new HelloWorldBean(String.format("Hello, %s", name)); 
}

@GetMapping(path ="/hello-world-internationalized")
public String helloWorldInternationalized(@RequestHeader(name = "Accept-Language", required = false) Locale locale) {
    return messageSource.getMessage("good.morning.message", null, locale); 
}

}

And

    package com.in28minutes.rest.webservices.restfulwebservices;

import java.util.Locale;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.annotation.Bean;
import org.springframework.context.support.ResourceBundleMessageSource;
import org.springframework.web.servlet.LocaleResolver;
import org.springframework.web.servlet.i18n.AcceptHeaderLocaleResolver;

@SpringBootApplication
public class RestfulWebServicesApplication {
    

    public static void main(String[] args) {
        SpringApplication.run(RestfulWebServicesApplication.class, args);
    }
    
    @Bean
    public LocaleResolver localeResolver() {
        AcceptHeaderLocaleResolver localeResolver= new AcceptHeaderLocaleResolver();
        localeResolver.setDefaultLocale(Locale.US);
        return localeResolver;
    }
    
    @Bean
    public ResourceBundleMessageSource messageSource() {
        ResourceBundleMessageSource messageSource= new ResourceBundleMessageSource();
        messageSource.setBasename("messages");
        return messageSource;
    }
    
}

I created the project using Spring Intializr while picking spring boot 2.1.15, Java, Maven Project and Dependencies like Web, DevTools, JPA and H2.

Upvotes: 1

Views: 3610

Answers (1)

Pratik Nandanikar
Pratik Nandanikar

Reputation: 21

Change the method signature. Instead of

@GetMapping(path ="/hello-world-internationalized")
public String helloWorldInternationalized(@RequestHeader(name = "Accept-Language", required = false) Locale locale) {
    return messageSource.getMessage("good.morning.message", null, locale); 
}

Write like below

@GetMapping(path ="/hello-world-internationalized")
public String helloWorldInternationalized(@RequestHeader(name = "Accept-Language", required = false) String localeString) {
Locale locale = new Locale(localeString);
    return messageSource.getMessage("good.morning.message", null, locale); 
}

This is one of the workaround solutions.

Upvotes: 2

Related Questions