相关疑难解决方法(0)

存储库设计模式

我见过很多存储库模式实现.具体有2种

  1. 它们公开了可查询的通用存储库,并期望服务类的lamba表达式从数据库中获取数据.

  2. 编写方法以根据业务需求从数据库中获取数据,并封装检索数据的逻辑(甚至是lambda).

哪一个是更好的方法?

design-patterns repository-pattern

7
推荐指数
1
解决办法
4174
查看次数

SpringMvc DAO接口和DAO实现的注释

我想知道我是否正确地注释这些类,因为我是注释的新手:

Country.java

@Component
public class Country {

private int countryId;
private String countryName;
private String countryCode;

/**
 * No args constructor
 */
public Country() {
}

/**
 * @param countryId
 * @param countryName
 * @param countryCode
 */
public Country(int countryId, String countryName, String countryCode) {
    this.countryId = countryId;
    this.countryName = countryName;
    this.countryCode = countryCode;
}
    //getters and setters   

}
Run Code Online (Sandbox Code Playgroud)

CountryDAO.java

@Repository
public interface CountryDAO {

    public List<Country> getCountryList();

    public void saveCountry(Country country);

    public void updateCountry(Country country);
}
Run Code Online (Sandbox Code Playgroud)

JdbcCountryDAO.java

@Component
public …
Run Code Online (Sandbox Code Playgroud)

java annotations spring-mvc

7
推荐指数
1
解决办法
2万
查看次数

RuntimeException无法映射到响应,重新抛出到HTTP容器java.lang.NullPointerException

我正在尝试使用Spring Framework,RESTful Web服务和Jersey实现从我的数据库验证用户记录.

我使用的是MySQL v5.6.0,Eclipse Galileo,Apache tomcat v6.0

UserAccessWS.java

