java.lang.NoSuchMethodError when using spring-social-twitter - java

I try using Twitter API via Spring library spring-social-twitter. However I'm when calling sendDirectMessage(String toScreenName, String text) function I get the following exception:
java.lang.NoSuchMethodError: 'org.springframework.http.HttpStatus org.springframework.http.client.ClientHttpResponse.getStatusCode()'
at org.springframework.social.twitter.api.impl.TwitterErrorHandler.handleError(TwitterErrorHandler.java:56) ~[spring-social-twitter-1.1.0.RELEASE.jar:1.1.0.RELEASE]
at org.springframework.web.client.ResponseErrorHandler.handleError(ResponseErrorHandler.java:63) ~[spring-web-6.0.4.jar:6.0.4]
at org.springframework.web.client.RestTemplate.handleResponse(RestTemplate.java:915) ~[spring-web-6.0.4.jar:6.0.4]
at org.springframework.web.client.RestTemplate.doExecute(RestTemplate.java:864) ~[spring-web-6.0.4.jar:6.0.4]
at org.springframework.web.client.RestTemplate.execute(RestTemplate.java:804) ~[spring-web-6.0.4.jar:6.0.4]
at org.springframework.web.client.RestTemplate.postForObject(RestTemplate.java:503) ~[spring-web-6.0.4.jar:6.0.4]
at org.springframework.social.twitter.api.impl.DirectMessageTemplate.sendDirectMessage(DirectMessageTemplate.java:77) ~[spring-social-twitter-1.1.0.RELEASE.jar:1.1.0.RELEASE]
at com.example.twittertest.TwitterController.send(TwitterController.java:17) ~[classes/:na]
at jakarta.servlet.http.HttpServlet.service(HttpServlet.java:731) ~[tomcat-embed-core-10.1.5.jar:6.0]
at jakarta.servlet.http.HttpServlet.service(HttpServlet.java:814) ~[tomcat-embed-core-10.1.5.jar:6.0]
I have the my TwitterTemplate configured as following:
#Component
public class TwitterTemplateCreator {
public Twitter getTwitterTemplate() {
return new TwitterTemplate(
"<cosumerKey>",
"<consumerSecret>",
"<accessToken>",
"<accessTokenSecret>");
}
}
TwitterController:
#RestController
#RequestMapping(("/api"))
public class TwitterController {
#Autowired
private TwitterTemplateCreator twitterTemplateCreator;
#PostMapping(value = "/send")
public void send(#RequestParam("name") String name, #RequestParam("text") String text) {
twitterTemplateCreator.getTwitterTemplate().directMessageOperations().sendDirectMessage(name, text);
}
}
And finally pom.xml:
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>3.0.2</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>com.example</groupId>
<artifactId>twitter-test</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>twitter-test</name>
<description>twitter-test</description>
<properties>
<java.version>17</java.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-devtools</artifactId>
<scope>runtime</scope>
<optional>true</optional>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-configuration-processor</artifactId>
<optional>true</optional>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<optional>true</optional>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.springframework.social</groupId>
<artifactId>spring-social-twitter</artifactId>
<version>1.1.0.RELEASE</version>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
<configuration>
<excludes>
<exclude>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
</exclude>
</excludes>
</configuration>
</plugin>
</plugins>
</build>
</project>
My guess is that there is inconsistency with dependencies versions but after trying different Spring Boot versions I still can't figure out what causes this issue.

Your guess is correct. Spring Social Twitter is no longer maintained. The last release, 1.1.2.RELEASE, was made in October 2015 and it depends on Spring Framework 4. Spring Boot 1.x is based on Spring Framework 4.x but it has been out of support since 2019.
I would recommend that you find an alternative to Spring Social Twitter. The alternative would be to use it with Spring Boot 1.x but then you will be using unsupported software for which bug fixes won't be available. Even if you're happy to take that risk, there's also a good chance that Spring Social Twitter isn't compatible with the current version of Twitter's API.

