kullanma requestMapping ek açıklama kullanarak bir servlet çağırmaya çalışıyorum. Sunucuda çalışan bir index.html sayfası var. Ancak, http://localhost:8080/spring-rest/api/users yazarak bir sunucuya başvurduğumda çalışmaz. Projemde 5 tane dersim var. İşte kod.Servlet çağrısı başarısız. Spring rest
User.java
package io.egen.entity;
import lombok.Data;
@Data
public class User {
public User(String id, String firstName, String lastName, String email, String compnay) {
super();
this.id = id;
this.firstName = firstName;
this.lastName = lastName;
this.email = email;
this.compnay = compnay;
}
private String id;
private String firstName;
private String lastName;
private String email;
private String compnay;
}
UserController.java
package io.egen.controller;
import java.util.List;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;
import io.egen.entity.User;
import io.egen.services.UserService;
@RestController
@RequestMapping(value="/users")
public class UserController {
@Autowired
private UserService services;
@RequestMapping(method=RequestMethod.GET, produces=MediaType.APPLICATION_JSON_UTF8_VALUE)
public List<User> findAll() {
return services.findAll();
}
}
UserService.java
package io.egen.services;
import java.util.ArrayList;
import java.util.List;
import java.util.UUID;
import org.springframework.stereotype.Service;
import io.egen.entity.User;
@Service
public class UserService {
public List<User> findAll() {
System.out.println("Inside UserService");
List<User> users = new ArrayList<User>();
users.add(new User(UUID.randomUUID().toString(), "Praveen1", "Salitra", "[email protected]", "Egen Solutions"));
users.add(new User(UUID.randomUUID().toString(), "Praveen2", "Salitra", "[email protected]", "Egen Solutions"));
users.add(new User(UUID.randomUUID().toString(), "Praveen3", "Salitra", "[email protected]", "Egen Solutions"));
return users;
}
}
AppInitializer.java
package io.egen;
import org.springframework.web.servlet.support.AbstractAnnotationConfigDispatcherServletInitializer;
public class AppInitializer extends AbstractAnnotationConfigDispatcherServletInitializer {
@Override
protected Class<?>[] getRootConfigClasses() {
return new Class [] {AppConfig.class};
}
@Override
protected Class<?>[] getServletConfigClasses() {
return null;
}
@Override
protected String[] getServletMappings() {
System.out.println("HEy");
return new String [] {"/api/*"};
}
}
AppConfig.java
package io.egen;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.EnableWebMvc;
@Configuration
@ComponentScan
@EnableWebMvc
public class AppConfig {
}
ben sırayla USerService.java içinde findAll yöntemini çağıran UserController.java içinde findAll yöntemini çağırmak çalışıyorum
<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/maven-v4_0_0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>io.egen</groupId>
<artifactId>spring-rest</artifactId>
<packaging>war</packaging>
<version>0.0.1</version>
<name>spring-rest Maven Webapp</name>
<url>http://maven.apache.org</url>
<build>
<plugins>
<plugin>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.1</version>
<configuration>
<source>1.7</source>
<target>1.7</target>
</configuration>
</plugin>
<plugin>
<artifactId>maven-war-plugin</artifactId>
<version>2.4</version>
<configuration>
<failOnMissingWebXml>false</failOnMissingWebXml>
</configuration>
</plugin>
</plugins>
</build>
<dependencies>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>3.8.1</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-webmvc</artifactId>
<version>4.2.5.RELEASE</version>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.12</version>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<version>1.16.8</version>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>5.1.38</version>
</dependency>
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>javax.servlet-api</artifactId>
<version>4.0.0-b01</version>
</dependency>
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-databind</artifactId>
<version>2.7.3</version>
</dependency>
</dependencies>
</project>
pom.xml . Ancak AppInitializer.java'daki getservletMappings yönteminin çalışmadığını düşünüyorum. Bana yardımcı olun.
Teşekkürler!
"Çalışmıyor" ne anlama geliyor? – david
Tarayıcıyı tarayıcıdan çağırmaya çalıştığımda url http: // localhost: 8080/spring-rest/api/users, 404 bulunamadı. Ancak Userservice sınıfının bu verileri JSON biçiminde tarayıcıya döndüren bazı ham verileri vardır. Bu tarayıcı üzerinde –