@Path("/user")
@Service
public class UserAccessWS {
@Autowired
private IUserService userService;
private static Logger LOGGER = Logger.getLogger(UserAccessWS.class);

@POST
@Path("/validateUser")
@Produces({ MediaType.APPLICATION_JSON })
public String getValidateUser(@Context HttpServletRequest request,
        @FormParam("userName") String userName,
        @FormParam("password") String password) throws JSONException {
    LOGGER.info("getValidateUser method");
    Users users = new Users();
    users.setUserName(userName);
    users.setPassword(password);
    List<Users> userList = new ArrayList<Users>();
    userList = userService.validateUser(users);

    JSONObject userInfo = new JSONObject();
    userInfo.put("authorize", true);
    return userInfo.toString();
}

@POST
@Path("/login")
@Produces({ MediaType.APPLICATION_JSON })
public String userLogin(
        @FormParam("userName") …
Run Code Online (Sandbox Code Playgroud)

rest spring jersey nullpointerexception runtimeexception

7
推荐指数
1
解决办法
2万
查看次数

Spring bean在Web应用程序中的作用域.最佳做法

我对这个话题有些怀疑.在我们的大多数Spring bean(dao`s,服务和控制器)的应用程序中,我们使用"请求"范围.这种方法允许我们减少内存使用并创建无状态层.但另一方面,我们在Spring上下文初始化的每个请求上都松散了性能.我想在"单例"或"原型"范围内创建一些bean,例如DAO层.

您在应用程序中使用了哪些技术?也许存在一些设计Spring Web应用程序bean范围的建议?

java spring spring-mvc java-ee

6
推荐指数
2
解决办法
8092
查看次数

Spring 中的 @Service 与 @Component

我了解@Component 和@Controller、@Component 和@Repository 之间的区别,但无法找到与@Component 相比我们在@Service 中获得的附加功能。

java spring annotations spring-mvc spring-boot

6
推荐指数
1
解决办法
4544
查看次数

识别Spring MVC架构模式

我正在通过一个春天的mvc视频系列并且喜欢它!

我想更多地了解正在使用的确切架构的细节,并且无法确定正确的名称 - 以便我可以进一步阅读.

例如,我理解表示层是MVC,但不确定如何更具体地描述模式以考虑服务和资源对象的使用 - 而不是选择使用服务,DAO和Domain对象.

有什么线索可以帮助我更好地集中精力去理解下面的布局?

application
  core
     models/entities
     services
  rest
     controllers
     resources
        resource_assemblers
Run Code Online (Sandbox Code Playgroud)

编辑: Nathan Hughes的评论澄清了我与命名法的混淆,SirKometa连接了​​我没有掌握的建筑点.多谢你们.

architecture model-view-controller spring

5
推荐指数
1
解决办法
982
查看次数

我何时应该使用@Component而不是@Service?

我已经阅读过每个Spring注释中的@Component,@ Repository和@Service注释在Spring中的区别什么?

我知道@Component并且@Service在Spring Framework中几乎完全相同.它的区别在于它的用法,所以我只是使用我访问我的存储库@Service服务层.

我想知道什么是更好的选择方式@Component而不是@Service.请注意,我现在正在使用@Component我创建的工厂,因为工厂不需要处理存储库.

java spring software-quality spring-boot

5
推荐指数
1
解决办法
324
查看次数

在Spring中什么时候应该使用@Component?

从软件设计的角度来看,什么时候应该使用@Component而不是传统的Java类(需要通过“ new”显式实例化)?例如,如果我们需要创建以下模式之一的类:

  • 适配器

  • 正面

  • 战略

  • 译者

类是否应该具有@Component注释(或任何Spring衍生注释,例如@Repository/ @Controller/ @Service)?

java architecture spring

5
推荐指数
1
解决办法
259
查看次数

Spring Boot 中的 API 调用出现 404 错误

我正在开发一个 Spring Boot 应用程序。我在点击我配置的 URL 路径时收到404 错误。我哪里错了?

主控制器.java

package com.example.homes;

import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController("/add")
public class HomeController {

    @GetMapping("/hello")
    public String add() {
        return "Hello";
    }

}
Run Code Online (Sandbox Code Playgroud)

主页应用程序.java

package com.example.homes;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
import org.springframework.boot.autoconfigure.SpringBootApplication;

import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@SpringBootApplication
public class HomeApplication {

    public static void main(String[] args) {
        SpringApplication.run(HomeApplication.class, args);
        System.out.println("Inside main");
    }

}
Run Code Online (Sandbox Code Playgroud)

java spring http-status-code-404 spring-boot

5
推荐指数
1
解决办法
3449
查看次数

@RestController 和 @Component 之间的细微差别

虽然网上每个页面都说@RestController是@Component的规范,但我不知道它是否与DispatcherServlet有关。但是当我通过在 @RestController 和 @Component 之间切换来尝试下面的代码时,我没有看到相同的行为:

首先我尝试使用@RestController:

@RestComponent
public class TestController {
    @RequestMapping(value="/testController", method=RequestMethod.POST,consumes=MediaType.APPLICATION_JSON_VALUE)
    public void testController() {
        System.out.println("Hello");
    }

}
Run Code Online (Sandbox Code Playgroud)

我在控制台中得到以下输出:

你好

其次我尝试使用@Component + @ResponseBody:

@Component
@ResponseBody
public class TestController {
    @RequestMapping(value="/testController", method=RequestMethod.POST,consumes=MediaType.APPLICATION_JSON_VALUE)
    public void testController() {
        System.out.println("Hello");
    }
}
Run Code Online (Sandbox Code Playgroud)

我在邮递员上收到错误:

{
    "timestamp": 1570998345860,
    "status": 405,
    "error": "Method Not Allowed",
    "message": "Request method 'POST' not supported",
    "path": "/testController"
}
Run Code Online (Sandbox Code Playgroud)

如果两个注释相同,那么为什么输出存在差异?

下面是 @RestController 和 @Controller 的源代码,可以看出 @RestController 和 @Controller 都是 @Component 的规范:

@Target(ElementType.TYPE)
@Retention(RetentionPolicy.RUNTIME)
@Documented
@Controller
@ResponseBody
public @interface RestController { …
Run Code Online (Sandbox Code Playgroud)

java spring annotations spring-annotations spring-boot

5
推荐指数
1
解决办法
5994
查看次数