Solved by defining my own OAuth1 Rest Template using spring-social.
First included following dependencies:
<dependency>
<groupId>org.springframework.social</groupId>
<artifactId>spring-social-core</artifactId>
<version>1.1.6.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework.social</groupId>
<artifactId>spring-social-web</artifactId>
<version>1.1.6.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework.social</groupId>
<artifactId>spring-social-security</artifactId>
<version>1.1.6.RELEASE</version>
</dependency>
Then create new temlate which extends AbstractOAuth1ApiBinding:
public class TwitterTemplate extends AbstractOAuth1ApiBinding {
public static final String TWITTER_API = "https://api.twitter.com/1.1";
public TwitterTemplate(String consumerKey, String consumerSecret, String accessToken,
String accessTokenSecret) {
super(consumerKey, consumerSecret, accessToken, accessTokenSecret);
}
public TwitterUserResponse getUser(String name) {
return getRestTemplate().getForObject(
TWITTER_API + "/users/show.json?screen_name={name}", TwitterUserResponse.class, name);
}
}
Then just instantiate TwitterTemplate and call needed method.

Related

Swagger #OpenAPIDefinition not doing anything

so I have a simple Spring Boot REST API that works fine, now I have added some swagger dependencies, both for ui and core, and have done some testing with some tags like #ApiIgnore or #Operation and they both work fine and both are updated in the http://localhost:8080/swagger-ui/#/
Now I am trying to update the API info through the #OpenAPIDefinition tag, something like this in my application class:
#OpenAPIDefinition(
info = #Info(
title = "the title",
version = "0.0",
description = "My API",
license = #License(name = "Apache 2.0", url = "http://foo.bar"),
contact = #Contact(url = "http://gigantic-server.com", name = "Fred", email = "Fred#gigagantic-server.com")))
Now I have read here that the spring boot app should do a class scan and recognize the #OpenAPIDefinition bean and update the generated json in http://localhost:8080/v3/api-docs. But this is not the case, I have also tried setting that info with openapi.yaml files but also no luck.
I suspect this might have to do with my dependencies, as I am new to swagger and still a bit lost and might have mixed something up, I leave my pom.xml here:
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.5.4</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>com.example</groupId>
<artifactId>demo</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>demo</name>
<description>Demo project for Spring Boot</description>
<properties>
<java.version>11</java.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-mongodb-reactive</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-webflux</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-devtools</artifactId>
<scope>runtime</scope>
<optional>true</optional>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>io.projectreactor</groupId>
<artifactId>reactor-test</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>de.flapdoodle.embed</groupId>
<artifactId>de.flapdoodle.embed.mongo</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>io.springfox</groupId>
<artifactId>springfox-boot-starter</artifactId>
<version>3.0.0</version>
</dependency>
<dependency>
<groupId>io.springfox</groupId>
<artifactId>springfox-swagger-ui</artifactId>
<version>3.0.0</version>
</dependency>
<dependency>
<groupId>io.swagger.core.v3</groupId>
<artifactId>swagger-jaxrs2</artifactId>
<version>2.1.2</version>
</dependency>
<dependency>
<groupId>io.swagger.core.v3</groupId>
<artifactId>swagger-jaxrs2-servlet-initializer-v2</artifactId>
<version>2.1.2</version>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>
I am at a bit of a loss on how to continue and don't understand how swagger can recognize the #ApiIgnore tag for example but ignore the #OpenAPIDefinition one. As I said I am new to this whole thing and stackoverflow in general, so please forgive me if I forgot to add any relevant code, thank you!
Try removing some unnecessary dependencies:
<dependency>
<groupId>io.springfox</groupId>
<artifactId>springfox-swagger-ui</artifactId>
<version>3.0.0</version>
</dependency>
<dependency>
<groupId>io.swagger.core.v3</groupId>
<artifactId>swagger-jaxrs2</artifactId>
<version>2.1.2</version>
</dependency>
<dependency>
<groupId>io.swagger.core.v3</groupId>
<artifactId>swagger-jaxrs2-servlet-initializer-v2</artifactId>
<version>2.1.2</version>
</dependency>
springfox-boot-starter should bring them in already.
If this does not work you can always do the following:
#Configuration
public class SwaggerConfiguration {
#Bean
public OpenAPI openAPI() {
return new OpenAPI()
.info(new Info().title("the title").version("0.0").description("My API")
.contact(new Contact().name("Fred").url("http://gigantic-server.com").email("Fred#gigagantic-server.com")));
}
}
So after a lot of different things I found this guide where it explains how to alter API info, in a nutshell, I added a #Api(tags = "Employee") to my EmployeeController class, then in my Application class added:
#Bean
public Docket docket() {
return new Docket(DocumentationType.OAS_30)
.apiInfo(new ApiInfoBuilder()
.title("Employee API")
.description("A CRUD API to demonstrate Springfox 3 integration")
.version("0.0.1-SNAPSHOT")
.license("MIT")
.licenseUrl("https://opensource.org/licenses/MIT")
.build())
.tags(new Tag("Employee", "Endpoints for CRUD operations on employees"))
.select().apis(RequestHandlerSelectors.withClassAnnotation(RestController.class))
.build();
}
And this appears to solve the issue, I now wonder what would happen if I had multiple controllers, I hope this helps anyone that was in my predicament, the overall issue seemed to be that this is a springfox related way of altering API documentation and the previous ways are swagger-core related? As I said I am new to this world so if anyone could point me out to why it wasn't working it would be amazing, thanks!

