小编Man*_*dan的帖子

为Spring RESTful应用程序使用ResponseEntity <T>和@RestController时

我正在使用Spring Framework 4.0.7,以及MVC和Rest

我可以和平地工作:

  • @Controller
  • ResponseEntity<T>

例如:

@Controller
@RequestMapping("/person")
@Profile("responseentity")
public class PersonRestResponseEntityController {
Run Code Online (Sandbox Code Playgroud)

用这个方法(只是为了创建)

@RequestMapping(value="/", method=RequestMethod.POST)
public ResponseEntity<Void> createPerson(@RequestBody Person person, UriComponentsBuilder ucb){
    logger.info("PersonRestResponseEntityController  - createPerson");
    if(person==null)
        logger.error("person is null!!!");
    else
        logger.info("{}", person.toString());

    personMapRepository.savePerson(person);
    HttpHeaders headers = new HttpHeaders();
    headers.add("1", "uno");
    //http://localhost:8080/spring-utility/person/1
    headers.setLocation(ucb.path("/person/{id}").buildAndExpand(person.getId()).toUri());

    return new ResponseEntity<>(headers, HttpStatus.CREATED);
}
Run Code Online (Sandbox Code Playgroud)

返回一些东西

@RequestMapping(value="/{id}", method=RequestMethod.GET)
public ResponseEntity<Person> getPerson(@PathVariable Integer id){
    logger.info("PersonRestResponseEntityController  - getPerson - id: {}", id);
    Person person = personMapRepository.findPerson(id);
    return new ResponseEntity<>(person, HttpStatus.FOUND);
}
Run Code Online (Sandbox Code Playgroud)

工作良好

我可以这样做:

