This question already has answers here:
Why is my Spring #Autowired field null?
(21 answers)
What is a NullPointerException, and how do I fix it?
(12 answers)
Closed 5 months ago.
I have an entity:
#Entity
public class User {
#Id
#GeneratedValue(strategy = GenerationType.AUTO)
private Integer id;
private String login;
private String password;
public User() {
}
public User(String login, String password) {
this.login = login;
this.password = password;
}
public String getLogin() {
return login;
}
public String getPassword() {
return password;
}
}
And i created an interface with annotation #Repository, which looks like:
#Repository
public interface UserRepository extends JpaRepository<User, Integer> {
}
So, i need to save in my database new users. But, if i use #Autowired UserRepository in my #Service class and call "save" method, it throws me an error 505 with description: HTTP Status 500 – Internal Server Error
Type Exception Report
Message Cannot invoke "com.example.testservletjsp.repository.UserRepository.save(Object)" because "this.userRepository" is null
Description The server encountered an unexpected condition that prevented it from fulfilling the request.
Exception
java.lang.NullPointerException: Cannot invoke "com.example.testservletjsp.repository.UserRepository.save(Object)" because "this.userRepository" is null
com.example.testservletjsp.account.UserService.saveUser(UserService.java:28)
com.example.testservletjsp.servlets.ExplorerServlet.doPost(ExplorerServlet.java:68)
javax.servlet.http.HttpServlet.service(HttpServlet.java:682)
javax.servlet.http.HttpServlet.service(HttpServlet.java:765)
org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:52)
Note The full stack trace of the root cause is available in the server logs.
My pom.xml file:
<?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.7.3</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>com.example</groupId>
<artifactId>test-servlet-jsp</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>war</packaging>
<name>test-servlet-jsp</name>
<description>test-servlet-jsp</description>
<properties>
<java.version>11</java.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>javax.servlet-api</artifactId>
<version>4.0.1</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-jpa</artifactId>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>8.0.30</version>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>5.2.8.RELEASE</version>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-war-plugin</artifactId>
<version>3.3.1</version>
</plugin>
</plugins>
</build>
What should i do..?
As I don't have the entirety of your project I cannot see if there are some miss-configured classes, but you can always visit this (https://spring.io/guides/gs/accessing-data-mysql/) spring guide that teaches you all the details you have to keep in mind in order to get that project working.
Related
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.
I am a new to Spring boot so I apologize if this is a easily resolved issue.
I am seeing a problem running springboot after I added a #Transient final variable to my entity. Essentially I need a constant variable in my class that stores a string that I do not want stored in the database.
Adding in just this variable causes no issues with spring and the program runs as expected however, if I add a getter for this variable Springboot raises the exception:
org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'entityManagerFactory' defined in class path resource [org/springframework/boot/autoconfigure/orm/jpa/HibernateJpaConfiguration.class]: Invocation of init method failed; nested exception is javax.persistence.PersistenceException: [PersistenceUnit: default] Unable to build Hibernate SessionFactory; nested exception is org.hibernate.MappingException: Could not get constructor for org.hibernate.persister.entity.SingleTableEntityPersister
Here is my code causing the issue (I am not even calling the getter yet and it's causing this issue):
#ApiModel(description = "This class represents a device with its basic information.")
#Entity
#Table(name = "device")
public class Device {
#Transient
private final String invisibleName = "Jwfqp4bbqiFzRLcFVV3qf";
#Id
#ApiModelProperty(notes = "A UUID number to identify the device in the system.")
private String deviceId;
#ApiModelProperty(notes = "The device IP address of the device, this is also a unique identifier.")
private String ipAddress;
public Device() {
}
#Id
public String getDeviceId() {
return deviceId;
}
public void setDeviceId(String deviceUuid) {
this.deviceId = deviceUuid;
}
public String getIpAddress() {
return ipAddress;
}
public void setIpAddress(String ipAddress) {
this.ipAddress = ipAddress;
}
public String getInvisibleName(){
return invisibleName;
}
}
Here is my pom
<?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.6.7</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>com.example</groupId>
<artifactId>project-name</artifactId>
<version>0.0.1</version>
<packaging>war</packaging>
<name>projectName</name>
<description>Project Name</description>
<properties>
<java.version>1.8</java.version>
<log4j2.version>2.16.0</log4j2.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-jpa</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</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-tomcat</artifactId>
<scope>compile</scope>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter</artifactId>
<exclusions>
<exclusion>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-logging</artifactId>
</exclusion>
</exclusions>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-log4j2</artifactId>
</dependency>
<dependency>
<groupId>com.fasterxml.jackson.dataformat</groupId>
<artifactId>jackson-dataformat-xml</artifactId>
</dependency>
<dependency>
<groupId>io.springfox</groupId>
<artifactId>springfox-boot-starter</artifactId>
<version>3.0.0</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-webflux</artifactId>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>
One important thing that I've noticed is that I can resolve the building issue by adding a useless parameter to the the getter such as:
public String getInvisibleName(int temp){
return invisibleName;
}
but If possible I would like to get to the bottom of this issue so I don't have useless parameters in my getter.
Let me know if I need to provide any additional information.
Thanks
Thank you all for your help. I removed the extraneous #Id on the 'getDeviceId' and moved the #Transient to the getter. I also noticed that apparently Spring had created an 'invisibleName' entry in the database causing a mismatch with my #Entity class. I removed the field from the database and that resolved the issue.
You should move the #Transient annotation above the getInvisibleName() method.
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/
`
In my Spring boot application i should show a date string on html. I using this code to get a time jason:
spring:
jackson:
date-format: HH:mm:ss.SSSSSS
joda-date-time-format: HH:mm:ss.SSSSSS
it can be word when i run with IDEA, but it can't work when i use maven to packet a jar and run this jar.
there is exception message :
beans.factory.BeanCreationException: Error creating bean with name 'jacksonObjectMapperBuilder' defined in class path resource [org/springframework/boot/autoconfigure/jackson/JacksonAutoConfiguration$Jac
ksonObjectMapperBuilderConfiguration.class]: Bean instantiation via factory method failed; nested exception is org.springframework.beans.BeanInstantiationException: Failed to instantiate [org.springframe
work.http.converter.json.Jackson2ObjectMapperBuilder]: Factory method 'jacksonObjectMapperBuilder' threw exception; nested exception is java.lang.IllegalArgumentException: name
how should do to get a jason time String?
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>com.example</groupId>
<artifactId>demo</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>jar</packaging>
<name>demo</name>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>1.5.4.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<properties>
<thymeleaf.version>3.0.0.RELEASE</thymeleaf.version>
<thymeleaf-layout-dialect.version>2.0.0</thymeleaf-layout-dialect.version>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
<java.version>1.8</java.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-jpa</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-thymeleaf</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</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>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>
and my table class is that:
#Entity
public class TimeData {
#Id
#GeneratedValue(strategy = GenerationType.AUTO)
private long id;
#Temporal(TemporalType.TIME)
#JsonFormat(shape = JsonFormat.Shape.STRING,pattern = "HH:mm:ss.SSSSSS",timezone = "GMT+8")
private Date curingTime;
public long getId() {
return id;
}
public void setId(long id) {
this.id = id;
}
public Date getCuringTime() {
return curingTime;
}
public void setCuringTime(Date curingTime) {
this.curingTime = curingTime;
}
}
in my spring boot application i should to return a jason String with TimeData.
it can be get well run with IDEA ,but couldn't run with jar .
This smells like a classpath issue. Your stacktrace also has nothing to do with your actual code. I think you may be pulling in incompatible versions of the same library somehow. Ensure you actually need all those dependencies and see what you can remove and try rerunning your jar.
I generated a project on start.spring.io with the following project dependencies:
Jersey (JAX-RS)
JPA
PostgreSQL
Web
When I try to access localhost:8080/homeroom/webapi/test I get a page with the following error:
Whitelabel Error Page
This application has no explicit mapping for /error, so you are seeing this as a fallback.
I get this error no matter what url I try to access.
In my console info I can see it print Mapping servlet: 'jerseyServlet' to [/webapi/*] so I know my config class is being registered. When I change #ApplicationPath("/webapi") to #ApplicationPath("/"), a GET on localhost:8080/homeroom/test or localhost:8080/homeroom/ returns a blank page instead, with no text or error.
Why can't I access my resource?
This 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>com.homeroomed</groupId>
<artifactId>homeroom</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>jar</packaging>
<name>HomeRoom</name>
<description>HomeRoom REST API</description>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>1.2.5.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<java.version>1.8</java.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-jpa</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-jersey</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.postgresql</groupId>
<artifactId>postgresql</artifactId>
<version>9.4-1201-jdbc41</version>
<scope>runtime</scope>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>
I'm trying to do a descriptor-less deploy so I have:
#Component
#ApplicationPath("/webapi")
public class MyJerseyConfig extends ResourceConfig{
public MyJerseyConfig() {
// String packageName =TestJerseyResource.class.getPackage().getName();
// this.packages(packageName);
this.register(TestJerseyResource.class);
// System.out.println("The package is:"+packageName);
}
}
And I have the following resource:
#Component
#Path("/test")
public class TestJerseyResource {
#GET
#Produces(MediaType.APPLICATION_JSON)
public String getTest()
{
return "Hi!";
}
}
I'm running the project from:
#SpringBootApplication
public class HomeRoomApplication {
public static void main(String[] args) {
SpringApplication.run(HomeRoomApplication.class, args);
}
}
UPDATE:
So 2 things:
I had to use spring annotations #Controller and #RequestMapping instead of Jersey's #PATH and #GET, and had to GET /webapi/test instead of /homeroom/webapi/test
The reason you can't access your resource from looking at the information you provided is you there is no /homeroom anywhere I saw in your code.
This is a valid URL for your project:
http://localhost:8080/webapi/test
If you wanted homeroom to be in the URL you could change the application path value to homeroom instead of webapi.
Well, quoting from #ApplicationPath JavaDoc:
May only be applied to a subclass of Application.
https://docs.oracle.com/javaee/6/api/javax/ws/rs/ApplicationPath.html
That might be part of the problem...