Unable to send Post Request with SpringBoot

I´m tring to make a post request, according the code below:
String url = new StringBuilder().append("https://...").toString();
Map<String, Map<String, String>> body = buildBotBody(email,message);
restTemplate.postForEntity(url, body, Void.class);
This request don´t needs authentication, it don´t needs login and password. At the Postman I can make the request with success, but when I try to execute the code above, I got:
401 Unauthorized
I got simulate the error at the Postman and it gave me the message below:
"message": "The request has both SAS authentication scheme and 'Basic' authorization scheme. Only one scheme should be used."
When I changed the authentication method to "No Auth" at Postman, the request worked fine.
I think I have to set this option "No Auth" at the code, but I don´t know how.
I made this way:
private HttpEntity<?> builderHeadersToBuildBotBody(String email, StringBuilder message) {
Map<String, String> map = new HashMap<String, String>();
map.put("emailadress", email);
map.put("emailSubject", "Pendência para lançamento de horas do Jira");
map.put("emailBody", message.toString());
return builderHeaders(map);
}
private HttpEntity<?> builderHeaders(Map<String, String> map) {
HttpHeaders requestHeaders = new HttpHeaders();
//requestHeaders.setAccept(Collections.singletonList(new MediaType("application", "json")));
requestHeaders.setContentType(new MediaType("application", "json"));
//requestHeaders.set(HttpHeaders.USER_AGENT, "");
return new HttpEntity<>(requestHeaders);
}
private void sendBotMessage(StringBuilder message, String nome, String email) throws Exception{
try {
String url = new StringBuilder().append("https://prod-12.westeurope.logic.azure.com:443/workflows/fbf4c29cbcad4679b1a1159fff7b07f9/triggers/manual/paths/invoke?api-version=2016-06-01&sp=%2Ftriggers%2Fmanual%2Frun&sv=1.0&sig=zxm46aQnBj3ZTKPOddnnwUgtQZoQcQfixNtXVxAJjPg").toString();
HttpEntity<?> requestEntity = builderHeadersToBuildBotBody(email,message);
restTemplate.exchange(url, HttpMethod.POST, requestEntity, Void.class);
logger.info("Bot enviado com sucesso! " + nome);
} catch (Exception e) {
logger.error("Erro ao enviar Bot.", e);
throw e;
}
}
But the error continues.
If Spring Security is on your classpath you may need to disable authentication. This is done by creating a #Configuration class which extends the WebSecurityConfigurerAdaptor
#Configuration
public class NoSecurityConfiguration extends WebSecurityConfigurerAdapter {
#Override
protected void configure(final HttpSecurity httpSecurity) throws Exception {
httpSecurity.authorizeRequests().anyRequest().permitAll()
.and()
.csrf().disable();
}
}
Obviously this is something you would not want to do in production but can be useful in dev environments.
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0
https://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.1.9.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>br.com.oss.jira.quality</groupId>
<artifactId>jira-quality</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>jira-quality</name>
<description>Gerenciador de inconsistências no Jira</description>
<properties>
<java.version>1.8</java.version>
<lombok.version>1.18.6</lombok.version>
</properties>
<dependencies>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<scope>runtime</scope>
</dependency>
<!-- <dependency>
<groupId>com.oracle.database.jdbc</groupId>
<artifactId>ojdbc8</artifactId>
<version>12.2.0.1</version>
</dependency> -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-jpa</artifactId>
</dependency>
<!-- <dependency>
<groupId>org.hsqldb</groupId>
<artifactId>hsqldb</artifactId>
<scope>runtime</scope>
</dependency> -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-mail</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-quartz</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
<!-- Exclude the default Jackson dependency -->
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<version>${lombok.version}</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>com.google.code.gson</groupId>
<artifactId>gson</artifactId>
<version>${gson.version}</version>
</dependency>
<dependency>
<groupId>io.springfox</groupId>
<artifactId>springfox-swagger2</artifactId>
<version>2.7.0</version>
</dependency>
<dependency>
<groupId>io.springfox</groupId>
<artifactId>springfox-swagger-ui</artifactId>
<version>2.7.0</version>
<scope>compile</scope>
</dependency>
</dependencies>
<profiles>
<profile>
<id>desenv</id>
<properties>
<activatedProperties>desenv</activatedProperties>
</properties>
<activation>
<activeByDefault>true</activeByDefault>
</activation>
</profile>
<profile>
<id>prod</id>
<properties>
<activatedProperties>prod</activatedProperties>
</properties>
</profile>
</profiles>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
<resources>
<resource>
<directory>src/main/resources</directory>
<filtering>true</filtering>
</resource>
</resources>
</build>
The endpoint requires authorization and you should provide it with the request (credentials, token, etc.). Clean the cookies on Postman and I guess it’ll stop working too.