  • @RestController(我知道它与@Controller+ …

spring spring-mvc spring-3 spring-4

153
推荐指数
4
解决办法
22万
查看次数

当使用getOne和findOne方法时,Spring Data JPA

我有一个用例,它调用以下内容:

@Override
@Transactional(propagation=Propagation.REQUIRES_NEW)
public UserControl getUserControlById(Integer id){
    return this.userControlRepository.getOne(id);
}
Run Code Online (Sandbox Code Playgroud)

观察@Transactionalhas Propagation.REQUIRES_NEW,存储库使用getOne.当我运行该应用程序时,收到以下错误消息:

Exception in thread "main" org.hibernate.LazyInitializationException: 
could not initialize proxy - no Session
...
Run Code Online (Sandbox Code Playgroud)

但是,如果我改变getOne(id)findOne(id)一切工作正常.

顺便说一下,就在用例调用getUserControlById方法之前,它已经调用了insertUserControl方法

@Override
@Transactional(propagation=Propagation.REQUIRES_NEW)
public UserControl insertUserControl(UserControl userControl) {
    return this.userControlRepository.save(userControl);
}
Run Code Online (Sandbox Code Playgroud)

两种方法都是Propagation.REQUIRES_NEW,因为我正在进行简单的审计控制.

我使用该getOne方法,因为它是在JpaRepository接口中定义的,我的Repository接口从那里扩展,我当然正在使用JPA.

JpaRepository接口扩展自CrudRepository.该findOne(id)方法定义于CrudRepository.

我的问题是:

  1. 为什么失败的getOne(id)方法?
  2. 什么时候应该使用这种getOne(id)方法?

我正在使用其他存储库并且所有使用该getOne(id)方法并且所有工作正常,只有当我使用Propagation.REQUIRES_NEW …

jpa spring-data spring-data-jpa

135
推荐指数
4
解决办法
15万
查看次数

远程连接Mysql Ubuntu

出于某种原因,我无法远程连接到我的MySQL服务器.我已经尝试了一切,但我仍然遇到错误.

root@server1:/home/administrator# mysql -u monty -p -h www.ganganadores.cl
Enter password:
ERROR 1045 (28000): Access denied for user 'monty'@'server1.ganganadores.cl' (using password: YES)
Run Code Online (Sandbox Code Playgroud)

现在,我试过跑步

 GRANT ALL ON *.* to monty@localhost IDENTIFIED BY 'XXXXX'; 
 GRANT ALL ON *.* to monty@'%' IDENTIFIED BY 'XXXXXX';` 
Run Code Online (Sandbox Code Playgroud)

但仍然没有!我做错了什么?

编辑:my.cnf已注释掉bind ip.

mysql ubuntu remoting

92
推荐指数
3
解决办法
25万
查看次数

何时使用AbstractAnnotationConfigDispatcherServletInitializer和WebApplicationInitializer?

我正在使用Spring 4.0.7

我通过JavaConfig对配置Spring MVC进行了研究.

实际上直到昨天我已经看到使用这两个选项的两种配置

  1. 扩展AbstractAnnotationConfigDispatcherServletInitializer
  2. 扩展WebMvcConfigurerAdapter 实现WebApplicationInitializer

注意:(2)是两个类,一个用于扩展,另一个用于实现

我正在使用(2)因为我找到了很多例子,我可以配置转换器,格式化程序,资源处理程序等...

但是在最近的几天里,我试图帮助StackOverflow上的一个问题,我确实认识到(1)存在..我在Google上做了一些关于(1)的概述,并且存在一些与(1)一起工作的例子

我的问题是这篇文章的标题如何描述.

谢谢

spring spring-mvc spring-3 spring-4

42
推荐指数
2
解决办法
3万
查看次数

多个场景@RequestMapping与Accept或ResponseEntity一起生成JSON/XML

我正在使用Spring 4.0.7

关于Spring MVC,出于研究目的,我有以下内容:

@RequestMapping(value="/getjsonperson", 
                method=RequestMethod.GET, 
                produces=MediaType.APPLICATION_JSON_VALUE)
public @ResponseBody Person getJSONPerson(){
    logger.info("getJSONPerson - getjsonperson");
    return PersonFactory.createPerson();
}

@RequestMapping(value="/getperson.json", method=RequestMethod.GET)
public @ResponseBody Person getPersonJSON(){
    logger.info("getPerson - getpersonJSON");
    return PersonFactory.createPerson();
}
Run Code Online (Sandbox Code Playgroud)

每个都工作正常,观察JSON,有和没有扩展:

  • / getjsonperson
  • /getperson.json

对于XML也是如此

@RequestMapping(value="/getxmlperson",
                method=RequestMethod.GET,
                produces=MediaType.APPLICATION_XML_VALUE
                )
public @ResponseBody Person getXMLPerson(){
    logger.info("getXMLPerson - getxmlperson");
    return PersonFactory.createPerson();
}

@RequestMapping(value="/getperson.xml", method=RequestMethod.GET)
@ResponseBody
public Person getPersonXML(){
    logger.info("getPerson - getpersonXML");
    return PersonFactory.createPerson();
}
Run Code Online (Sandbox Code Playgroud)

每个都工作正常,观察XML,有和没有扩展:

  • / getxmlperson
  • /getperson.xml

现在关于Restful我有以下内容:

@RequestMapping(value="/person/{id}/", 
                method=RequestMethod.GET,
                produces={MediaType.APPLICATION_JSON_VALUE, 
                          MediaType.APPLICATION_XML_VALUE})
public ResponseEntity<Person> getPersonCustomizedRestrict(@PathVariable Integer id){
    Person person = personMapRepository.findPerson(id); …
Run Code Online (Sandbox Code Playgroud)

rest spring spring-mvc spring-3 spring-4

36
推荐指数
1
解决办法
12万
查看次数

Gradle不使用Maven Local Repository作为新的依赖项

我将Maven与M2_HOME定义为:

  • /Users/manuelj/apache/maven/3.2.5

我有settings.xml文件,位于:

  • /Users/manuelj/apache/maven/3.2.5/conf/settings.xml

我声明如下:

<localRepository>/Users/manuelj/apache/maven/repository</localRepository>

直到与Maven一起工作一切正常.任何新的依赖都会在那里.

我有一个基于Gradle的项目,在我的build.gradle中有很多东西,存在以下内容:

apply plugin: 'java'
apply plugin: 'maven'
apply plugin: 'eclipse'
apply plugin: 'application'

version = '1.0.0'
sourceCompatibility = '1.8'

repositories {
   mavenLocal()
   mavenCentral()
}
… more
Run Code Online (Sandbox Code Playgroud)

直到这里,一切都很好.代码编译,执行良好.

我的困惑如下.

根据我的理解,Gradle mavenLocal()应该使用与<localRepository>Maven settings.xml文件中定义的路径相同的路径.

现在确认在Maven本地存储库中存在已经下载的一些依赖项.

当我执行例如gradle build时,我确实意识到了这一点

  • 如果Maven Local Repository中已存在依赖关系,则从那里使用它.
  • 如果Maven Local Repository Gradle中不存在依赖项,则将新依赖项下载到: /Users/manuelj/.gradle/caches/modules-2/files-2.1

我希望新的依赖项直接转到同一个Maven Local Repository.

因此,需要什么额外配置呢?

gradle build.gradle

31
推荐指数
3
解决办法
5万
查看次数

Java:使用系统参数vs"常规"命令行选项

让我们假设一个Java应用程序,接受一个整数命令行参数,比方说bubu.

假设有一个使用一个像样的命令行解析器(我做 - http://pholser.github.com/jopt-simple/)加上记住-D java开关,这些是传递这个命令行的一些典型方法参数:

  1. --bubu 5(--bubu=5--bubu5)
  2. -Dbubu=5

第一个是程序参数,必须由应用程序使用某个命令行解析器处理,而第二个是VM参数,并且已经由java解析,使其可用作 Integer.getInteger("bubu")

我有点困惑.我该怎么用?使用系统属性工具:

  • 似乎没有任何成本
  • 不依赖于任何命令行解析器库
  • 提供方便(尽管意外)的API来获取值

据我所知,唯一的缺点是所有命令行选项都必须使用该-D标志.

请指教.

谢谢.

编辑

系统参数的另一个优点 - "即使应用程序不是从主程序开始的独立应用程序,它们也可用,但当应用程序是webapp或单元测试时." - 谢谢/sf/users/39998521/

EDIT2

让我更专注于此.是否有任何严重的原因(除了美学)不使用系统参数,总是如此?

EDIT3

好的,我想我现在明白了.如果我的代码可能由Web应用程序加载,则存在潜在名称冲突的问题,因为由同一Web容器托管的其他Web应用程序与我的代码共享系统属性空间.

因此,我必须事先谨慎并消除我的系统属性的歧义.所以,现在bubu不是com.shunra.myapp.bubu了.意思是代替简单

-Dbubu=5
Run Code Online (Sandbox Code Playgroud)

我有

-Dcom.shunra.myapp.bubu=5
Run Code Online (Sandbox Code Playgroud)

这对于简单的命令行应用程序来说变得不那么有吸

另一个原因是Mark Peters,这对我来说非常好.

java

21
推荐指数
1
解决办法
2742
查看次数

为什么运行时异常是未经检查的异常?

通常,如果任何类扩展Exception,它将成为检查异常.Runtime exception还扩展了Exception.那怎么回事unchecked exception

对于这种特殊情况,它们是否有自定义检入编译器

编辑: 我对检查v/s未经检查的异常和他们的优点和cos等有正确的想法.我没有他们在答案之间的差异.

java exception runtimeexception

19
推荐指数
2
解决办法
6165
查看次数

如果从scheduleWithFixedDelay/scheduleAtFixedRate方法检索ScheduledFuture.get()方法的目的是什么

我对以下内容感到困惑

我知道,如果我使用该类中的schedule方法ScheduledThreadPoolExecutor:

ScheduledFuture<?> scheduledFuture = 
scheduledThreadPoolExecutor.schedule(myClassRunnable, 5, TimeUnit.SECONDS);
Run Code Online (Sandbox Code Playgroud)

我能够通过或稍后检索, 并且应该为null,因为该任务只执行了一次并且已完成.并且null因为我正在使用方法版本而不是方法版本.它符合APIscheduledFuture.get(5, TimeUnit.SECONDS)scheduledFuture.get()shedule's Runnableshedule's Callable

直到这里我很好.

我的问题:

ScheduledFuturescheduleWithFixedDelay(甚至来自scheduleAtFixedRate)方法中检索if 的目的是什么:

ScheduledFuture<?> scheduledFuture= 
scheduledThreadPoolExecutor.scheduleWithFixedDelay(myClassRunnable, 1, 5, TimeUnit.SECONDS);
Run Code Online (Sandbox Code Playgroud)

是的,我知道两种固定方法多次执行相同的任务,直到ScheduledThreadPoolExecutor's shutdown调用该方法(它必须停止所有已安排的任务).

我做了一项研究,通过谷歌寻找一些使用ScheduledFuture返回的例子scheduleWithFixedDelay,我只找到一个使用cancel方法,取消一个特定的任务.但没有人与get合作.

我不知道我是不是错了,但如果我们正在使用,似乎没有get方法scheduleWithFixedDelay,因为如果我以后使用:

  • scheduledFuture.get() - 它仍在等待,Runnable对象保持工作多次(运行,完成,延迟,运行等......)
  • scheduledFuture.get(32,TimeUnit.SECONDS) - 总是出现TimeoutException

我以为我应该能够检索空值,因为我可以使用方法中的period参数/参数scheduleWithFixedDelay.我的意思是:运行Runnable对象,等待它完成并使用scheduledFuture.get()获取确认它已完成的空值,等待延迟时间的周期再次运行Runnable对象根据period值等....

澄清和例子非常受欢迎

提前致谢.

java java.util.concurrent

14
推荐指数
1
解决办法
8690
查看次数

如何在 Spring Boot 3 中启用问题详细信息(RFC 7807)?

我正在使用Spring Boot 3,据我了解,它应该支持RFC 7807,又名“问题详细信息”,开箱即用。但是,我不知道如何启用Spring Bootweb)以这种格式返回错误。默认情况下,它似乎以标准 SpringJSON格式返回:

{
    "timestamp": "2012-04-25T10:56:28.294+0000",
    "path": "/api/v1/some-resource",
    "status": 400,
    "error": "Bad Request",
    "exception": "java.lang.IllegalArgumentException"
}
Run Code Online (Sandbox Code Playgroud)

如何在 Spring Boot 3 中启用“问题详细信息”(RFC 7807)?

rest http spring-boot

12
推荐指数
1
解决办法
4042
查看次数