Java Spring 4(带注释)Rest Controller未被Firefox

时间:2016-11-15 23:32:55

标签: spring rest spring-boot

  

您好,

     

我遇到一个让我感到困惑的问题,因为映射应该可以工作,看起来它在调试模式下启动Spring Boot时会映射。我不知道在哪里可以检查这个问题的明显解决方案。

     

这是application.properties:

server.port=8082
server.contextPath = /
  

这是SpringBootInitializer类,它添加了一个" / api"到> Servlet注册:

public class App extends SpringBootServletInitializer {

@Bean
public DispatcherServlet dispatcherServlet() {
    return new DispatcherServlet();
}

@Bean
public ServletRegistrationBean dispatcherServletRegistration() {
    final ServletRegistrationBean registration = new ServletRegistrationBean(dispatcherServlet(), "/api/*");

    final Map<String, String> params = new HashMap<String, String>();
    params.put("contextClass", "org.springframework.web.context.support.AnnotationConfigWebApplicationContext");
    params.put("contextConfigLocation", "org.spring.sec2.spring");
    params.put("dispatchOptionsRequest", "true");
    registration.setInitParameters(params);

    registration.setLoadOnStartup(1);
    return registration;
}

//

@Override
protected SpringApplicationBuilder configure(final SpringApplicationBuilder application) {
    return application.initializers(new MyApplicationContextInitializer()).sources(App.class);
}

public static void main(final String... args) {
    new SpringApplicationBuilder(App.class).initializers(new MyApplicationContextInitializer()).run(args);
}
}
  

这是Controler,它增加了一些用户&#34;到映射。我设置了调试点的方法&gt;是findAll,不需要进一步映射到&gt;到达它(即/ users /的根<:

@Controller
@RequestMapping(value = users)
public class UserController extends AbstractController<User> {

@Autowired
private IUserService userService;

public UserController() {
    super(User.class);
}

// API

// find

@RequestMapping(method = RequestMethod.GET)
@ResponseBody
public void getItsWorking() {
    System.out.println("It's Working!!!");
}

}
  

以下是用户实体:

@Entity
public class User implements IEntity {

@Id
@GeneratedValue(strategy = GenerationType.AUTO)
@Column(name="user_id")
private Long user_id;

@Column(name = "username", unique = true, nullable = false)
private String name;

@Column(unique = true, nullable = false)
private String email;

@Column(nullable = false)
private String password;

@Column(nullable = false)
private Boolean locked;


public User() {
    super();
}

public User(final String nameToSet, final String passwordToSet, /*final       
Set<Role> rolesToSet,*/ final Boolean lockedToSet) {
    super();

    name = nameToSet;
    password = passwordToSet;
    locked = lockedToSet;
}

// API

public Long getId() {
    return user_id;
}

public void setId(final Long idToSet) {
    user_id = idToSet;
}

public String getName() {
    return name;
}

public void setName(final String nameToSet) {
    name = nameToSet;
}

public String getEmail() {
    return email;
}

public void setEmail(final String emailToSet) {
    email = emailToSet;
}

public String getPassword() {
    return password;
}

public void setPassword(final String passwordToSet) {
    password = passwordToSet;
}

public Boolean getLocked() {
    return locked;
}

public void setLocked(final Boolean lockedToSet) {
    locked = lockedToSet;
}

}
  

以下是Spring Boot调试启动时的输出:

Mapped "{[/users],methods=[GET]}" onto public    
java.util.List<org.um.persistence.model.User> 
     org.um.web.controller.UserController.findAll(javax.servlet.http.HttpServletRequest)
  

所以,看起来它正确映射,但是当我使用Firefox中的Rest&gt;客户端工具添加它时,我在执行&#34; GET&#34;时会得到以下内容:在我的标题中使用Content-Type:application / json的&gt;以下网址:http://localhost:8082/api/users

Rest Client 404 Image

  

发生了什么? 非常困惑。

1 个答案:

答案 0 :(得分:1)

你应该在你的课堂上加@RequestMapping("/api"),在你的方法上加@RequestMapping("/users")(最好将一些东西归还给客户)。

这种方式将您的端点公开为/api/users,您可以轻松地将/api/*下的其他端点添加到此类中。