Problem hitting REST endpoint in Spring Boot 2.2.0 application

I try to create a Spring Boot REST application with GraphQL and MongoeDB (I start from that article https://medium.com/oril/spring-boot-graphql-mongodb-8733002b728a). The application start but I got 404 error when I try to post something to a endpoint.
I read also that #PostConstruct is not supported anymore with 2.2.0 (it's another problem I'll try to figure out later, I try to preload the database during startup).
pom.xml
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.2.0.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>com.tsoai-api</groupId>
<artifactId>demo</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>demo</name>
<description>Demo project for Spring Boot</description>
<properties>
<java.version>1.8</java.version>
<lombok.version>1.18.10</lombok.version>
<commons-io.version>2.5</commons-io.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-mongodb</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
<exclusions>
<exclusion>
<groupId>org.junit.vintage</groupId>
<artifactId>junit-vintage-engine</artifactId>
</exclusion>
</exclusions>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<version>${lombok.version}</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>commons-io</groupId>
<artifactId>commons-io</artifactId>
<version>${commons-io.version}</version>
</dependency>
<dependency>
<groupId>com.graphql-java</groupId>
<artifactId>graphql-spring-boot-starter</artifactId>
<version>3.6.0</version>
</dependency>
<dependency>
<groupId>com.graphql-java</groupId>
<artifactId>graphql-java-tools</artifactId>
<version>3.2.0</version>
</dependency>
<dependency>
<groupId>com.graphql-java</groupId>
<artifactId>graphiql-spring-boot-starter</artifactId>
<version>3.6.0</version>
</dependency>
<dependency>
<groupId>javax.annotation</groupId>
<artifactId>javax.annotation-api</artifactId>
<version>1.3.2</version>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>
My main application file look like this (nothing very special!):
#SpringBootApplication
public class DemoApplication {
public static void main(String[] args) {
SpringApplication.run(DemoApplication.class, args);
}
}
MainController.java
#RestController
#RequestMapping(path = "/query")
public class MainController {
private GraphQL graphQL;
private GraphQlUtility graphQlUtility;
#Autowired
MainController(GraphQlUtility graphQlUtility) throws IOException {
this.graphQlUtility = graphQlUtility;
graphQL = graphQlUtility.createGraphQlObject();
}
#PostMapping(path= "/")
public ResponseEntity query(#RequestBody String query){
ExecutionResult result = graphQL.execute(query);
System.out.println("errors: "+result.getErrors());
return ResponseEntity.ok(result.getData());
}
}
When I post on http://localhost:8080/query, I got this error:
{
"timestamp": "2019-10-19T16:10:19.136+0000",
"status": 404,
"error": "Not Found",
"message": "No message available",
"path": "/query"
}
It will match /query/ only but not /query . If you want to match both /query and /query/ , you can just leave path of the #PostMapping in the query method as the default.
#RestController
#RequestMapping(path = "/query")
public class MainController {
#PostMapping
public ResponseEntity query(#RequestBody String query){
}
}
There could be a reason for this error that Spring boot is not scanning this class. Please try to add ComponentScan annotation on Spring boot main class with the package where you keep your controllers as given below.
#ComponentScan(basePackages= {"com.example"})
Check the port on which your Spring Server is running, According to the medium post they have configured it to run on :8000 while you're hitting on :8080.
The error states the same that it cannot find any controller mapped to this api-endpoint.
http://localhost:8000/query/
`

Spring boot blank page response on browser

I have a web application using maven & spring boot with a war deployment.
While it works normally on a windows machine (starting it using netbeans), I get a blank page response from browser when I run it on a linux machine (not sure if this is relevant). No errors occur on startup.
After searching a while I found that it is related with some 404 error response of spring boot. This happens for any route that I try (valid or not)
Another clue is that I tried to redirect 404 errors to a test.jsp but nothing changed in the browsers (I still get this blank page). With postman I get this .jsp as a response.
In any case it's not normal to get an error response since the routes are correct.
Here is my pom.xml
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation=`"http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">`
<modelVersion>4.0.0</modelVersion>
<groupId>org</groupId>
<artifactId>app</artifactId>
<version>1.0-RELEASE</version>
<packaging>war</packaging>
<name>webapp</name>
<description></description>
<properties>
<endorsed.dir>${project.build.directory}/endorsed</endorsed.dir>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<java.version>1.8</java.version>
</properties>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>1.5.8.RELEASE</version>
</parent>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-jpa</artifactId>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-devtools</artifactId>
<optional>true</optional>
</dependency>
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>jstl</artifactId>
</dependency>
<!-- Provided
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-tomcat</artifactId>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>org.apache.tomcat.embed</groupId>
<artifactId>tomcat-embed-jasper</artifactId>
<scope>provided</scope>
</dependency>-->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-test</artifactId>
<type>jar</type>
</dependency>
<dependency>
<groupId>org.jsoup</groupId>
<artifactId>jsoup</artifactId>
<version>1.11.2</version>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
<configuration>
<includeSystemScope>true</includeSystemScope>
</configuration>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-war-plugin</artifactId>
<version>3.2.0</version>
<configuration>
<!--<failOnMissingWebXml>true</failOnMissingWebXml>-->
<webResources>
<resource>
<directory>${basedir}/src/main/resources/lib</directory>
<targetPath>WEB-INF/lib</targetPath>
</resource>
</webResources>
</configuration>
</plugin>
</plugins>
</build>
</project>
Any ideas?
EDIT
I add some extra info that might help:
No MVC-relevant configuration is included in application.properties
Application.java (I'm not using #EnableWebMvc):
#SpringBootApplication
#EnableScheduling
public class Application extends SpringBootServletInitializer {
private static final Logger log = LoggerFactory.getLogger(Application.class);
public static void main(String[] args) {
SpringApplication.run(Application.class, args);
}
#Override
protected SpringApplicationBuilder configure(SpringApplicationBuilder application) {
return application.sources(Application.class);
}
}
Web MVC Configuration is done by:
#Configuration
public class MvcConfig extends WebMvcConfigurerAdapter {
#Bean
public ViewResolver getPageViewResolver(){
InternalResourceViewResolver resolver = new InternalResourceViewResolver();
resolver.setPrefix("/WEB-INF/pages/");
resolver.setSuffix(".jsp");
resolver.setViewClass(JstlView.class);
resolver.setOrder(1);
return resolver;
}
}
After some tries I conclude that:
mvn spring-boot:run
java -classpath "lib/*:classes/." org.app.Application
java -jar webapp-1.0.RELEASE.war
start the app as a normal application which leads to this problem. No matter if pom packing is defined as jar or war (If I'm wrong someone pls correct me)
Netbeans was deploying the app into an external tomcat server. I finally tried the classic war deployment way in order to make it work.
This answer is not solving the original question but provides a fix in case someone else has the same problem.

Unable to access and use static resources in Spring Boot Application

I have been fighting with getting resources to work for quite sometime now, and I know there are thousands of similar questions.. Anyway, my condition is this
I will start with my pom.. I might have missed something here?
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0
http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.ruruapps</groupId>
<artifactId>spring-demo</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>jar</packaging>
<name>spring-demo</name>
<description>Spring-Demo Application</description>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>1.2.4.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<org.thymeleaf-version>2.1.4</org.thymeleaf-version>
<webjars-bootstrap.version>3.3.5</webjars-bootstrap.version>
<webjars-jquery-ui.version>2.1.1</webjars-jquery-ui.version>
<webjars-jquery.version>2.0.3-1</webjars-jquery.version>
<java.version>1.8</java.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-actuator</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-batch</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-jpa</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-rest</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-undertow</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-tomcat</artifactId>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-security</artifactId>
</dependency>
<!-- Thymeleaf -->
<!--<dependency>-->
<!--<groupId>org.thymeleaf</groupId>-->
<!--<artifactId>thymeleaf</artifactId>-->
<!--<version>2.1.4.RELEASE</version>-->
<!--</dependency>-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-thymeleaf</artifactId>
</dependency>
<!-- Webjars (static dependencies distributed as WAR files) -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-websocket</artifactId>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<scope>runtime</scope>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.webjars</groupId>
<artifactId>bootstrap</artifactId>
<version>3.3.5</version>
</dependency>
<dependency>
<groupId>org.webjars</groupId>
<artifactId>jquery</artifactId>
<version>2.1.4</version>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>
Here are the errors I am dealing with
And in the terminal I see this
Looking up handler method for path /css/bootstrap/css/bootstrap.min.css
Did not find handler method for [/css/bootstrap/css/bootstrap.min.css]
Request method 'GET' not supported
What does this mean?
I have a correct structure and doing it by the convention
But no luck what so ever. I tried changing to public/ and tried using webjars. All in vain. What are the norms for this and how do I get this set up so it wont be breaking! Thanks!
EDIT:: Configuration Files
Here is my application file that is provided with the spring boot
#SpringBootApplication
#EnableAutoConfiguration(exclude = {
org.springframework.boot.autoconfigure.security.SecurityAutoConfiguration.class,
org.springframework.boot.actuate.autoconfigure.ManagementSecurityAutoConfiguration.class})
#ComponentScan(basePackages = {"webapp"})
#EnableJpaRepositories
public class SpringDemoApplication {
public static void main(String[] args) {
ApplicationContext ctx = SpringApplication.run(SpringDemoApplication.class, args);
}
}
And I have a WebConfig file which is mostly empty, but I tried adding the #Overide on addResourceHandlers, with no improvements
#Configuration
#EnableWebMvc
#ComponentScan(basePackages = {"webapp"})
public class WebConfig extends WebMvcConfigurerAdapter {
#Override
public void addResourceHandlers(ResourceHandlerRegistry registry) {
registry.addResourceHandler("/resources/**").addResourceLocations("classpath:/resources/static/").setCachePeriod(31556926);
}
}
EDIT
After further examining the application, I have found this
It says that the request it allows is PUT, but why? From what I can understand is that something is blocking my request to the resource files, is that right?
Desperate to get this fixed, it's really bothering to configure Spring, even with Spring Boot!
Found a problem that I was facing with not being able to access static resources.
For those who experience the same:
This behaviour (405 error) signifies that something is blocking the path to your static resources. This doesn't have to be /static or /public. It wont do anything.
In my case the problem was that a controller was blocking the /css path. To fix this a #RequestMapping(value = "/Somepath") annotation on the class lvl is needed to release the path for the static resources.
This worked for me and now I can access my static resources without any issues.
This can be a very annoying problem and someone can spend hours and days trying to figure this out. Hope this helps others to save their time
If there are alternative or other methods of preventing this behaviour that please mention in the comments
If you are trying to configure resource mapping through configuration class as you mentioned in comment, then it should be like this:
#EnableWebMvc
#ComponentScan(basePackages = {"org.company.yourpackage"})
#Configuration
public class AppConfig extends WebMvcConfigurerAdapter {
#Override
public void addResourceHandlers(ResourceHandlerRegistry registry) {
registry.addResourceHandler("/resources/**").addResourceLocations("classpath:/resources/static/").setCachePeriod(31556926);
}
}
this post also may help.
I was having the exact same issue, What helped me was adding:
enter code here
<servlet-mapping>
<servlet-name>default</servlet-name>
<url-pattern>*.js</url-pattern>
</servlet-mapping>
OR
<servlet-mapping>
<servlet-name>default</servlet-name>
<url-pattern>/js/*</url-pattern>
</servlet-mapping>
from this post
servlet for serving static content here

Categories

Resources