我正在使用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+ …我有一个用例,它调用以下内容:
@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.
我的问题是:
getOne(id)方法?getOne(id)方法?我正在使用其他存储库并且所有使用该getOne(id)方法并且所有工作正常,只有当我使用Propagation.REQUIRES_NEW …
出于某种原因,我无法远程连接到我的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.
我正在使用Spring 4.0.7
我通过JavaConfig对配置Spring MVC进行了研究.
实际上直到昨天我已经看到使用这两个选项的两种配置
注意:(2)是两个类,一个用于扩展,另一个用于实现
我正在使用(2)因为我找到了很多例子,我可以配置转换器,格式化程序,资源处理程序等...
但是在最近的几天里,我试图帮助StackOverflow上的一个问题,我确实认识到(1)存在..我在Google上做了一些关于(1)的概述,并且存在一些与(1)一起工作的例子
我的问题是这篇文章的标题如何描述.
谢谢
我正在使用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,有和没有扩展:
对于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,有和没有扩展:
现在关于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) 我将Maven与M2_HOME定义为:
我有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时,我确实意识到了这一点
/Users/manuelj/.gradle/caches/modules-2/files-2.1我希望新的依赖项直接转到同一个Maven Local Repository.
因此,需要什么额外配置呢?
让我们假设一个Java应用程序,接受一个整数命令行参数,比方说bubu.
假设有一个使用一个像样的命令行解析器(我做 - http://pholser.github.com/jopt-simple/)加上记住-D java开关,这些是传递这个命令行的一些典型方法参数:
--bubu 5(--bubu=5或--bubu5)-Dbubu=5第一个是程序参数,必须由应用程序使用某个命令行解析器处理,而第二个是VM参数,并且已经由java解析,使其可用作 Integer.getInteger("bubu")
我有点困惑.我该怎么用?使用系统属性工具:
据我所知,唯一的缺点是所有命令行选项都必须使用该-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,这对我来说非常好.
通常,如果任何类扩展Exception,它将成为检查异常.Runtime exception还扩展了Exception.那怎么回事unchecked exception?
对于这种特殊情况,它们是否有自定义检入编译器?
编辑: 我对检查v/s未经检查的异常和他们的优点和cos等有正确的想法.我没有他们在答案之间的差异.
我对以下内容感到困惑
我知道,如果我使用该类中的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
直到这里我很好.
我的问题:
ScheduledFuture从scheduleWithFixedDelay(甚至来自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,因为如果我以后使用:
我以为我应该能够检索空值,因为我可以使用方法中的period参数/参数scheduleWithFixedDelay.我的意思是:运行Runnable对象,等待它完成并使用scheduledFuture.get()获取确认它已完成的空值,等待延迟时间的周期再次运行Runnable对象根据period值等....
澄清和例子非常受欢迎
提前致谢.
我正在使用Spring Boot 3,据我了解,它应该支持RFC 7807,又名“问题详细信息”,开箱即用。但是,我不知道如何启用Spring Boot(web)以这种格式返回错误。默认情况下,它似乎以标准 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)?
java ×3
spring ×3
spring-3 ×3
spring-4 ×3
spring-mvc ×3
rest ×2
build.gradle ×1
exception ×1
gradle ×1
http ×1
jpa ×1
mysql ×1
remoting ×1
spring-boot ×1
spring-data ×1
ubuntu ×1